Piotr Labudda 9 лет назад
Родитель
Сommit
5cf6ba17bc
1 измененных файлов с 52 добавлено и 0 удалено
  1. 52 0
      SE/se-lib/XML.php

+ 52 - 0
SE/se-lib/XML.php

@@ -66,4 +66,56 @@ class XML {
     $xmlWriter->endDocument();
   }
 
+  public static function findElementName($docArray, $nodeArray) {
+    if (!empty($nodeArray[1]['name'])) return $nodeArray[1]['name'];
+    if (!empty($nodeArray[1]['ref'])) return $nodeArray[1]['ref'];
+    throw new Exception("Missing xsd:element name");
+  }
+
+  public static function findElementType($docArray, $nodeArray) {
+    $fieldName = self::findElementName($docArray, $nodeArray);
+    if (!empty($nodeArray[1]['type'])) return $nodeArray[1]['type'];
+    if (!empty($nodeArray[1]['ref'])) return 'ref:' . $nodeArray[1]['ref'];
+    if (empty($nodeArray[2])) throw new Exception("Missing xsd:element childrens - cannot find type");
+    if (empty($nodeArray[2][0][0]) || 'xsd:simpleType' != $nodeArray[2][0][0]) throw new Exception("Missing 'xsd:simpleType' for field '{$fieldName}'");
+    if (empty($nodeArray[2][0][2][0]) || 'xsd:restriction' != $nodeArray[2][0][2][0][0]) throw new Exception("Missing 'xsd:restriction' for field '{$fieldName}'");
+    if (empty($nodeArray[2][0][2][0][1]['base'])) throw new Exception("Missing 'xsd:restriction/@base' for field '{$fieldName}'");
+    return $nodeArray[2][0][2][0][1]['base'];
+  }
+
+  public static function findElementRestrictions($docArray, $nodeArray) {
+    $restrictions = [];
+    $fieldName = self::findElementName($docArray, $nodeArray);
+    if (!empty($nodeArray[1]['nillable']) && 'true' === $nodeArray[1]['nillable']) $restrictions['nillable'] = true;
+    if (!empty($nodeArray[2])) {
+      if (empty($nodeArray[2][0][0]) || 'xsd:simpleType' != $nodeArray[2][0][0]) throw new Exception("Missing 'xsd:simpleType' for field '{$fieldName}'");
+      if (empty($nodeArray[2][0][2][0]) || 'xsd:restriction' != $nodeArray[2][0][2][0][0]) throw new Exception("Missing 'xsd:restriction' for field '{$fieldName}'");
+      if (empty($nodeArray[2][0][2][0][1]['base'])) throw new Exception("Missing 'xsd:restriction/@base' for field '{$fieldName}'");
+      // [2] => Array:
+      //    [0] => Array:
+      //             [0] => xsd:simpleType
+      //             [1] => Array:
+      //             [2] => Array:
+      //                 [0] => Array:
+      //                     [0] => xsd:restriction
+      //                     [1] => Array:
+      //                         [base] => xsd:string
+      //                     [2] => Array:
+      foreach ($nodeArray[2][0][2][0][2] as $tagRestriction) {
+        //                         [0] => Array:
+        //                             [0] => xsd:maxLength
+        //                             [1] => Array:
+        //                                 [value] => 20
+        //                             [2] =>
+        $val = $tagRestriction[1]['value'];
+        if ('xsd:enumeration' == $tagRestriction[0]) {
+          $restrictions['enumeration'][$val] = $val;
+        } else {
+          $restrictions[substr($tagRestriction[0], 4)] = $val;
+        }
+      }
+    }
+    return $restrictions;
+  }
+
 }