nusoap_xmlschema

Parses an XML Schema, allows access to it's data, other utility methods.

Defined (1)

The class is defined in the following location(s).

/src/vendor/codecasts/nusoap-php7/lib/class.xmlschema.php  
  1. class nusoap_xmlschema extends nusoap_base { 
  2.  
  3. // files 
  4. var $schema = ''; 
  5. var $xml = ''; 
  6. // namespaces 
  7. var $enclosingNamespaces; 
  8. // schema info 
  9. var $schemaInfo = array(); 
  10. var $schemaTargetNamespace = ''; 
  11. // types, elements, attributes defined by the schema 
  12. var $attributes = array(); 
  13. var $complexTypes = array(); 
  14. var $complexTypeStack = array(); 
  15. var $currentComplexType = null; 
  16. var $elements = array(); 
  17. var $elementStack = array(); 
  18. var $currentElement = null; 
  19. var $simpleTypes = array(); 
  20. var $simpleTypeStack = array(); 
  21. var $currentSimpleType = null; 
  22. // imports 
  23. var $imports = array(); 
  24. // parser vars 
  25. var $parser; 
  26. var $position = 0; 
  27. var $depth = 0; 
  28. var $depth_array = array(); 
  29. var $message = array(); 
  30. var $defaultNamespace = array(); 
  31.  
  32. /** 
  33. * constructor 
  34. * @param string $schema schema document URI 
  35. * @param string $xml xml document URI 
  36. * @param string $namespaces namespaces defined in enclosing XML 
  37. * @access public 
  38. */ 
  39. function __construct($schema='', $xml='', $namespaces=array()) { 
  40. parent::__construct(); 
  41. $this->debug('nusoap_xmlschema class instantiated, inside constructor'); 
  42. // files 
  43. $this->schema = $schema; 
  44. $this->xml = $xml; 
  45.  
  46. // namespaces 
  47. $this->enclosingNamespaces = $namespaces; 
  48. $this->namespaces = array_merge($this->namespaces, $namespaces); 
  49.  
  50. // parse schema file 
  51. if($schema != '') { 
  52. $this->debug('initial schema file: '.$schema); 
  53. $this->parseFile($schema, 'schema'); 
  54.  
  55. // parse xml file 
  56. if($xml != '') { 
  57. $this->debug('initial xml file: '.$xml); 
  58. $this->parseFile($xml, 'xml'); 
  59.  
  60.  
  61. /** 
  62. * parse an XML file 
  63. * @param string $xml path/URL to XML file 
  64. * @param string $type (schema | xml) 
  65. * @return boolean 
  66. * @access public 
  67. */ 
  68. function parseFile($xml, $type) { 
  69. // parse xml file 
  70. if($xml != "") { 
  71. $xmlStr = @join("", @file($xml)); 
  72. if($xmlStr == "") { 
  73. $msg = 'Error reading XML from '.$xml; 
  74. $this->setError($msg); 
  75. $this->debug($msg); 
  76. return false; 
  77. } else { 
  78. $this->debug("parsing $xml"); 
  79. $this->parseString($xmlStr, $type); 
  80. $this->debug("done parsing $xml"); 
  81. return true; 
  82. return false; 
  83.  
  84. /** 
  85. * parse an XML string 
  86. * @param string $xml path or URL 
  87. * @param string $type (schema|xml) 
  88. * @access private 
  89. */ 
  90. function parseString($xml, $type) { 
  91. // parse xml string 
  92. if($xml != "") { 
  93.  
  94. // Create an XML parser. 
  95. $this->parser = xml_parser_create(); 
  96. // Set the options for parsing the XML data. 
  97. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); 
  98.  
  99. // Set the object for the parser. 
  100. xml_set_object($this->parser, $this); 
  101.  
  102. // Set the element handlers for the parser. 
  103. if($type == "schema") { 
  104. xml_set_element_handler($this->parser, 'schemaStartElement', 'schemaEndElement'); 
  105. xml_set_character_data_handler($this->parser, 'schemaCharacterData'); 
  106. } elseif($type == "xml") { 
  107. xml_set_element_handler($this->parser, 'xmlStartElement', 'xmlEndElement'); 
  108. xml_set_character_data_handler($this->parser, 'xmlCharacterData'); 
  109.  
  110. // Parse the XML file. 
  111. if(!xml_parse($this->parser, $xml, true)) { 
  112. // Display an error message. 
  113. $errstr = sprintf('XML error parsing XML schema on line %d: %s',  
  114. xml_get_current_line_number($this->parser),  
  115. xml_error_string(xml_get_error_code($this->parser)) 
  116. ); 
  117. $this->debug($errstr); 
  118. $this->debug("XML payload:\n" . $xml); 
  119. $this->setError($errstr); 
  120.  
  121. xml_parser_free($this->parser); 
  122. unset($this->parser); 
  123. } else{ 
  124. $this->debug('no xml passed to parseString()!!'); 
  125. $this->setError('no xml passed to parseString()!!'); 
  126.  
  127. /** 
  128. * gets a type name for an unnamed type 
  129. * @param string Element name 
  130. * @return string A type name for an unnamed type 
  131. * @access private 
  132. */ 
  133. function CreateTypeName($ename) { 
  134. $scope = ''; 
  135. for ($i = 0; $i < count($this->complexTypeStack); $i++) { 
  136. $scope .= $this->complexTypeStack[$i] . '_'; 
  137. return $scope . $ename . '_ContainedType'; 
  138.  
  139. /** 
  140. * start-element handler 
  141. * @param string $parser XML parser object 
  142. * @param string $name element name 
  143. * @param string $attrs associative array of attributes 
  144. * @access private 
  145. */ 
  146. function schemaStartElement($parser, $name, $attrs) { 
  147.  
  148. // position in the total number of elements, starting from 0 
  149. $pos = $this->position++; 
  150. $depth = $this->depth++; 
  151. // set self as current value for this depth 
  152. $this->depth_array[$depth] = $pos; 
  153. $this->message[$pos] = array('cdata' => '');  
  154. if ($depth > 0) { 
  155. $this->defaultNamespace[$pos] = $this->defaultNamespace[$this->depth_array[$depth - 1]]; 
  156. } else { 
  157. $this->defaultNamespace[$pos] = false; 
  158.  
  159. // get element prefix 
  160. if($prefix = $this->getPrefix($name)) { 
  161. // get unqualified name 
  162. $name = $this->getLocalPart($name); 
  163. } else { 
  164. $prefix = ''; 
  165.  
  166. // loop thru attributes, expanding, and registering namespace declarations 
  167. if(count($attrs) > 0) { 
  168. foreach($attrs as $k => $v) { 
  169. // if ns declarations, add to class level array of valid namespaces 
  170. if(preg_match('/^xmlns/', $k)) { 
  171. //$this->xdebug("$k: $v"); 
  172. //$this->xdebug('ns_prefix: '.$this->getPrefix($k)); 
  173. if($ns_prefix = substr(strrchr($k, ':'), 1)) { 
  174. //$this->xdebug("Add namespace[$ns_prefix] = $v"); 
  175. $this->namespaces[$ns_prefix] = $v; 
  176. } else { 
  177. $this->defaultNamespace[$pos] = $v; 
  178. if (! $this->getPrefixFromNamespace($v)) { 
  179. $this->namespaces['ns'.(count($this->namespaces)+1)] = $v; 
  180. if($v == 'http://www.w3.org/2001/XMLSchema' || $v == 'http://www.w3.org/1999/XMLSchema' || $v == 'http://www.w3.org/2000/10/XMLSchema') { 
  181. $this->XMLSchemaVersion = $v; 
  182. $this->namespaces['xsi'] = $v.'-instance'; 
  183. foreach($attrs as $k => $v) { 
  184. // expand each attribute 
  185. $k = strpos($k, ':') ? $this->expandQname($k) : $k; 
  186. $v = strpos($v, ':') ? $this->expandQname($v) : $v; 
  187. $eAttrs[$k] = $v; 
  188. $attrs = $eAttrs; 
  189. } else { 
  190. $attrs = array(); 
  191. // find status, register data 
  192. switch($name) { 
  193. case 'all': // (optional) compositor content for a complexType 
  194. case 'choice': 
  195. case 'group': 
  196. case 'sequence': 
  197. //$this->xdebug("compositor $name for currentComplexType: $this->currentComplexType and currentElement: $this->currentElement"); 
  198. $this->complexTypes[$this->currentComplexType]['compositor'] = $name; 
  199. //if($name == 'all' || $name == 'sequence') { 
  200. // $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct'; 
  201. //} 
  202. break; 
  203. case 'attribute': // complexType attribute 
  204. //$this->xdebug("parsing attribute $attrs[name] $attrs[ref] of value: ".$attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']); 
  205. $this->xdebug("parsing attribute:"); 
  206. $this->appendDebug($this->varDump($attrs)); 
  207. if (!isset($attrs['form'])) { 
  208. // TODO: handle globals 
  209. $attrs['form'] = $this->schemaInfo['attributeFormDefault']; 
  210. if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) { 
  211. $v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']; 
  212. if (!strpos($v, ':')) { 
  213. // no namespace in arrayType attribute value... 
  214. if ($this->defaultNamespace[$pos]) { 
  215. // ...so use the default 
  216. $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'] = $this->defaultNamespace[$pos] . ':' . $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']; 
  217. if(isset($attrs['name'])) { 
  218. $this->attributes[$attrs['name']] = $attrs; 
  219. $aname = $attrs['name']; 
  220. } elseif(isset($attrs['ref']) && $attrs['ref'] == 'http://schemas.xmlsoap.org/soap/encoding/:arrayType') { 
  221. if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) { 
  222. $aname = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']; 
  223. } else { 
  224. $aname = ''; 
  225. } elseif(isset($attrs['ref'])) { 
  226. $aname = $attrs['ref']; 
  227. $this->attributes[$attrs['ref']] = $attrs; 
  228.  
  229. if($this->currentComplexType) { // This should *always* be 
  230. $this->complexTypes[$this->currentComplexType]['attrs'][$aname] = $attrs; 
  231. // arrayType attribute 
  232. if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']) || $this->getLocalPart($aname) == 'arrayType') { 
  233. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array'; 
  234. $prefix = $this->getPrefix($aname); 
  235. if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) { 
  236. $v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']; 
  237. } else { 
  238. $v = ''; 
  239. if(strpos($v, '[, ]')) { 
  240. $this->complexTypes[$this->currentComplexType]['multidimensional'] = true; 
  241. $v = substr($v, 0, strpos($v, '[')); // clip the [] 
  242. if(!strpos($v, ':') && isset($this->typemap[$this->XMLSchemaVersion][$v])) { 
  243. $v = $this->XMLSchemaVersion.':'.$v; 
  244. $this->complexTypes[$this->currentComplexType]['arrayType'] = $v; 
  245. break; 
  246. case 'complexContent': // (optional) content for a complexType 
  247. $this->xdebug("do nothing for element $name"); 
  248. break; 
  249. case 'complexType': 
  250. array_push($this->complexTypeStack, $this->currentComplexType); 
  251. if(isset($attrs['name'])) { 
  252. // TODO: what is the scope of named complexTypes that appear 
  253. // nested within other c complexTypes? 
  254. $this->xdebug('processing named complexType '.$attrs['name']); 
  255. //$this->currentElement = false; 
  256. $this->currentComplexType = $attrs['name']; 
  257. $this->complexTypes[$this->currentComplexType] = $attrs; 
  258. $this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType'; 
  259. // This is for constructs like 
  260. // <complexType name="ListOfString" base="soap:Array"> 
  261. // <sequence> 
  262. // <element name="string" type="xsd:string" 
  263. // minOccurs="0" maxOccurs="unbounded" /> 
  264. // </sequence> 
  265. // </complexType> 
  266. if(isset($attrs['base']) && preg_match('/:Array$/', $attrs['base'])) { 
  267. $this->xdebug('complexType is unusual array'); 
  268. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array'; 
  269. } else { 
  270. $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct'; 
  271. } else { 
  272. $name = $this->CreateTypeName($this->currentElement); 
  273. $this->xdebug('processing unnamed complexType for element ' . $this->currentElement . ' named ' . $name); 
  274. $this->currentComplexType = $name; 
  275. //$this->currentElement = false; 
  276. $this->complexTypes[$this->currentComplexType] = $attrs; 
  277. $this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType'; 
  278. // This is for constructs like 
  279. // <complexType name="ListOfString" base="soap:Array"> 
  280. // <sequence> 
  281. // <element name="string" type="xsd:string" 
  282. // minOccurs="0" maxOccurs="unbounded" /> 
  283. // </sequence> 
  284. // </complexType> 
  285. if(isset($attrs['base']) && preg_match('/:Array$/', $attrs['base'])) { 
  286. $this->xdebug('complexType is unusual array'); 
  287. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array'; 
  288. } else { 
  289. $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct'; 
  290. $this->complexTypes[$this->currentComplexType]['simpleContent'] = 'false'; 
  291. break; 
  292. case 'element': 
  293. array_push($this->elementStack, $this->currentElement); 
  294. if (!isset($attrs['form'])) { 
  295. if ($this->currentComplexType) { 
  296. $attrs['form'] = $this->schemaInfo['elementFormDefault']; 
  297. } else { 
  298. // global 
  299. $attrs['form'] = 'qualified'; 
  300. if(isset($attrs['type'])) { 
  301. $this->xdebug("processing typed element ".$attrs['name']." of type ".$attrs['type']); 
  302. if (! $this->getPrefix($attrs['type'])) { 
  303. if ($this->defaultNamespace[$pos]) { 
  304. $attrs['type'] = $this->defaultNamespace[$pos] . ':' . $attrs['type']; 
  305. $this->xdebug('used default namespace to make type ' . $attrs['type']); 
  306. // This is for constructs like 
  307. // <complexType name="ListOfString" base="soap:Array"> 
  308. // <sequence> 
  309. // <element name="string" type="xsd:string" 
  310. // minOccurs="0" maxOccurs="unbounded" /> 
  311. // </sequence> 
  312. // </complexType> 
  313. if ($this->currentComplexType && $this->complexTypes[$this->currentComplexType]['phpType'] == 'array') { 
  314. $this->xdebug('arrayType for unusual array is ' . $attrs['type']); 
  315. $this->complexTypes[$this->currentComplexType]['arrayType'] = $attrs['type']; 
  316. $this->currentElement = $attrs['name']; 
  317. $ename = $attrs['name']; 
  318. } elseif(isset($attrs['ref'])) { 
  319. $this->xdebug("processing element as ref to ".$attrs['ref']); 
  320. $this->currentElement = "ref to ".$attrs['ref']; 
  321. $ename = $this->getLocalPart($attrs['ref']); 
  322. } else { 
  323. $type = $this->CreateTypeName($this->currentComplexType . '_' . $attrs['name']); 
  324. $this->xdebug("processing untyped element " . $attrs['name'] . ' type ' . $type); 
  325. $this->currentElement = $attrs['name']; 
  326. $attrs['type'] = $this->schemaTargetNamespace . ':' . $type; 
  327. $ename = $attrs['name']; 
  328. if (isset($ename) && $this->currentComplexType) { 
  329. $this->xdebug("add element $ename to complexType $this->currentComplexType"); 
  330. $this->complexTypes[$this->currentComplexType]['elements'][$ename] = $attrs; 
  331. } elseif (!isset($attrs['ref'])) { 
  332. $this->xdebug("add element $ename to elements array"); 
  333. $this->elements[ $attrs['name'] ] = $attrs; 
  334. $this->elements[ $attrs['name'] ]['typeClass'] = 'element'; 
  335. break; 
  336. case 'enumeration': // restriction value list member 
  337. $this->xdebug('enumeration ' . $attrs['value']); 
  338. if ($this->currentSimpleType) { 
  339. $this->simpleTypes[$this->currentSimpleType]['enumeration'][] = $attrs['value']; 
  340. } elseif ($this->currentComplexType) { 
  341. $this->complexTypes[$this->currentComplexType]['enumeration'][] = $attrs['value']; 
  342. break; 
  343. case 'extension': // simpleContent or complexContent type extension 
  344. $this->xdebug('extension ' . $attrs['base']); 
  345. if ($this->currentComplexType) { 
  346. $ns = $this->getPrefix($attrs['base']); 
  347. if ($ns == '') { 
  348. $this->complexTypes[$this->currentComplexType]['extensionBase'] = $this->schemaTargetNamespace . ':' . $attrs['base']; 
  349. } else { 
  350. $this->complexTypes[$this->currentComplexType]['extensionBase'] = $attrs['base']; 
  351. } else { 
  352. $this->xdebug('no current complexType to set extensionBase'); 
  353. break; 
  354. case 'import': 
  355. if (isset($attrs['schemaLocation'])) { 
  356. $this->xdebug('import namespace ' . $attrs['namespace'] . ' from ' . $attrs['schemaLocation']); 
  357. $this->imports[$attrs['namespace']][] = array('location' => $attrs['schemaLocation'], 'loaded' => false); 
  358. } else { 
  359. $this->xdebug('import namespace ' . $attrs['namespace']); 
  360. $this->imports[$attrs['namespace']][] = array('location' => '', 'loaded' => true); 
  361. if (! $this->getPrefixFromNamespace($attrs['namespace'])) { 
  362. $this->namespaces['ns'.(count($this->namespaces)+1)] = $attrs['namespace']; 
  363. break; 
  364. case 'include': 
  365. if (isset($attrs['schemaLocation'])) { 
  366. $this->xdebug('include into namespace ' . $this->schemaTargetNamespace . ' from ' . $attrs['schemaLocation']); 
  367. $this->imports[$this->schemaTargetNamespace][] = array('location' => $attrs['schemaLocation'], 'loaded' => false); 
  368. } else { 
  369. $this->xdebug('ignoring invalid XML Schema construct: include without schemaLocation attribute'); 
  370. break; 
  371. case 'list': // simpleType value list 
  372. $this->xdebug("do nothing for element $name"); 
  373. break; 
  374. case 'restriction': // simpleType, simpleContent or complexContent value restriction 
  375. $this->xdebug('restriction ' . $attrs['base']); 
  376. if($this->currentSimpleType) { 
  377. $this->simpleTypes[$this->currentSimpleType]['type'] = $attrs['base']; 
  378. } elseif($this->currentComplexType) { 
  379. $this->complexTypes[$this->currentComplexType]['restrictionBase'] = $attrs['base']; 
  380. if(strstr($attrs['base'], ':') == ':Array') { 
  381. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array'; 
  382. break; 
  383. case 'schema': 
  384. $this->schemaInfo = $attrs; 
  385. $this->schemaInfo['schemaVersion'] = $this->getNamespaceFromPrefix($prefix); 
  386. if (isset($attrs['targetNamespace'])) { 
  387. $this->schemaTargetNamespace = $attrs['targetNamespace']; 
  388. if (!isset($attrs['elementFormDefault'])) { 
  389. $this->schemaInfo['elementFormDefault'] = 'unqualified'; 
  390. if (!isset($attrs['attributeFormDefault'])) { 
  391. $this->schemaInfo['attributeFormDefault'] = 'unqualified'; 
  392. break; 
  393. case 'simpleContent': // (optional) content for a complexType 
  394. if ($this->currentComplexType) { // This should *always* be 
  395. $this->complexTypes[$this->currentComplexType]['simpleContent'] = 'true'; 
  396. } else { 
  397. $this->xdebug("do nothing for element $name because there is no current complexType"); 
  398. break; 
  399. case 'simpleType': 
  400. array_push($this->simpleTypeStack, $this->currentSimpleType); 
  401. if(isset($attrs['name'])) { 
  402. $this->xdebug("processing simpleType for name " . $attrs['name']); 
  403. $this->currentSimpleType = $attrs['name']; 
  404. $this->simpleTypes[ $attrs['name'] ] = $attrs; 
  405. $this->simpleTypes[ $attrs['name'] ]['typeClass'] = 'simpleType'; 
  406. $this->simpleTypes[ $attrs['name'] ]['phpType'] = 'scalar'; 
  407. } else { 
  408. $name = $this->CreateTypeName($this->currentComplexType . '_' . $this->currentElement); 
  409. $this->xdebug('processing unnamed simpleType for element ' . $this->currentElement . ' named ' . $name); 
  410. $this->currentSimpleType = $name; 
  411. //$this->currentElement = false; 
  412. $this->simpleTypes[$this->currentSimpleType] = $attrs; 
  413. $this->simpleTypes[$this->currentSimpleType]['phpType'] = 'scalar'; 
  414. break; 
  415. case 'union': // simpleType type list 
  416. $this->xdebug("do nothing for element $name"); 
  417. break; 
  418. default: 
  419. $this->xdebug("do not have any logic to process element $name"); 
  420.  
  421. /** 
  422. * end-element handler 
  423. * @param string $parser XML parser object 
  424. * @param string $name element name 
  425. * @access private 
  426. */ 
  427. function schemaEndElement($parser, $name) { 
  428. // bring depth down a notch 
  429. $this->depth--; 
  430. // position of current element is equal to the last value left in depth_array for my depth 
  431. if(isset($this->depth_array[$this->depth])) { 
  432. $pos = $this->depth_array[$this->depth]; 
  433. // get element prefix 
  434. if ($prefix = $this->getPrefix($name)) { 
  435. // get unqualified name 
  436. $name = $this->getLocalPart($name); 
  437. } else { 
  438. $prefix = ''; 
  439. // move on... 
  440. if($name == 'complexType') { 
  441. $this->xdebug('done processing complexType ' . ($this->currentComplexType ? $this->currentComplexType : '(unknown)')); 
  442. $this->xdebug($this->varDump($this->complexTypes[$this->currentComplexType])); 
  443. $this->currentComplexType = array_pop($this->complexTypeStack); 
  444. //$this->currentElement = false; 
  445. if($name == 'element') { 
  446. $this->xdebug('done processing element ' . ($this->currentElement ? $this->currentElement : '(unknown)')); 
  447. $this->currentElement = array_pop($this->elementStack); 
  448. if($name == 'simpleType') { 
  449. $this->xdebug('done processing simpleType ' . ($this->currentSimpleType ? $this->currentSimpleType : '(unknown)')); 
  450. $this->xdebug($this->varDump($this->simpleTypes[$this->currentSimpleType])); 
  451. $this->currentSimpleType = array_pop($this->simpleTypeStack); 
  452.  
  453. /** 
  454. * element content handler 
  455. * @param string $parser XML parser object 
  456. * @param string $data element content 
  457. * @access private 
  458. */ 
  459. function schemaCharacterData($parser, $data) { 
  460. $pos = $this->depth_array[$this->depth - 1]; 
  461. $this->message[$pos]['cdata'] .= $data; 
  462.  
  463. /** 
  464. * serialize the schema 
  465. * @access public 
  466. */ 
  467. function serializeSchema() { 
  468.  
  469. $schemaPrefix = $this->getPrefixFromNamespace($this->XMLSchemaVersion); 
  470. $xml = ''; 
  471. // imports 
  472. if (sizeof($this->imports) > 0) { 
  473. foreach($this->imports as $ns => $list) { 
  474. foreach ($list as $ii) { 
  475. if ($ii['location'] != '') { 
  476. $xml .= " <$schemaPrefix:import location=\"" . $ii['location'] . '" namespace="' . $ns . "\" />\n"; 
  477. } else { 
  478. $xml .= " <$schemaPrefix:import namespace=\"" . $ns . "\" />\n"; 
  479. }  
  480. }  
  481. // complex types 
  482. foreach($this->complexTypes as $typeName => $attrs) { 
  483. $contentStr = ''; 
  484. // serialize child elements 
  485. if(isset($attrs['elements']) && (count($attrs['elements']) > 0)) { 
  486. foreach($attrs['elements'] as $element => $eParts) { 
  487. if(isset($eParts['ref'])) { 
  488. $contentStr .= " <$schemaPrefix:element ref=\"$element\"/>\n"; 
  489. } else { 
  490. $contentStr .= " <$schemaPrefix:element name=\"$element\" type=\"" . $this->contractQName($eParts['type']) . "\""; 
  491. foreach ($eParts as $aName => $aValue) { 
  492. // handle, e.g., abstract, default, form, minOccurs, maxOccurs, nillable 
  493. if ($aName != 'name' && $aName != 'type') { 
  494. $contentStr .= " $aName=\"$aValue\""; 
  495. $contentStr .= "/>\n"; 
  496. // compositor wraps elements 
  497. if (isset($attrs['compositor']) && ($attrs['compositor'] != '')) { 
  498. $contentStr = " <$schemaPrefix:$attrs[compositor]>\n".$contentStr." </$schemaPrefix:$attrs[compositor]>\n"; 
  499. // attributes 
  500. if(isset($attrs['attrs']) && (count($attrs['attrs']) >= 1)) { 
  501. foreach($attrs['attrs'] as $attr => $aParts) { 
  502. $contentStr .= " <$schemaPrefix:attribute"; 
  503. foreach ($aParts as $a => $v) { 
  504. if ($a == 'ref' || $a == 'type') { 
  505. $contentStr .= " $a=\"".$this->contractQName($v).'"'; 
  506. } elseif ($a == 'http://schemas.xmlsoap.org/wsdl/:arrayType') { 
  507. $this->usedNamespaces['wsdl'] = $this->namespaces['wsdl']; 
  508. $contentStr .= ' wsdl:arrayType="'.$this->contractQName($v).'"'; 
  509. } else { 
  510. $contentStr .= " $a=\"$v\""; 
  511. $contentStr .= "/>\n"; 
  512. // if restriction 
  513. if (isset($attrs['restrictionBase']) && $attrs['restrictionBase'] != '') { 
  514. $contentStr = " <$schemaPrefix:restriction base=\"".$this->contractQName($attrs['restrictionBase'])."\">\n".$contentStr." </$schemaPrefix:restriction>\n"; 
  515. // complex or simple content 
  516. if ((isset($attrs['elements']) && count($attrs['elements']) > 0) || (isset($attrs['attrs']) && count($attrs['attrs']) > 0)) { 
  517. $contentStr = " <$schemaPrefix:complexContent>\n".$contentStr." </$schemaPrefix:complexContent>\n"; 
  518. // finalize complex type 
  519. if($contentStr != '') { 
  520. $contentStr = " <$schemaPrefix:complexType name=\"$typeName\">\n".$contentStr." </$schemaPrefix:complexType>\n"; 
  521. } else { 
  522. $contentStr = " <$schemaPrefix:complexType name=\"$typeName\"/>\n"; 
  523. $xml .= $contentStr; 
  524. // simple types 
  525. if(isset($this->simpleTypes) && count($this->simpleTypes) > 0) { 
  526. foreach($this->simpleTypes as $typeName => $eParts) { 
  527. $xml .= " <$schemaPrefix:simpleType name=\"$typeName\">\n <$schemaPrefix:restriction base=\"".$this->contractQName($eParts['type'])."\">\n"; 
  528. if (isset($eParts['enumeration'])) { 
  529. foreach ($eParts['enumeration'] as $e) { 
  530. $xml .= " <$schemaPrefix:enumeration value=\"$e\"/>\n"; 
  531. $xml .= " </$schemaPrefix:restriction>\n </$schemaPrefix:simpleType>"; 
  532. // elements 
  533. if(isset($this->elements) && count($this->elements) > 0) { 
  534. foreach($this->elements as $element => $eParts) { 
  535. $xml .= " <$schemaPrefix:element name=\"$element\" type=\"".$this->contractQName($eParts['type'])."\"/>\n"; 
  536. // attributes 
  537. if(isset($this->attributes) && count($this->attributes) > 0) { 
  538. foreach($this->attributes as $attr => $aParts) { 
  539. $xml .= " <$schemaPrefix:attribute name=\"$attr\" type=\"".$this->contractQName($aParts['type'])."\"\n/>"; 
  540. // finish 'er up 
  541. $attr = ''; 
  542. foreach ($this->schemaInfo as $k => $v) { 
  543. if ($k == 'elementFormDefault' || $k == 'attributeFormDefault') { 
  544. $attr .= " $k=\"$v\""; 
  545. $el = "<$schemaPrefix:schema$attr targetNamespace=\"$this->schemaTargetNamespace\"\n"; 
  546. foreach (array_diff($this->usedNamespaces, $this->enclosingNamespaces) as $nsp => $ns) { 
  547. $el .= " xmlns:$nsp=\"$ns\""; 
  548. $xml = $el . ">\n".$xml."</$schemaPrefix:schema>\n"; 
  549. return $xml; 
  550.  
  551. /** 
  552. * adds debug data to the clas level debug string 
  553. * @param string $string debug data 
  554. * @access private 
  555. */ 
  556. function xdebug($string) { 
  557. $this->debug('<' . $this->schemaTargetNamespace . '> '.$string); 
  558.  
  559. /** 
  560. * get the PHP type of a user defined type in the schema 
  561. * PHP type is kind of a misnomer since it actually returns 'struct' for assoc. arrays 
  562. * returns false if no type exists, or not w/ the given namespace 
  563. * else returns a string that is either a native php type, or 'struct' 
  564. * @param string $type name of defined type 
  565. * @param string $ns namespace of type 
  566. * @return mixed 
  567. * @access public 
  568. * @deprecated 
  569. */ 
  570. function getPHPType($type, $ns) { 
  571. if(isset($this->typemap[$ns][$type])) { 
  572. //print "found type '$type' and ns $ns in typemap<br>"; 
  573. return $this->typemap[$ns][$type]; 
  574. } elseif(isset($this->complexTypes[$type])) { 
  575. //print "getting type '$type' and ns $ns from complexTypes array<br>"; 
  576. return $this->complexTypes[$type]['phpType']; 
  577. return false; 
  578.  
  579. /** 
  580. * returns an associative array of information about a given type 
  581. * returns false if no type exists by the given name 
  582. * For a complexType typeDef = array( 
  583. * 'restrictionBase' => '',  
  584. * 'phpType' => '',  
  585. * 'compositor' => '(sequence|all)',  
  586. * 'elements' => array(), // refs to elements array 
  587. * 'attrs' => array() // refs to attributes array 
  588. * ... and so on (see addComplexType) 
  589. * ) 
  590. * For simpleType or element, the array has different keys. 
  591. * @param string $type 
  592. * @return mixed 
  593. * @access public 
  594. * @see addComplexType 
  595. * @see addSimpleType 
  596. * @see addElement 
  597. */ 
  598. function getTypeDef($type) { 
  599. //$this->debug("in getTypeDef for type $type"); 
  600. if (substr($type, -1) == '^') { 
  601. $is_element = 1; 
  602. $type = substr($type, 0, -1); 
  603. } else { 
  604. $is_element = 0; 
  605.  
  606. if((! $is_element) && isset($this->complexTypes[$type])) { 
  607. $this->xdebug("in getTypeDef, found complexType $type"); 
  608. return $this->complexTypes[$type]; 
  609. } elseif((! $is_element) && isset($this->simpleTypes[$type])) { 
  610. $this->xdebug("in getTypeDef, found simpleType $type"); 
  611. if (!isset($this->simpleTypes[$type]['phpType'])) { 
  612. // get info for type to tack onto the simple type 
  613. // TODO: can this ever really apply (i.e. what is a simpleType really?) 
  614. $uqType = substr($this->simpleTypes[$type]['type'], strrpos($this->simpleTypes[$type]['type'], ':') + 1); 
  615. $ns = substr($this->simpleTypes[$type]['type'], 0, strrpos($this->simpleTypes[$type]['type'], ':')); 
  616. $etype = $this->getTypeDef($uqType); 
  617. if ($etype) { 
  618. $this->xdebug("in getTypeDef, found type for simpleType $type:"); 
  619. $this->xdebug($this->varDump($etype)); 
  620. if (isset($etype['phpType'])) { 
  621. $this->simpleTypes[$type]['phpType'] = $etype['phpType']; 
  622. if (isset($etype['elements'])) { 
  623. $this->simpleTypes[$type]['elements'] = $etype['elements']; 
  624. return $this->simpleTypes[$type]; 
  625. } elseif(isset($this->elements[$type])) { 
  626. $this->xdebug("in getTypeDef, found element $type"); 
  627. if (!isset($this->elements[$type]['phpType'])) { 
  628. // get info for type to tack onto the element 
  629. $uqType = substr($this->elements[$type]['type'], strrpos($this->elements[$type]['type'], ':') + 1); 
  630. $ns = substr($this->elements[$type]['type'], 0, strrpos($this->elements[$type]['type'], ':')); 
  631. $etype = $this->getTypeDef($uqType); 
  632. if ($etype) { 
  633. $this->xdebug("in getTypeDef, found type for element $type:"); 
  634. $this->xdebug($this->varDump($etype)); 
  635. if (isset($etype['phpType'])) { 
  636. $this->elements[$type]['phpType'] = $etype['phpType']; 
  637. if (isset($etype['elements'])) { 
  638. $this->elements[$type]['elements'] = $etype['elements']; 
  639. if (isset($etype['extensionBase'])) { 
  640. $this->elements[$type]['extensionBase'] = $etype['extensionBase']; 
  641. } elseif ($ns == 'http://www.w3.org/2001/XMLSchema') { 
  642. $this->xdebug("in getTypeDef, element $type is an XSD type"); 
  643. $this->elements[$type]['phpType'] = 'scalar'; 
  644. return $this->elements[$type]; 
  645. } elseif(isset($this->attributes[$type])) { 
  646. $this->xdebug("in getTypeDef, found attribute $type"); 
  647. return $this->attributes[$type]; 
  648. } elseif (preg_match('/_ContainedType$/', $type)) { 
  649. $this->xdebug("in getTypeDef, have an untyped element $type"); 
  650. $typeDef['typeClass'] = 'simpleType'; 
  651. $typeDef['phpType'] = 'scalar'; 
  652. $typeDef['type'] = 'http://www.w3.org/2001/XMLSchema:string'; 
  653. return $typeDef; 
  654. $this->xdebug("in getTypeDef, did not find $type"); 
  655. return false; 
  656.  
  657. /** 
  658. * returns a sample serialization of a given type, or false if no type by the given name 
  659. * @param string $type name of type 
  660. * @return mixed 
  661. * @access public 
  662. * @deprecated 
  663. */ 
  664. function serializeTypeDef($type) { 
  665. //print "in sTD() for type $type<br>"; 
  666. if($typeDef = $this->getTypeDef($type)) { 
  667. $str .= '<'.$type; 
  668. if(is_array($typeDef['attrs'])) { 
  669. foreach($typeDef['attrs'] as $attName => $data) { 
  670. $str .= " $attName=\"{type = ".$data['type']."}\""; 
  671. $str .= " xmlns=\"".$this->schema['targetNamespace']."\""; 
  672. if(count($typeDef['elements']) > 0) { 
  673. $str .= ">"; 
  674. foreach($typeDef['elements'] as $element => $eData) { 
  675. $str .= $this->serializeTypeDef($element); 
  676. $str .= "</$type>"; 
  677. } elseif($typeDef['typeClass'] == 'element') { 
  678. $str .= "></$type>"; 
  679. } else { 
  680. $str .= "/>"; 
  681. return $str; 
  682. return false; 
  683.  
  684. /** 
  685. * returns HTML form elements that allow a user 
  686. * to enter values for creating an instance of the given type. 
  687. * @param string $name name for type instance 
  688. * @param string $type name of type 
  689. * @return string 
  690. * @access public 
  691. * @deprecated 
  692. */ 
  693. function typeToForm($name, $type) { 
  694. // get typedef 
  695. if($typeDef = $this->getTypeDef($type)) { 
  696. // if struct 
  697. if($typeDef['phpType'] == 'struct') { 
  698. $buffer .= '<table>'; 
  699. foreach($typeDef['elements'] as $child => $childDef) { 
  700. $buffer .= " 
  701. <tr><td align='right'>$childDef[name] (type: ".$this->getLocalPart($childDef['type'])."):</td> 
  702. <td><input type='text' name='parameters[".$name."][$childDef[name]]'></td></tr>"; 
  703. $buffer .= '</table>'; 
  704. // if array 
  705. } elseif($typeDef['phpType'] == 'array') { 
  706. $buffer .= '<table>'; 
  707. for($i=0;$i < 3; $i++) { 
  708. $buffer .= " 
  709. <tr><td align='right'>array item (type: $typeDef[arrayType]):</td> 
  710. <td><input type='text' name='parameters[".$name."][]'></td></tr>"; 
  711. $buffer .= '</table>'; 
  712. // if scalar 
  713. } else { 
  714. $buffer .= "<input type='text' name='parameters[$name]'>"; 
  715. } else { 
  716. $buffer .= "<input type='text' name='parameters[$name]'>"; 
  717. return $buffer; 
  718.  
  719. /** 
  720. * adds a complex type to the schema 
  721. *  
  722. * example: array 
  723. *  
  724. * addType( 
  725. * 'ArrayOfstring',  
  726. * 'complexType',  
  727. * 'array',  
  728. * '',  
  729. * 'SOAP-ENC:Array',  
  730. * array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType'=>'string[]'),  
  731. * 'xsd:string' 
  732. * ); 
  733. *  
  734. * example: PHP associative array ( SOAP Struct ) 
  735. *  
  736. * addType( 
  737. * 'SOAPStruct',  
  738. * 'complexType',  
  739. * 'struct',  
  740. * 'all',  
  741. * array('myVar'=> array('name'=>'myVar', 'type'=>'string') 
  742. * ); 
  743. *  
  744. * @param name 
  745. * @param typeClass (complexType|simpleType|attribute) 
  746. * @param phpType: currently supported are array and struct (php assoc array) 
  747. * @param compositor (all|sequence|choice) 
  748. * @param restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array) 
  749. * @param elements = array ( name = array(name=>'', type=>'') ) 
  750. * @param attrs = array( 
  751. * array( 
  752. * 'ref' => "http://schemas.xmlsoap.org/soap/encoding/:arrayType",  
  753. * "http://schemas.xmlsoap.org/wsdl/:arrayType" => "string[]" 
  754. * ) 
  755. * ) 
  756. * @param arrayType: namespace:name (http://www.w3.org/2001/XMLSchema:string) 
  757. * @access public 
  758. * @see getTypeDef 
  759. */ 
  760. function addComplexType($name, $typeClass='complexType', $phpType='array', $compositor='', $restrictionBase='', $elements=array(), $attrs=array(), $arrayType='') { 
  761. $this->complexTypes[$name] = array( 
  762. 'name' => $name,  
  763. 'typeClass' => $typeClass,  
  764. 'phpType' => $phpType,  
  765. 'compositor'=> $compositor,  
  766. 'restrictionBase' => $restrictionBase,  
  767. 'elements' => $elements,  
  768. 'attrs' => $attrs,  
  769. 'arrayType' => $arrayType 
  770. ); 
  771.  
  772. $this->xdebug("addComplexType $name:"); 
  773. $this->appendDebug($this->varDump($this->complexTypes[$name])); 
  774.  
  775. /** 
  776. * adds a simple type to the schema 
  777. * @param string $name 
  778. * @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array) 
  779. * @param string $typeClass (should always be simpleType) 
  780. * @param string $phpType (should always be scalar) 
  781. * @param array $enumeration array of values 
  782. * @access public 
  783. * @see nusoap_xmlschema 
  784. * @see getTypeDef 
  785. */ 
  786. function addSimpleType($name, $restrictionBase='', $typeClass='simpleType', $phpType='scalar', $enumeration=array()) { 
  787. $this->simpleTypes[$name] = array( 
  788. 'name' => $name,  
  789. 'typeClass' => $typeClass,  
  790. 'phpType' => $phpType,  
  791. 'type' => $restrictionBase,  
  792. 'enumeration' => $enumeration 
  793. ); 
  794.  
  795. $this->xdebug("addSimpleType $name:"); 
  796. $this->appendDebug($this->varDump($this->simpleTypes[$name])); 
  797.  
  798. /** 
  799. * adds an element to the schema 
  800. * @param array $attrs attributes that must include name and type 
  801. * @see nusoap_xmlschema 
  802. * @access public 
  803. */ 
  804. function addElement($attrs) { 
  805. if (! $this->getPrefix($attrs['type'])) { 
  806. $attrs['type'] = $this->schemaTargetNamespace . ':' . $attrs['type']; 
  807. $this->elements[ $attrs['name'] ] = $attrs; 
  808. $this->elements[ $attrs['name'] ]['typeClass'] = 'element'; 
  809.  
  810. $this->xdebug("addElement " . $attrs['name']); 
  811. $this->appendDebug($this->varDump($this->elements[ $attrs['name'] ]));