XML.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. Lib::loadClass('Core_XmlWriter');
  3. // $zxo = new XMLDiff\Memory;// @require xmldiff pecl package installed
  4. class XML {
  5. public static function xmlToArray($xml) {
  6. }
  7. public static function readXmlFileToArray($filePath) {
  8. $z = new XMLReader();
  9. if (!$z) throw new HttpException("Class XMLReader not installed", 500);
  10. if (!$z->open($filePath)) throw new Exception("Failed to open ant schema file for '{$item['namespace']}'", 500);
  11. return self::xmlReadToArray($z);
  12. }
  13. public static function xmlReadToArray($z) {
  14. while ($z->read()) {
  15. if (XMLReader::ELEMENT !== $z->nodeType) continue;
  16. return self::xmlReadRecurse($z);
  17. }
  18. }
  19. public static function xmlReadRecurse($z) {
  20. $node = [ $z->name, [], [] ];// name, attrs, childrens
  21. $depth = $z->depth;
  22. $isEmpty = $z->isEmptyElement;
  23. if ($z->hasAttributes) {
  24. while ($z->moveToNextAttribute()) {
  25. $node[1][$z->name] = $z->value;
  26. }
  27. }
  28. if ($isEmpty) {
  29. $node[2] = null;
  30. return $node;
  31. }
  32. // switch ($z->name) {
  33. // case 'xsd:complexType': $node[1]['name'] = $z->getAttribute('name'); break;
  34. // case 'xsd:simpleType': $node[1]['name'] = $z->getAttribute('name'); break;
  35. // case 'xsd:restriction': $node[1]['base'] = $z->getAttribute('base'); break;
  36. // case 'xsd:enumeration': $node[1]['value'] = $z->getAttribute('value'); break;
  37. // case 'xsd:element': $node[1] = [
  38. // 'type' => $z->getAttribute('type')
  39. // ]; break;
  40. // case 'xsd:complexContent': break;
  41. // case 'xsd:sequence': break;
  42. // case 'xsd:attribute': $node[1]['base'] = $z->getAttribute('base'); break;
  43. // case 'xsd:extension': $node[1]['base'] = $z->getAttribute('base'); break;
  44. // default: UI::alert('warning', "TODO: read attributes from: d({$z->depth}) name($z->name)");
  45. // }
  46. while ($z->read() && $z->depth > $depth) {
  47. if (XMLReader::ELEMENT !== $z->nodeType) continue;
  48. // if ($z->depth == $depth + 2)
  49. $node[2][] = self::xmlReadRecurse($z);
  50. // else $node[2][] = "d({$z->depth}) name($z->name)";
  51. }
  52. return $node;
  53. }
  54. public static function printXmlFromArray($xml) {
  55. $xmlWriter = new Core_XmlWriter();
  56. $xmlWriter->openUri('php://output');
  57. $xmlWriter->setIndent(true);
  58. $xmlWriter->startDocument('1.0','UTF-8');
  59. $xmlWriter->h($xml[0], $xml[1], $xml[2]);
  60. $xmlWriter->endDocument();
  61. }
  62. public static function findElementName($docArray, $nodeArray) {
  63. if (!empty($nodeArray[1]['name'])) return $nodeArray[1]['name'];
  64. if (!empty($nodeArray[1]['ref'])) return $nodeArray[1]['ref'];
  65. throw new Exception("Missing xsd:element name");
  66. }
  67. public static function findElementType($docArray, $nodeArray) {
  68. $fieldName = self::findElementName($docArray, $nodeArray);
  69. if (!empty($nodeArray[1]['type'])) return $nodeArray[1]['type'];
  70. if (!empty($nodeArray[1]['ref'])) return 'ref:' . $nodeArray[1]['ref'];
  71. if (empty($nodeArray[2])) throw new Exception("Missing xsd:element childrens - cannot find type");
  72. if (empty($nodeArray[2][0][0]) || 'xsd:simpleType' != $nodeArray[2][0][0]) throw new Exception("Missing 'xsd:simpleType' for field '{$fieldName}'");
  73. if (empty($nodeArray[2][0][2][0]) || 'xsd:restriction' != $nodeArray[2][0][2][0][0]) throw new Exception("Missing 'xsd:restriction' for field '{$fieldName}'");
  74. if (empty($nodeArray[2][0][2][0][1]['base'])) throw new Exception("Missing 'xsd:restriction/@base' for field '{$fieldName}'");
  75. return $nodeArray[2][0][2][0][1]['base'];
  76. }
  77. public static function findElementRestrictions($docArray, $nodeArray) {
  78. $restrictions = [];
  79. $fieldName = self::findElementName($docArray, $nodeArray);
  80. if (!empty($nodeArray[1]['nillable']) && 'true' === $nodeArray[1]['nillable']) $restrictions['nillable'] = true;
  81. if (!empty($nodeArray[2])) {
  82. if (empty($nodeArray[2][0][0]) || 'xsd:simpleType' != $nodeArray[2][0][0]) throw new Exception("Missing 'xsd:simpleType' for field '{$fieldName}'");
  83. if (empty($nodeArray[2][0][2][0]) || 'xsd:restriction' != $nodeArray[2][0][2][0][0]) throw new Exception("Missing 'xsd:restriction' for field '{$fieldName}'");
  84. if (empty($nodeArray[2][0][2][0][1]['base'])) throw new Exception("Missing 'xsd:restriction/@base' for field '{$fieldName}'");
  85. // [2] => Array:
  86. // [0] => Array:
  87. // [0] => xsd:simpleType
  88. // [1] => Array:
  89. // [2] => Array:
  90. // [0] => Array:
  91. // [0] => xsd:restriction
  92. // [1] => Array:
  93. // [base] => xsd:string
  94. // [2] => Array:
  95. foreach ($nodeArray[2][0][2][0][2] as $tagRestriction) {
  96. // [0] => Array:
  97. // [0] => xsd:maxLength
  98. // [1] => Array:
  99. // [value] => 20
  100. // [2] =>
  101. $val = $tagRestriction[1]['value'];
  102. if ('xsd:enumeration' == $tagRestriction[0]) {
  103. $restrictions['enumeration'][$val] = $val;
  104. } else {
  105. $restrictions[substr($tagRestriction[0], 4)] = $val;
  106. }
  107. }
  108. }
  109. return $restrictions;
  110. }
  111. }