XML.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. Lib::loadClass('Core_XmlWriter');
  3. // $zxo = new XMLDiff\Memory;// @require xmldiff pecl package installed
  4. // TODO:TYPE_RECURCE: if type has local prefix - choose:
  5. // - find recurse typeName and restrictions
  6. // - find types and save in SystemObjectField table (if simpleType)
  7. // - ? find types and save in SystemObject table + SystemObjectField table (if complexType)
  8. class XML {
  9. public static function xmlToArray($xml) {
  10. }
  11. public static function readXmlFileToArray($filePath) {
  12. $z = new XMLReader();
  13. if (!$z) throw new HttpException("Class XMLReader not installed", 500);
  14. $fileName = basename($filePath);
  15. if (!$z->open($filePath)) throw new Exception("Failed to open ant schema file '{$fileName}'", 500);
  16. return self::xmlReadToArray($z);
  17. }
  18. public static function xmlReadToArray($z) {
  19. while ($z->read()) {
  20. if (XMLReader::ELEMENT !== $z->nodeType) continue;
  21. return self::xmlReadRecurse($z);
  22. }
  23. }
  24. public static function xmlReadRecurse($z) {
  25. $node = [ $z->name, [], [] ];// name, attrs, childrens
  26. $depth = $z->depth;
  27. $isEmpty = $z->isEmptyElement;
  28. if ($z->hasAttributes) {
  29. while ($z->moveToNextAttribute()) {
  30. $node[1][$z->name] = $z->value;
  31. }
  32. }
  33. if ($isEmpty) {
  34. $node[2] = null;
  35. return $node;
  36. }
  37. // switch ($z->name) {
  38. // case 'xsd:complexType': $node[1]['name'] = $z->getAttribute('name'); break;
  39. // case 'xsd:simpleType': $node[1]['name'] = $z->getAttribute('name'); break;
  40. // case 'xsd:restriction': $node[1]['base'] = $z->getAttribute('base'); break;
  41. // case 'xsd:enumeration': $node[1]['value'] = $z->getAttribute('value'); break;
  42. // case 'xsd:element': $node[1] = [
  43. // 'type' => $z->getAttribute('type')
  44. // ]; break;
  45. // case 'xsd:complexContent': break;
  46. // case 'xsd:sequence': break;
  47. // case 'xsd:attribute': $node[1]['base'] = $z->getAttribute('base'); break;
  48. // case 'xsd:extension': $node[1]['base'] = $z->getAttribute('base'); break;
  49. // default: UI::alert('warning', "TODO: read attributes from: d({$z->depth}) name($z->name)");
  50. // }
  51. while ($z->read() && $z->depth > $depth) {
  52. if (XMLReader::ELEMENT !== $z->nodeType) continue;
  53. // if ($z->depth == $depth + 2)
  54. $node[2][] = self::xmlReadRecurse($z);
  55. // else $node[2][] = "d({$z->depth}) name($z->name)";
  56. }
  57. return $node;
  58. }
  59. public static function printXmlFromArray($xml) {
  60. $xmlWriter = new Core_XmlWriter();
  61. $xmlWriter->openUri('php://output');
  62. $xmlWriter->setIndent(true);
  63. $xmlWriter->startDocument('1.0','UTF-8');
  64. $xmlWriter->h($xml[0], $xml[1], $xml[2]);
  65. $xmlWriter->endDocument();
  66. }
  67. public static function findTargetNamespace($docArray) {
  68. return V::get('targetNamespace', '', $docArray[1]);
  69. }
  70. public static function findSimpleTypeNode($docArray, $name) {
  71. foreach ($docArray[2] as $child) {
  72. if ('simpleType' === self::getTagName($child[0])) {
  73. if ($name === $child[1]['name']) {
  74. return $child;
  75. }
  76. }
  77. }
  78. }
  79. public static function findTargetNamespacePrefix($docArray) {
  80. $tns = self::findTargetNamespace($docArray);
  81. if (!$tns) throw new Exception("targetNamespace not defined");
  82. foreach ($docArray[1] as $attr => $val) {
  83. if ('xmlns:' !== substr($attr, 0, 6)) continue;
  84. if ($tns === $val) return substr($attr, 6);
  85. }
  86. throw new Exception("Missing targetNamespace xmlns:...");
  87. }
  88. public static function findElementName($docArray, $nodeArray) {
  89. if (!empty($nodeArray[1]['name'])) return $nodeArray[1]['name'];
  90. if (!empty($nodeArray[1]['ref'])) return $nodeArray[1]['ref'];
  91. throw new Exception("Missing xsd:element name");
  92. }
  93. public static function findElementType($docArray, $nodeArray) {
  94. $fieldName = self::findElementName($docArray, $nodeArray);
  95. if (!empty($nodeArray[1]['type'])) {
  96. // TODO:TYPE_RECURCE: if local ns prefix then find correct typeName?
  97. // TODO:TYPE_RECURCE:the same for restrictions
  98. $type = $nodeArray[1]['type'];
  99. DBG::log(['is_xs:' => ('xs:' === substr($type, 0, 3)), 'is_xsd:'=>('xsd:' === substr($type, 0, 4))], 'array', "DBG: findElementType field({$fieldName}) type({$type})");
  100. if ($fixedType = self::tryConvertXsdTypeToXsdPrefix($type)) {
  101. return $fixedType;
  102. }
  103. list($prefix, $name) = explode(':', $type);
  104. if ($prefix === self::findTargetNamespacePrefix($docArray)) {
  105. $simpleTypeNode = self::findSimpleTypeNode($docArray, $name);
  106. DBG::log($simpleTypeNode, 'array', "\$simpleTypeNode \$fieldName='{$fieldName}' type='{$type}'");
  107. if (!empty($simpleTypeNode[1]['type'])) {
  108. throw new Exception("TODO: findElementType node/@type => 'xsd:simpleType/@type' = '{$simpleTypeNode[1]['type']}'");
  109. }
  110. if (!empty($simpleTypeNode[2][0]) && self::isXsdTag($simpleTypeNode[2][0][0], 'restriction')) {
  111. $restrictionNode = $simpleTypeNode[2][0];
  112. if (empty($restrictionNode[1]['base'])) throw new Exception("Missing xsd:restriction/@base (node/@type => xsd:simpleType/[@type='{$simpleTypeNode[1]['type']}']/xsd:restriction')");
  113. $type = $restrictionNode[1]['base'];
  114. DBG::log($type, 'array', "findElementType \$fieldName='{$fieldName}' type='{$nodeArray[1]['type']}' => type='{$type}'");
  115. // check restrictions - if has enumeration then return 'p5:enum'
  116. $isEnum = false;
  117. if (!empty($restrictionNode[2])) foreach ($restrictionNode[2] as $restr) {
  118. if ('enumeration' === self::getTagName($restr[0])) {
  119. $isEnum = true;
  120. break;
  121. }
  122. }
  123. if ($isEnum) return 'p5:enum';
  124. // TODO: recurse with limit
  125. if ($fixedType = self::tryConvertXsdTypeToXsdPrefix($type)) {
  126. return $fixedType;
  127. }
  128. list($prefix, $name) = explode(':', $type);
  129. $simpleTypeNode = self::findSimpleTypeNode($docArray, $name);
  130. DBG::log($simpleTypeNode, 'array', "\$simpleTypeNode \$fieldName='{$fieldName}' ... type='{$type}'");
  131. if (!empty($simpleTypeNode[1]['type'])) {
  132. throw new Exception("TODO: findElementType node/@type => 'xsd:simpleType/@type' = '{$simpleTypeNode[1]['type']}'");
  133. }
  134. if (!empty($simpleTypeNode[2][0]) && self::isXsdTag($simpleTypeNode[2][0][0], 'restriction')) {
  135. $restrictionNode = $simpleTypeNode[2][0];
  136. if (empty($restrictionNode[1]['base'])) throw new Exception("Missing xsd:restriction/@base (node/@type => xsd:simpleType/[@type='{$simpleTypeNode[1]['type']}']/xsd:restriction')");
  137. $type = $restrictionNode[1]['base'];
  138. DBG::log($type, 'array', "findElementType \$fieldName='{$fieldName}' ... type='{$type}'");
  139. return self::convertXsdTypeToXsdPrefix($type);
  140. }
  141. // TODO: throw...
  142. }
  143. // TODO: throw...
  144. // 0 => 'xsd:simpleType',
  145. // 1 => [
  146. // 'name' => 'PROCES_INIT_Simple',
  147. // ],
  148. // 2 => [
  149. // 0 => [
  150. // 0 => 'xsd:restriction',
  151. // 1 => [
  152. // 'base' => 'default_db__x3A__CRM_PROCES:TYPE_Simple',
  153. // ],
  154. // 2 => [
  155. // 0 => [
  156. // 0 => 'xsd:enumeration',
  157. // 1 => [
  158. // 'value' => 'PROCES_INIT',
  159. // ],
  160. // 2 => NULL,
  161. } else {
  162. return $nodeArray[1]['type'];
  163. }
  164. }
  165. if (!empty($nodeArray[1]['ref'])) return 'ref:' . $nodeArray[1]['ref'];
  166. if (empty($nodeArray[2])) throw new Exception("Missing xsd:element childrens - cannot find type");
  167. // TODO: find in loop (xsd:annotation may accure)
  168. if (empty($nodeArray[2][0][0]) || !self::isXsdTag($nodeArray[2][0][0], 'simpleType')) throw new Exception("Missing 'xsd:simpleType' for field '{$fieldName}'");
  169. if (empty($nodeArray[2][0][2][0]) || !self::isXsdTag($nodeArray[2][0][2][0][0], 'restriction')) throw new Exception("Missing 'xsd:restriction' for field '{$fieldName}'");
  170. if (empty($nodeArray[2][0][2][0][1]['base'])) throw new Exception("Missing 'xsd:restriction/@base' for field '{$fieldName}'");
  171. $type = $nodeArray[2][0][2][0][1]['base'];
  172. if ($fixedType = self::tryConvertXsdTypeToXsdPrefix($type)) {
  173. return $fixedType;
  174. }
  175. DBG::log($nodeArray[2][0], 'array', "TODO:findElementType: local type with restriction field({$fieldName}) type({$type})");
  176. // TODO: element local type with restriction
  177. // <xsd:element name="TYPE">
  178. // <xsd:simpleType>
  179. // <xsd:restriction base="default_db__x3A__CRM_PROCES:TYPE_Simple">
  180. // <xsd:enumeration value="PROCES_BENEFIT_INFO"/>
  181. // </xsd:restriction>
  182. // </xsd:simpleType>
  183. // </xsd:element>
  184. return $type;
  185. }
  186. public static function tryConvertXsdTypeToXsdPrefix($type) {
  187. try {
  188. $type = self::convertXsdTypeToXsdPrefix($type);
  189. return $type;
  190. } catch (Exception $e) {
  191. DBG::log($e);
  192. }
  193. return false;
  194. }
  195. public static function convertXsdTypeToXsdPrefix($type) {
  196. // TODO: validate if type is supported in object engine and gui
  197. // TODO: prefix p5
  198. list($prefix, $name) = explode(':', $type);
  199. $prefix = ('xs' === $prefix) ? 'xsd' : $prefix;
  200. if ('xsd' === $prefix && 'int' === $name) return 'xsd:integer';
  201. if ('xsd' === $prefix && 'NCName' === $name) return 'xsd:string'; // TODO: restriction?
  202. if ('xsd' === $prefix && 'NMTOKEN' === $name) return 'xsd:string'; // TODO: restriction?
  203. if ('xsd' === $prefix) return implode(":", [ $prefix, $name ]);
  204. if ('p5' === $prefix) return implode(":", [ $prefix, $name ]);
  205. // if ('xs:' === substr($type, 0, 3)) return "xsd:" . substr($type, 3);
  206. // if ('xsd:' === substr($type, 0, 4)) return $type;
  207. throw new Exception("Not implemented type '{$type}'");
  208. }
  209. public static function findElementRestrictions($docArray, $nodeArray) {
  210. $restrictions = [];
  211. $fieldName = self::findElementName($docArray, $nodeArray);
  212. $childNodes = !empty($nodeArray[2]) ? $nodeArray[2] : null; // definition in child nodes
  213. $typeAlias = !empty($nodeArray[1]['type']) ? $nodeArray[1]['type'] : null; // definition alias - search simpleType with name = Type
  214. DBG::log([
  215. 'has $childNodes' => !empty($childNodes),
  216. 'has $typeAlias' => !empty($typeAlias),
  217. '$typeAlias' => $typeAlias,
  218. '$nodeArray' => $nodeArray
  219. ], 'array', "DBG: findElementRestrictions field({$fieldName})");
  220. if (!empty($nodeArray[1]['nillable']) && 'true' === $nodeArray[1]['nillable']) $restrictions['nillable'] = true;
  221. if ($typeAlias) {
  222. list($prefix, $name) = explode(':', $typeAlias);
  223. if ($prefix === XML::findTargetNamespacePrefix($docArray)) {
  224. $simpleTypeNode = self::findSimpleTypeNode($docArray, $name);
  225. return XML::parseSimpleTypeRestrictions($docArray, $simpleTypeNode, $fieldName);
  226. }
  227. }
  228. if ($childNodes) {
  229. foreach ($childNodes as $c) {
  230. switch (XML::getTagName($c[0])) {
  231. case 'annotation': break; // skip xsd:element/xsd:annotation @see findElementAppInfo
  232. case 'simpleType': $restrictions = XML::parseSimpleTypeRestrictions($docArray, $c, $fieldName);
  233. default: {
  234. DBG::log($c, 'array', "Not imeplemented element child '{$c[0]}'");
  235. }
  236. }
  237. }
  238. }
  239. return $restrictions;
  240. }
  241. public static function parseSimpleTypeRestrictions($docArray, $simpleTypeNode, $fieldName) { // $simpleTypeNode must be 'simpleType' === XML::getTagName($nodeArray[0]), expected 'xsd:restriction' child
  242. DBG::log($simpleTypeNode, 'array', "DBG: parseSimpleTypeRestrictions \$simpleTypeNode for field '{$fieldName}'");
  243. if (empty($simpleTypeNode[2])) return []; // Missing childrens in simpleType definition
  244. $restrictionNode = null;
  245. foreach ($simpleTypeNode[2] as $c) {
  246. if ('restriction' == XML::getTagName($c[0])) {
  247. $restrictionNode = $c;
  248. break;
  249. }
  250. }
  251. DBG::log($restrictionNode, 'array', "DBG: parseSimpleTypeRestrictions \$restrictionNode for field '{$fieldName}'");
  252. if (empty($restrictionNode)) return []; // Missing xsd:restriction in simpleType definition
  253. $restrictions = [];
  254. if (empty($restrictionNode[1]['base'])) throw new Exception("Missing 'xsd:restriction/@base' for field '{$fieldName}'");
  255. // xsd:simpleType/xsd:restriction/xsd:string
  256. if (!empty($restrictionNode[2])) foreach ($restrictionNode[2] as $tagRestriction) {
  257. // xsd:string/xsd:maxLength
  258. $val = $tagRestriction[1]['value'];
  259. if ('enumeration' == XML::getTagName($tagRestriction[0])) {
  260. $restrictions['enumeration'][$val] = $val;
  261. } else {
  262. $restrictions[ XML::getTagName($tagRestriction[0]) ] = $val;
  263. }
  264. }
  265. return $restrictions;
  266. }
  267. public static function findElementAppInfo($docArray, $nodeArray) {
  268. $appInfo = [];
  269. $fieldName = self::findElementName($docArray, $nodeArray);
  270. if (!empty($nodeArray[2])) {
  271. foreach ($nodeArray[2] as $c) {
  272. switch (XML::getTagName($c[0])) {
  273. case 'annotation': { // skip xsd:element/xsd:annotation
  274. DBG::log($c, 'array', "xsd:annotation/xsd:appinfo '{$fieldName}'");
  275. // <xsd:annotation>
  276. // <xsd:appinfo>
  277. // <system_cache__appinfo:flat_relation_cache>
  278. // <system_cache__appinfo:source system_cache__appinfo:name="ID"
  279. // system_cache__appinfo:xpath="default_db__x3A__CRM_WSKAZNIK:CRM_WSKAZNIK/ID_PROCES"/>
  280. $prefix = 'system_cache__appinfo';
  281. foreach ($c[2] as $cc) {
  282. if ('appinfo' == XML::getTagName($cc[0])) {
  283. foreach ($cc[2] as $appTag) {
  284. $appInfo[ XML::getTagName($appTag[0]) ] = XML::readAppInfoRecurse($appTag);
  285. }
  286. }
  287. }
  288. DBG::log($appInfo, 'array', "xsd:annotation/xsd:appinfo '{$fieldName}' \$appInfo");
  289. // <xs:annotation>
  290. // <xs:appinfo>
  291. // <system_cache__appinfo:flat_relation_cache system_cache__appinfo:backref_evaluate="true">
  292. // <system_cache__appinfo:source system_cache__appinfo:name="krs" system_cache__appinfo:xpath="default_db__x3A__BI_audit_KRS:BI_audit_KRS/krs" system_cache__appinfo:ref_engine="view"/>
  293. } break;
  294. case 'simpleType': break; // skip xsd:element/xsd:simpleType @see findElementRestrictions
  295. default: {
  296. DBG::log($c, 'array', "Not imeplemented element child '{$c[0]}'");
  297. }
  298. }
  299. }
  300. }
  301. DBG::log($fieldName, 'array', "findElementAppInfo fieldName='{$fieldName}'");
  302. return $appInfo;
  303. }
  304. public static function getTagName($xsdName) {
  305. return (false !== ($pos = strpos($xsdName, ':')))
  306. ? substr($xsdName, $pos + 1)
  307. : $xsdName;
  308. }
  309. public static function isXsdTag($xsdName, $expectedTagName) {
  310. list($xsdPrefix, $tagName) = explode(':', $xsdName);
  311. switch ($xsdPrefix) {
  312. case 'xs':
  313. case 'xsd': return ($tagName === $expectedTagName);
  314. }
  315. return false;
  316. }
  317. static function isXsdNodeAnnotation($xsdNode) { return self::isXsdTag($xsdNode[0], 'annotation'); }
  318. static function isXsdNodeAppInfo($xsdNode) { return self::isXsdTag($xsdNode[0], 'appinfo'); }
  319. public static function readAppInfoRecurse($nodeArray) {
  320. $appInfo = [];
  321. if (!empty($nodeArray[1])) foreach ($nodeArray[1] as $attrName => $attrVal) {
  322. $appInfo['@' . XML::getTagName($attrName)] = $attrVal;
  323. }
  324. if (!empty($nodeArray[2])) foreach ($nodeArray[2] as $appTag) {
  325. $appInfo[ XML::getTagName($appTag[0]) ] = XML::readAppInfoRecurse($appTag);
  326. }
  327. // TODO: text nodes
  328. return $appInfo;
  329. }
  330. public static function findFieldsFromSequence($docArray, $nodeArray) {
  331. if (!self::isXsdTag($nodeArray[0], 'sequence')) throw new Exception("Error Parsing Schema - expected 'sequence'");
  332. $fields = [];
  333. foreach ($nodeArray[2] as $f) {
  334. if (!self::isXsdTag($f[0], 'element')) {
  335. DBG::log($n, 'array', "Schema xsd parse error - Not implemented node type '{$f[0]}'");
  336. continue;
  337. }
  338. $fieldName = XML::findElementName($docArray, $f); // V::get('name', '', $f[1]);
  339. if (!$fieldName) throw new Exception("Error Parsing Schema - expected 'element[@name]'");
  340. if ('__' === substr($fieldName, 0, 2)) continue;
  341. if (!V::get('type', '', $f[1]) && !V::get('ref', '', $f[1]) && empty($f[2])) {
  342. UI::alert('danger', "Skipping not implemented field structure '{$fieldName}'");
  343. DBG::log($f, 'array', "Skipping not implemented field structure '{$fieldName}'");
  344. continue;
  345. }
  346. $fields[$fieldName] = [
  347. 'type' => XML::findElementType($docArray, $f),
  348. 'minOccurs' => V::get('minOccurs', 0, $f[1], 'int'),
  349. 'maxOccurs' => V::get('maxOccurs', '1', $f[1]),
  350. 'restrictions' => XML::findElementRestrictions($docArray, $f),
  351. 'appInfo' => XML::findElementAppInfo($docArray, $f),
  352. ];
  353. }
  354. return $fields;
  355. }
  356. public static function findFieldsFromComplexContent($docArray, $nodeArray) {
  357. // xsd:complexType / xsd:complexContent / xsd:restriction [ @base = "default_db__x3A__CRM_PROCES:CRM_PROCES" ]
  358. // xsd:complexType / xsd:complexContent / xsd:extension [ @base = "default_db__x3A__CRM_PROCES:CRM_PROCES" ]
  359. switch (XML::getTagName($nodeArray[2][0][0])) {
  360. case 'extension': return XML::findFieldsFromExtension($docArray, $nodeArray[2][0]);
  361. case 'restriction': return XML::findFieldsFromRestriction($docArray, $nodeArray[2][0]);
  362. }
  363. // TODO:? $xsdType['extensionBase'] = V::get('base', '', $nodeArray[1]);
  364. // TODO:? $xsdType['restrictionBase'] = V::get('base', '', $nodeArray[1]);
  365. }
  366. public static function findFieldsFromExtension($docArray, $nodeArray) {
  367. $fields = [];
  368. if (!self::isXsdTag($nodeArray[2][0][0], 'sequence')) throw new Exception("Error Parsing Schema - expected 'complexType/complexContent/extension/sequence'");
  369. foreach ($nodeArray[2][0][2] as $f) {
  370. if (!self::isXsdTag($f[0], 'element')) {
  371. DBG::log($n, 'array', "Schema xsd parse error - Not implemented node type '{$f[0]}'");
  372. continue;
  373. }
  374. $fieldName = XML::findElementName($docArray, $f); // V::get('name', '', $f[1]);
  375. if (!$fieldName) throw new Exception("Error Parsing Schema - expected 'element[@name]'");
  376. if ('__' === substr($fieldName, 0, 2)) continue;
  377. if (!V::get('type', '', $f[1]) && !V::get('ref', '', $f[1]) && empty($f[2])) {
  378. UI::alert('danger', "Skipping not implemented field structure '{$fieldName}'");
  379. continue;
  380. }
  381. $fields[$fieldName] = [
  382. 'type' => XML::findElementType($docArray, $f),
  383. 'minOccurs' => V::get('minOccurs', 0, $f[1], 'int'),
  384. 'maxOccurs' => V::get('maxOccurs', '1', $f[1]),
  385. 'restrictions' => XML::findElementRestrictions($docArray, $f),
  386. 'appInfo' => XML::findElementAppInfo($docArray, $f),
  387. ];
  388. }
  389. return $fields;
  390. }
  391. public static function findFieldsFromRestriction($docArray, $nodeArray) {
  392. $fields = [];
  393. if (!self::isXsdTag($nodeArray[2][0][0], 'sequence')) throw new Exception("Error Parsing Schema - expected 'complexType/complexContent/restriction/sequence'");
  394. foreach ($nodeArray[2][0][2] as $f) {
  395. if (!self::isXsdTag($f[0], 'element')) {
  396. DBG::log($n, 'array', "Schema xsd parse error - Not implemented node type '{$f[0]}'");
  397. continue;
  398. }
  399. $fieldName = XML::findElementName($docArray, $f); // V::get('name', '', $f[1]);
  400. if (!$fieldName) throw new Exception("Error Parsing Schema - expected 'element[@name]'");
  401. if ('__' === substr($fieldName, 0, 2)) continue;
  402. if (!V::get('type', '', $f[1]) && !V::get('ref', '', $f[1]) && empty($f[2])) {
  403. UI::alert('danger', "Skipping not implemented field structure '{$fieldName}'");
  404. continue;
  405. }
  406. $fields[$fieldName] = [
  407. 'type' => XML::findElementType($docArray, $f),
  408. 'minOccurs' => V::get('minOccurs', 0, $f[1], 'int'),
  409. 'maxOccurs' => V::get('maxOccurs', '1', $f[1]),
  410. 'restrictions' => XML::findElementRestrictions($docArray, $f),
  411. 'appInfo' => XML::findElementAppInfo($docArray, $f),
  412. ];
  413. }
  414. return $fields;
  415. }
  416. public static function getXsdTypeFromXsdSchema($xsdFilePath, $namespace, $name) {
  417. $schema = XML::readXmlFileToArray($xsdFilePath);
  418. if (empty($schema)) throw new Exception("Missing schema file for '{$namespace}'");
  419. $xsdType = [ // find xsd:element with @name = $name
  420. 'nsPrefix' => null,
  421. 'name' => null,
  422. 'nsUri' => null,
  423. 'primaryKey' => null,
  424. 'targetNsUri' => V::get('targetNamespace', '', $schema[1])
  425. ];
  426. if (!$xsdType['targetNsUri']) throw new Exception("Missing schema target namespace declaration '{$name}'");
  427. foreach ($schema[2] as $n) {
  428. if (!XML::isXsdTag($n[0], 'element')) continue;
  429. if ($name != V::get('name', '', $n[1])) continue;
  430. list($xsdType['nsPrefix'], $xsdType['name']) = explode(':', V::get('type', '', $n[1]));
  431. }
  432. if (!$xsdType['nsPrefix'] || !$xsdType['name']) throw new Exception("Missing schema root element name = '{$name}'");
  433. $xsdType['nsUri'] = V::get("xmlns:{$xsdType['nsPrefix']}", '', $schema[1]);// find xmlns:default_objects = "https://biuro.biall-net.pl/wfs/default_objects"
  434. if (!$xsdType['nsUri']) throw new Exception("Missing schema root element namespace declaration = '{$name}'");
  435. if ($xsdType['nsUri'] != $xsdType['targetNsUri']) throw new Exception("TODO: type ns is not the same as targetNamespace '{$name}'");// TODO
  436. $foundComplexTypes = array_filter($schema[2], function ($n) use ($xsdType) {
  437. return (XML::isXsdTag($n[0], 'complexType') && $xsdType['name'] === V::get('name', '', $n[1]));
  438. });
  439. if (empty($foundComplexTypes)) throw new Exception("Missing complexType node with name='{$xsdType['name']}'");
  440. $nodeComplexType = reset($foundComplexTypes);
  441. DBG::log($nodeComplexType, 'array', "found main complexType node");
  442. if (array_key_exists('system_cache__appinfo:primaryKey', $nodeComplexType[1])) {
  443. $xsdType['primaryKey'] = V::get('system_cache__appinfo:primaryKey', '', $nodeComplexType[1]);
  444. }
  445. $listAnnotations = array_filter($nodeComplexType[2], [ self, 'isXsdNodeAnnotation' ]);
  446. if (!empty($listAnnotations)) {
  447. // DBG::nicePrint($listAnnotations, "\$listAnnotations");
  448. $nodeAnnotation = reset($listAnnotations);
  449. $listAppInfo = array_filter($nodeAnnotation[2], [ self, 'isXsdNodeAppInfo' ]);
  450. if (!empty($listAppInfo)) {
  451. // DBG::nicePrint($listAppInfo, "\$listAppInfo");
  452. $nodeAppInfo = reset($listAppInfo);
  453. $xsdType['appInfo'] = XML::readAppInfoRecurse($nodeAppInfo);
  454. }
  455. }
  456. // complexType/sequence/element
  457. // complexType/complexContent/extension[base=...]/sequence/element
  458. switch ($nodeComplexType[2][0][0]) { // TODO: convert to loop
  459. case 'xs:sequence':
  460. case 'xsd:sequence': $xsdType['struct'] = XML::findFieldsFromSequence($schema, $nodeComplexType[2][0]); break;
  461. case 'xs:complexContent':
  462. case 'xsd:complexContent': $xsdType['struct'] = XML::findFieldsFromComplexContent($schema, $nodeComplexType[2][0]); break;
  463. case 'xs:annotation':
  464. case 'xsd:annotation': {
  465. switch ($nodeComplexType[2][1][0]) {
  466. case 'xs:sequence':
  467. case 'xsd:sequence': $xsdType['struct'] = XML::findFieldsFromSequence($schema, $nodeComplexType[2][1]); break;
  468. case 'xs:complexContent':
  469. case 'xsd:complexContent': $xsdType['struct'] = XML::findFieldsFromComplexContent($schema, $nodeComplexType[2][1]); break;
  470. }
  471. } break;
  472. }
  473. // if ($n[2][0][0] != 'xsd:complexContent') throw new Exception("Error Parsing Schema - expected 'complexType/complexContent'");
  474. if (empty($xsdType['primaryKey'])) {
  475. foreach ($xsdType['struct'] as $fieldName => $field) {
  476. if ('ID' === strtoupper($fieldName)) {
  477. $xsdType['primaryKey'] = $fieldName;
  478. break;
  479. }
  480. }
  481. }
  482. if (empty($xsdType['primaryKey'])) throw new Exception("Missing primaryKey for schema '{$namespace}'");
  483. return $xsdType;
  484. }
  485. }