7 * parses an XML Schema, allows access to it's data, other utility methods.
8 * imperfect, no validation... yet, but quite functional.
10 * @author Dietrich Ayala <dietrich@ganx4.com>
11 * @author Scott Nichol <snichol@users.sourceforge.net>
12 * @version $Id: class.xmlschema.php,v 1.53 2010/04/26 20:15:08 snichol Exp $
15 class nusoap_xmlschema extends nusoap_base {
21 var $enclosingNamespaces;
23 var $schemaInfo = array();
24 var $schemaTargetNamespace = '';
25 // types, elements, attributes defined by the schema
26 var $attributes = array();
27 var $complexTypes = array();
28 var $complexTypeStack = array();
29 var $currentComplexType = null;
30 var $elements = array();
31 var $elementStack = array();
32 var $currentElement = null;
33 var $simpleTypes = array();
34 var $simpleTypeStack = array();
35 var $currentSimpleType = null;
37 var $imports = array();
42 var $depth_array = array();
43 var $message = array();
44 var $defaultNamespace = array();
49 * @param string $schema schema document URI
50 * @param string $xml xml document URI
51 * @param string $namespaces namespaces defined in enclosing XML
54 function nusoap_xmlschema($schema='',$xml='',$namespaces=array()){
55 parent::nusoap_base();
56 $this->debug('nusoap_xmlschema class instantiated, inside constructor');
58 $this->schema = $schema;
62 $this->enclosingNamespaces = $namespaces;
63 $this->namespaces = array_merge($this->namespaces, $namespaces);
67 $this->debug('initial schema file: '.$schema);
68 $this->parseFile($schema, 'schema');
73 $this->debug('initial xml file: '.$xml);
74 $this->parseFile($xml, 'xml');
82 * @param string $xml path/URL to XML file
83 * @param string $type (schema | xml)
87 function parseFile($xml,$type){
90 $xmlStr = @join("",@file($xml));
92 $msg = 'Error reading XML from '.$xml;
93 $this->setError($msg);
97 $this->debug("parsing $xml");
98 $this->parseString($xmlStr,$type);
99 $this->debug("done parsing $xml");
107 * parse an XML string
109 * @param string $xml path or URL
110 * @param string $type (schema|xml)
113 function parseString($xml,$type){
117 // Create an XML parser.
118 $this->parser = xml_parser_create();
119 // Set the options for parsing the XML data.
120 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
122 // Set the object for the parser.
123 xml_set_object($this->parser, $this);
125 // Set the element handlers for the parser.
126 if($type == "schema"){
127 xml_set_element_handler($this->parser, 'schemaStartElement','schemaEndElement');
128 xml_set_character_data_handler($this->parser,'schemaCharacterData');
129 } elseif($type == "xml"){
130 xml_set_element_handler($this->parser, 'xmlStartElement','xmlEndElement');
131 xml_set_character_data_handler($this->parser,'xmlCharacterData');
134 // Parse the XML file.
135 if(!xml_parse($this->parser,$xml,true)){
136 // Display an error message.
137 $errstr = sprintf('XML error parsing XML schema on line %d: %s',
138 xml_get_current_line_number($this->parser),
139 xml_error_string(xml_get_error_code($this->parser))
141 $this->debug($errstr);
142 $this->debug("XML payload:\n" . $xml);
143 $this->setError($errstr);
146 xml_parser_free($this->parser);
148 $this->debug('no xml passed to parseString()!!');
149 $this->setError('no xml passed to parseString()!!');
154 * gets a type name for an unnamed type
156 * @param string Element name
157 * @return string A type name for an unnamed type
160 function CreateTypeName($ename) {
162 for ($i = 0; $i < count($this->complexTypeStack); $i++) {
163 $scope .= $this->complexTypeStack[$i] . '_';
165 return $scope . $ename . '_ContainedType';
169 * start-element handler
171 * @param string $parser XML parser object
172 * @param string $name element name
173 * @param string $attrs associative array of attributes
176 function schemaStartElement($parser, $name, $attrs) {
178 // position in the total number of elements, starting from 0
179 $pos = $this->position++;
180 $depth = $this->depth++;
181 // set self as current value for this depth
182 $this->depth_array[$depth] = $pos;
183 $this->message[$pos] = array('cdata' => '');
185 $this->defaultNamespace[$pos] = $this->defaultNamespace[$this->depth_array[$depth - 1]];
187 $this->defaultNamespace[$pos] = false;
190 // get element prefix
191 if($prefix = $this->getPrefix($name)){
192 // get unqualified name
193 $name = $this->getLocalPart($name);
198 // loop thru attributes, expanding, and registering namespace declarations
199 if(count($attrs) > 0){
200 foreach($attrs as $k => $v){
201 // if ns declarations, add to class level array of valid namespaces
202 if(preg_match('/^xmlns/',$k)){
203 //$this->xdebug("$k: $v");
204 //$this->xdebug('ns_prefix: '.$this->getPrefix($k));
205 if($ns_prefix = substr(strrchr($k,':'),1)){
206 //$this->xdebug("Add namespace[$ns_prefix] = $v");
207 $this->namespaces[$ns_prefix] = $v;
209 $this->defaultNamespace[$pos] = $v;
210 if (! $this->getPrefixFromNamespace($v)) {
211 $this->namespaces['ns'.(count($this->namespaces)+1)] = $v;
214 if($v == 'http://www.w3.org/2001/XMLSchema' || $v == 'http://www.w3.org/1999/XMLSchema' || $v == 'http://www.w3.org/2000/10/XMLSchema'){
215 $this->XMLSchemaVersion = $v;
216 $this->namespaces['xsi'] = $v.'-instance';
220 foreach($attrs as $k => $v){
221 // expand each attribute
222 $k = strpos($k,':') ? $this->expandQname($k) : $k;
223 $v = strpos($v,':') ? $this->expandQname($v) : $v;
230 // find status, register data
232 case 'all': // (optional) compositor content for a complexType
236 //$this->xdebug("compositor $name for currentComplexType: $this->currentComplexType and currentElement: $this->currentElement");
237 $this->complexTypes[$this->currentComplexType]['compositor'] = $name;
238 //if($name == 'all' || $name == 'sequence'){
239 // $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
242 case 'attribute': // complexType attribute
243 //$this->xdebug("parsing attribute $attrs[name] $attrs[ref] of value: ".$attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']);
244 $this->xdebug("parsing attribute:");
245 $this->appendDebug($this->varDump($attrs));
246 if (!isset($attrs['form'])) {
247 // TODO: handle globals
248 $attrs['form'] = $this->schemaInfo['attributeFormDefault'];
250 if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) {
251 $v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
252 if (!strpos($v, ':')) {
253 // no namespace in arrayType attribute value...
254 if ($this->defaultNamespace[$pos]) {
255 // ...so use the default
256 $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'] = $this->defaultNamespace[$pos] . ':' . $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
260 if(isset($attrs['name'])){
261 $this->attributes[$attrs['name']] = $attrs;
262 $aname = $attrs['name'];
263 } elseif(isset($attrs['ref']) && $attrs['ref'] == 'http://schemas.xmlsoap.org/soap/encoding/:arrayType'){
264 if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) {
265 $aname = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
269 } elseif(isset($attrs['ref'])){
270 $aname = $attrs['ref'];
271 $this->attributes[$attrs['ref']] = $attrs;
274 if($this->currentComplexType){ // This should *always* be
275 $this->complexTypes[$this->currentComplexType]['attrs'][$aname] = $attrs;
277 // arrayType attribute
278 if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']) || $this->getLocalPart($aname) == 'arrayType'){
279 $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
280 $prefix = $this->getPrefix($aname);
281 if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])){
282 $v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
286 if(strpos($v,'[,]')){
287 $this->complexTypes[$this->currentComplexType]['multidimensional'] = true;
289 $v = substr($v,0,strpos($v,'[')); // clip the []
290 if(!strpos($v,':') && isset($this->typemap[$this->XMLSchemaVersion][$v])){
291 $v = $this->XMLSchemaVersion.':'.$v;
293 $this->complexTypes[$this->currentComplexType]['arrayType'] = $v;
296 case 'complexContent': // (optional) content for a complexType
297 $this->xdebug("do nothing for element $name");
300 array_push($this->complexTypeStack, $this->currentComplexType);
301 if(isset($attrs['name'])){
302 // TODO: what is the scope of named complexTypes that appear
303 // nested within other c complexTypes?
304 $this->xdebug('processing named complexType '.$attrs['name']);
305 //$this->currentElement = false;
306 $this->currentComplexType = $attrs['name'];
307 $this->complexTypes[$this->currentComplexType] = $attrs;
308 $this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
309 // This is for constructs like
310 // <complexType name="ListOfString" base="soap:Array">
312 // <element name="string" type="xsd:string"
313 // minOccurs="0" maxOccurs="unbounded" />
316 if(isset($attrs['base']) && preg_match('/:Array$/',$attrs['base'])){
317 $this->xdebug('complexType is unusual array');
318 $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
320 $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
323 $name = $this->CreateTypeName($this->currentElement);
324 $this->xdebug('processing unnamed complexType for element ' . $this->currentElement . ' named ' . $name);
325 $this->currentComplexType = $name;
326 //$this->currentElement = false;
327 $this->complexTypes[$this->currentComplexType] = $attrs;
328 $this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
329 // This is for constructs like
330 // <complexType name="ListOfString" base="soap:Array">
332 // <element name="string" type="xsd:string"
333 // minOccurs="0" maxOccurs="unbounded" />
336 if(isset($attrs['base']) && preg_match('/:Array$/',$attrs['base'])){
337 $this->xdebug('complexType is unusual array');
338 $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
340 $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
343 $this->complexTypes[$this->currentComplexType]['simpleContent'] = 'false';
346 array_push($this->elementStack, $this->currentElement);
347 if (!isset($attrs['form'])) {
348 if ($this->currentComplexType) {
349 $attrs['form'] = $this->schemaInfo['elementFormDefault'];
352 $attrs['form'] = 'qualified';
355 if(isset($attrs['type'])){
356 $this->xdebug("processing typed element ".$attrs['name']." of type ".$attrs['type']);
357 if (! $this->getPrefix($attrs['type'])) {
358 if ($this->defaultNamespace[$pos]) {
359 $attrs['type'] = $this->defaultNamespace[$pos] . ':' . $attrs['type'];
360 $this->xdebug('used default namespace to make type ' . $attrs['type']);
363 // This is for constructs like
364 // <complexType name="ListOfString" base="soap:Array">
366 // <element name="string" type="xsd:string"
367 // minOccurs="0" maxOccurs="unbounded" />
370 if ($this->currentComplexType && $this->complexTypes[$this->currentComplexType]['phpType'] == 'array') {
371 $this->xdebug('arrayType for unusual array is ' . $attrs['type']);
372 $this->complexTypes[$this->currentComplexType]['arrayType'] = $attrs['type'];
374 $this->currentElement = $attrs['name'];
375 $ename = $attrs['name'];
376 } elseif(isset($attrs['ref'])){
377 $this->xdebug("processing element as ref to ".$attrs['ref']);
378 $this->currentElement = "ref to ".$attrs['ref'];
379 $ename = $this->getLocalPart($attrs['ref']);
381 $type = $this->CreateTypeName($this->currentComplexType . '_' . $attrs['name']);
382 $this->xdebug("processing untyped element " . $attrs['name'] . ' type ' . $type);
383 $this->currentElement = $attrs['name'];
384 $attrs['type'] = $this->schemaTargetNamespace . ':' . $type;
385 $ename = $attrs['name'];
387 if (isset($ename) && $this->currentComplexType) {
388 $this->xdebug("add element $ename to complexType $this->currentComplexType");
389 $this->complexTypes[$this->currentComplexType]['elements'][$ename] = $attrs;
390 } elseif (!isset($attrs['ref'])) {
391 $this->xdebug("add element $ename to elements array");
392 $this->elements[ $attrs['name'] ] = $attrs;
393 $this->elements[ $attrs['name'] ]['typeClass'] = 'element';
396 case 'enumeration': // restriction value list member
397 $this->xdebug('enumeration ' . $attrs['value']);
398 if ($this->currentSimpleType) {
399 $this->simpleTypes[$this->currentSimpleType]['enumeration'][] = $attrs['value'];
400 } elseif ($this->currentComplexType) {
401 $this->complexTypes[$this->currentComplexType]['enumeration'][] = $attrs['value'];
404 case 'extension': // simpleContent or complexContent type extension
405 $this->xdebug('extension ' . $attrs['base']);
406 if ($this->currentComplexType) {
407 $ns = $this->getPrefix($attrs['base']);
409 $this->complexTypes[$this->currentComplexType]['extensionBase'] = $this->schemaTargetNamespace . ':' . $attrs['base'];
411 $this->complexTypes[$this->currentComplexType]['extensionBase'] = $attrs['base'];
414 $this->xdebug('no current complexType to set extensionBase');
418 if (isset($attrs['schemaLocation'])) {
419 $this->xdebug('import namespace ' . $attrs['namespace'] . ' from ' . $attrs['schemaLocation']);
420 $this->imports[$attrs['namespace']][] = array('location' => $attrs['schemaLocation'], 'loaded' => false);
422 $this->xdebug('import namespace ' . $attrs['namespace']);
423 $this->imports[$attrs['namespace']][] = array('location' => '', 'loaded' => true);
424 if (! $this->getPrefixFromNamespace($attrs['namespace'])) {
425 $this->namespaces['ns'.(count($this->namespaces)+1)] = $attrs['namespace'];
430 if (isset($attrs['schemaLocation'])) {
431 $this->xdebug('include into namespace ' . $this->schemaTargetNamespace . ' from ' . $attrs['schemaLocation']);
432 $this->imports[$this->schemaTargetNamespace][] = array('location' => $attrs['schemaLocation'], 'loaded' => false);
434 $this->xdebug('ignoring invalid XML Schema construct: include without schemaLocation attribute');
437 case 'list': // simpleType value list
438 $this->xdebug("do nothing for element $name");
440 case 'restriction': // simpleType, simpleContent or complexContent value restriction
441 $this->xdebug('restriction ' . $attrs['base']);
442 if($this->currentSimpleType){
443 $this->simpleTypes[$this->currentSimpleType]['type'] = $attrs['base'];
444 } elseif($this->currentComplexType){
445 $this->complexTypes[$this->currentComplexType]['restrictionBase'] = $attrs['base'];
446 if(strstr($attrs['base'],':') == ':Array'){
447 $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
452 $this->schemaInfo = $attrs;
453 $this->schemaInfo['schemaVersion'] = $this->getNamespaceFromPrefix($prefix);
454 if (isset($attrs['targetNamespace'])) {
455 $this->schemaTargetNamespace = $attrs['targetNamespace'];
457 if (!isset($attrs['elementFormDefault'])) {
458 $this->schemaInfo['elementFormDefault'] = 'unqualified';
460 if (!isset($attrs['attributeFormDefault'])) {
461 $this->schemaInfo['attributeFormDefault'] = 'unqualified';
464 case 'simpleContent': // (optional) content for a complexType
465 if ($this->currentComplexType) { // This should *always* be
466 $this->complexTypes[$this->currentComplexType]['simpleContent'] = 'true';
468 $this->xdebug("do nothing for element $name because there is no current complexType");
472 array_push($this->simpleTypeStack, $this->currentSimpleType);
473 if(isset($attrs['name'])){
474 $this->xdebug("processing simpleType for name " . $attrs['name']);
475 $this->currentSimpleType = $attrs['name'];
476 $this->simpleTypes[ $attrs['name'] ] = $attrs;
477 $this->simpleTypes[ $attrs['name'] ]['typeClass'] = 'simpleType';
478 $this->simpleTypes[ $attrs['name'] ]['phpType'] = 'scalar';
480 $name = $this->CreateTypeName($this->currentComplexType . '_' . $this->currentElement);
481 $this->xdebug('processing unnamed simpleType for element ' . $this->currentElement . ' named ' . $name);
482 $this->currentSimpleType = $name;
483 //$this->currentElement = false;
484 $this->simpleTypes[$this->currentSimpleType] = $attrs;
485 $this->simpleTypes[$this->currentSimpleType]['phpType'] = 'scalar';
488 case 'union': // simpleType type list
489 $this->xdebug("do nothing for element $name");
492 $this->xdebug("do not have any logic to process element $name");
497 * end-element handler
499 * @param string $parser XML parser object
500 * @param string $name element name
503 function schemaEndElement($parser, $name) {
504 // bring depth down a notch
506 // position of current element is equal to the last value left in depth_array for my depth
507 if(isset($this->depth_array[$this->depth])){
508 $pos = $this->depth_array[$this->depth];
510 // get element prefix
511 if ($prefix = $this->getPrefix($name)){
512 // get unqualified name
513 $name = $this->getLocalPart($name);
518 if($name == 'complexType'){
519 $this->xdebug('done processing complexType ' . ($this->currentComplexType ? $this->currentComplexType : '(unknown)'));
520 $this->xdebug($this->varDump($this->complexTypes[$this->currentComplexType]));
521 $this->currentComplexType = array_pop($this->complexTypeStack);
522 //$this->currentElement = false;
524 if($name == 'element'){
525 $this->xdebug('done processing element ' . ($this->currentElement ? $this->currentElement : '(unknown)'));
526 $this->currentElement = array_pop($this->elementStack);
528 if($name == 'simpleType'){
529 $this->xdebug('done processing simpleType ' . ($this->currentSimpleType ? $this->currentSimpleType : '(unknown)'));
530 $this->xdebug($this->varDump($this->simpleTypes[$this->currentSimpleType]));
531 $this->currentSimpleType = array_pop($this->simpleTypeStack);
536 * element content handler
538 * @param string $parser XML parser object
539 * @param string $data element content
542 function schemaCharacterData($parser, $data){
543 $pos = $this->depth_array[$this->depth - 1];
544 $this->message[$pos]['cdata'] .= $data;
548 * serialize the schema
552 function serializeSchema(){
554 $schemaPrefix = $this->getPrefixFromNamespace($this->XMLSchemaVersion);
557 if (sizeof($this->imports) > 0) {
558 foreach($this->imports as $ns => $list) {
559 foreach ($list as $ii) {
560 if ($ii['location'] != '') {
561 $xml .= " <$schemaPrefix:import location=\"" . $ii['location'] . '" namespace="' . $ns . "\" />\n";
563 $xml .= " <$schemaPrefix:import namespace=\"" . $ns . "\" />\n";
569 foreach($this->complexTypes as $typeName => $attrs){
571 // serialize child elements
572 if(isset($attrs['elements']) && (count($attrs['elements']) > 0)){
573 foreach($attrs['elements'] as $element => $eParts){
574 if(isset($eParts['ref'])){
575 $contentStr .= " <$schemaPrefix:element ref=\"$element\"/>\n";
577 $contentStr .= " <$schemaPrefix:element name=\"$element\" type=\"" . $this->contractQName($eParts['type']) . "\"";
578 foreach ($eParts as $aName => $aValue) {
579 // handle, e.g., abstract, default, form, minOccurs, maxOccurs, nillable
580 if ($aName != 'name' && $aName != 'type') {
581 $contentStr .= " $aName=\"$aValue\"";
584 $contentStr .= "/>\n";
587 // compositor wraps elements
588 if (isset($attrs['compositor']) && ($attrs['compositor'] != '')) {
589 $contentStr = " <$schemaPrefix:$attrs[compositor]>\n".$contentStr." </$schemaPrefix:$attrs[compositor]>\n";
593 if(isset($attrs['attrs']) && (count($attrs['attrs']) >= 1)){
594 foreach($attrs['attrs'] as $attr => $aParts){
595 $contentStr .= " <$schemaPrefix:attribute";
596 foreach ($aParts as $a => $v) {
597 if ($a == 'ref' || $a == 'type') {
598 $contentStr .= " $a=\"".$this->contractQName($v).'"';
599 } elseif ($a == 'http://schemas.xmlsoap.org/wsdl/:arrayType') {
600 $this->usedNamespaces['wsdl'] = $this->namespaces['wsdl'];
601 $contentStr .= ' wsdl:arrayType="'.$this->contractQName($v).'"';
603 $contentStr .= " $a=\"$v\"";
606 $contentStr .= "/>\n";
610 if (isset($attrs['restrictionBase']) && $attrs['restrictionBase'] != ''){
611 $contentStr = " <$schemaPrefix:restriction base=\"".$this->contractQName($attrs['restrictionBase'])."\">\n".$contentStr." </$schemaPrefix:restriction>\n";
612 // complex or simple content
613 if ((isset($attrs['elements']) && count($attrs['elements']) > 0) || (isset($attrs['attrs']) && count($attrs['attrs']) > 0)){
614 $contentStr = " <$schemaPrefix:complexContent>\n".$contentStr." </$schemaPrefix:complexContent>\n";
617 // finalize complex type
618 if($contentStr != ''){
619 $contentStr = " <$schemaPrefix:complexType name=\"$typeName\">\n".$contentStr." </$schemaPrefix:complexType>\n";
621 $contentStr = " <$schemaPrefix:complexType name=\"$typeName\"/>\n";
626 if(isset($this->simpleTypes) && count($this->simpleTypes) > 0){
627 foreach($this->simpleTypes as $typeName => $eParts){
628 $xml .= " <$schemaPrefix:simpleType name=\"$typeName\">\n <$schemaPrefix:restriction base=\"".$this->contractQName($eParts['type'])."\">\n";
629 if (isset($eParts['enumeration'])) {
630 foreach ($eParts['enumeration'] as $e) {
631 $xml .= " <$schemaPrefix:enumeration value=\"$e\"/>\n";
634 $xml .= " </$schemaPrefix:restriction>\n </$schemaPrefix:simpleType>";
638 if(isset($this->elements) && count($this->elements) > 0){
639 foreach($this->elements as $element => $eParts){
640 $xml .= " <$schemaPrefix:element name=\"$element\" type=\"".$this->contractQName($eParts['type'])."\"/>\n";
644 if(isset($this->attributes) && count($this->attributes) > 0){
645 foreach($this->attributes as $attr => $aParts){
646 $xml .= " <$schemaPrefix:attribute name=\"$attr\" type=\"".$this->contractQName($aParts['type'])."\"\n/>";
651 foreach ($this->schemaInfo as $k => $v) {
652 if ($k == 'elementFormDefault' || $k == 'attributeFormDefault') {
653 $attr .= " $k=\"$v\"";
656 $el = "<$schemaPrefix:schema$attr targetNamespace=\"$this->schemaTargetNamespace\"\n";
657 foreach (array_diff($this->usedNamespaces, $this->enclosingNamespaces) as $nsp => $ns) {
658 $el .= " xmlns:$nsp=\"$ns\"";
660 $xml = $el . ">\n".$xml."</$schemaPrefix:schema>\n";
665 * adds debug data to the clas level debug string
667 * @param string $string debug data
670 function xdebug($string){
671 $this->debug('<' . $this->schemaTargetNamespace . '> '.$string);
675 * get the PHP type of a user defined type in the schema
676 * PHP type is kind of a misnomer since it actually returns 'struct' for assoc. arrays
677 * returns false if no type exists, or not w/ the given namespace
678 * else returns a string that is either a native php type, or 'struct'
680 * @param string $type name of defined type
681 * @param string $ns namespace of type
686 function getPHPType($type,$ns){
687 if(isset($this->typemap[$ns][$type])){
688 //print "found type '$type' and ns $ns in typemap<br>";
689 return $this->typemap[$ns][$type];
690 } elseif(isset($this->complexTypes[$type])){
691 //print "getting type '$type' and ns $ns from complexTypes array<br>";
692 return $this->complexTypes[$type]['phpType'];
698 * returns an associative array of information about a given type
699 * returns false if no type exists by the given name
701 * For a complexType typeDef = array(
702 * 'restrictionBase' => '',
704 * 'compositor' => '(sequence|all)',
705 * 'elements' => array(), // refs to elements array
706 * 'attrs' => array() // refs to attributes array
707 * ... and so on (see addComplexType)
710 * For simpleType or element, the array has different keys.
712 * @param string $type
715 * @see addComplexType
719 function getTypeDef($type){
720 //$this->debug("in getTypeDef for type $type");
721 if (substr($type, -1) == '^') {
723 $type = substr($type, 0, -1);
728 if((! $is_element) && isset($this->complexTypes[$type])){
729 $this->xdebug("in getTypeDef, found complexType $type");
730 return $this->complexTypes[$type];
731 } elseif((! $is_element) && isset($this->simpleTypes[$type])){
732 $this->xdebug("in getTypeDef, found simpleType $type");
733 if (!isset($this->simpleTypes[$type]['phpType'])) {
734 // get info for type to tack onto the simple type
735 // TODO: can this ever really apply (i.e. what is a simpleType really?)
736 $uqType = substr($this->simpleTypes[$type]['type'], strrpos($this->simpleTypes[$type]['type'], ':') + 1);
737 $ns = substr($this->simpleTypes[$type]['type'], 0, strrpos($this->simpleTypes[$type]['type'], ':'));
738 $etype = $this->getTypeDef($uqType);
740 $this->xdebug("in getTypeDef, found type for simpleType $type:");
741 $this->xdebug($this->varDump($etype));
742 if (isset($etype['phpType'])) {
743 $this->simpleTypes[$type]['phpType'] = $etype['phpType'];
745 if (isset($etype['elements'])) {
746 $this->simpleTypes[$type]['elements'] = $etype['elements'];
750 return $this->simpleTypes[$type];
751 } elseif(isset($this->elements[$type])){
752 $this->xdebug("in getTypeDef, found element $type");
753 if (!isset($this->elements[$type]['phpType'])) {
754 // get info for type to tack onto the element
755 $uqType = substr($this->elements[$type]['type'], strrpos($this->elements[$type]['type'], ':') + 1);
756 $ns = substr($this->elements[$type]['type'], 0, strrpos($this->elements[$type]['type'], ':'));
757 $etype = $this->getTypeDef($uqType);
759 $this->xdebug("in getTypeDef, found type for element $type:");
760 $this->xdebug($this->varDump($etype));
761 if (isset($etype['phpType'])) {
762 $this->elements[$type]['phpType'] = $etype['phpType'];
764 if (isset($etype['elements'])) {
765 $this->elements[$type]['elements'] = $etype['elements'];
767 if (isset($etype['extensionBase'])) {
768 $this->elements[$type]['extensionBase'] = $etype['extensionBase'];
770 } elseif ($ns == 'http://www.w3.org/2001/XMLSchema') {
771 $this->xdebug("in getTypeDef, element $type is an XSD type");
772 $this->elements[$type]['phpType'] = 'scalar';
775 return $this->elements[$type];
776 } elseif(isset($this->attributes[$type])){
777 $this->xdebug("in getTypeDef, found attribute $type");
778 return $this->attributes[$type];
779 } elseif (preg_match('/_ContainedType$/', $type)) {
780 $this->xdebug("in getTypeDef, have an untyped element $type");
781 $typeDef['typeClass'] = 'simpleType';
782 $typeDef['phpType'] = 'scalar';
783 $typeDef['type'] = 'http://www.w3.org/2001/XMLSchema:string';
786 $this->xdebug("in getTypeDef, did not find $type");
791 * returns a sample serialization of a given type, or false if no type by the given name
793 * @param string $type name of type
798 function serializeTypeDef($type){
799 //print "in sTD() for type $type<br>";
800 if($typeDef = $this->getTypeDef($type)){
802 if(is_array($typeDef['attrs'])){
803 foreach($typeDef['attrs'] as $attName => $data){
804 $str .= " $attName=\"{type = ".$data['type']."}\"";
807 $str .= " xmlns=\"".$this->schema['targetNamespace']."\"";
808 if(count($typeDef['elements']) > 0){
810 foreach($typeDef['elements'] as $element => $eData){
811 $str .= $this->serializeTypeDef($element);
814 } elseif($typeDef['typeClass'] == 'element') {
825 * returns HTML form elements that allow a user
826 * to enter values for creating an instance of the given type.
828 * @param string $name name for type instance
829 * @param string $type name of type
834 function typeToForm($name,$type){
836 if($typeDef = $this->getTypeDef($type)){
838 if($typeDef['phpType'] == 'struct'){
839 $buffer .= '<table>';
840 foreach($typeDef['elements'] as $child => $childDef){
842 <tr><td align='right'>$childDef[name] (type: ".$this->getLocalPart($childDef['type'])."):</td>
843 <td><input type='text' name='parameters[".$name."][$childDef[name]]'></td></tr>";
845 $buffer .= '</table>';
847 } elseif($typeDef['phpType'] == 'array'){
848 $buffer .= '<table>';
849 for($i=0;$i < 3; $i++){
851 <tr><td align='right'>array item (type: $typeDef[arrayType]):</td>
852 <td><input type='text' name='parameters[".$name."][]'></td></tr>";
854 $buffer .= '</table>';
857 $buffer .= "<input type='text' name='parameters[$name]'>";
860 $buffer .= "<input type='text' name='parameters[$name]'>";
866 * adds a complex type to the schema
876 * array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'string[]'),
880 * example: PHP associative array ( SOAP Struct )
887 * array('myVar'=> array('name'=>'myVar','type'=>'string')
891 * @param typeClass (complexType|simpleType|attribute)
892 * @param phpType: currently supported are array and struct (php assoc array)
893 * @param compositor (all|sequence|choice)
894 * @param restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
895 * @param elements = array ( name = array(name=>'',type=>'') )
896 * @param attrs = array(
898 * 'ref' => "http://schemas.xmlsoap.org/soap/encoding/:arrayType",
899 * "http://schemas.xmlsoap.org/wsdl/:arrayType" => "string[]"
902 * @param arrayType: namespace:name (http://www.w3.org/2001/XMLSchema:string)
906 function addComplexType($name,$typeClass='complexType',$phpType='array',$compositor='',$restrictionBase='',$elements=array(),$attrs=array(),$arrayType=''){
907 $this->complexTypes[$name] = array(
909 'typeClass' => $typeClass,
910 'phpType' => $phpType,
911 'compositor'=> $compositor,
912 'restrictionBase' => $restrictionBase,
913 'elements' => $elements,
915 'arrayType' => $arrayType
918 $this->xdebug("addComplexType $name:");
919 $this->appendDebug($this->varDump($this->complexTypes[$name]));
923 * adds a simple type to the schema
925 * @param string $name
926 * @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
927 * @param string $typeClass (should always be simpleType)
928 * @param string $phpType (should always be scalar)
929 * @param array $enumeration array of values
931 * @see nusoap_xmlschema
934 function addSimpleType($name, $restrictionBase='', $typeClass='simpleType', $phpType='scalar', $enumeration=array()) {
935 $this->simpleTypes[$name] = array(
937 'typeClass' => $typeClass,
938 'phpType' => $phpType,
939 'type' => $restrictionBase,
940 'enumeration' => $enumeration
943 $this->xdebug("addSimpleType $name:");
944 $this->appendDebug($this->varDump($this->simpleTypes[$name]));
948 * adds an element to the schema
950 * @param array $attrs attributes that must include name and type
951 * @see nusoap_xmlschema
954 function addElement($attrs) {
955 if (! $this->getPrefix($attrs['type'])) {
956 $attrs['type'] = $this->schemaTargetNamespace . ':' . $attrs['type'];
958 $this->elements[ $attrs['name'] ] = $attrs;
959 $this->elements[ $attrs['name'] ]['typeClass'] = 'element';
961 $this->xdebug("addElement " . $attrs['name']);
962 $this->appendDebug($this->varDump($this->elements[ $attrs['name'] ]));
967 * Backward compatibility
969 class XMLSchema extends nusoap_xmlschema {