ServiceSchemaReader.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. require_once 'XMLAttribute.php';
  3. require_once 'AttributeRule.php';
  4. require_once 'Option.php';
  5. require_once 'ServiceSchemaFactory.php';
  6. class ServiceSchemaReader
  7. {
  8. /**
  9. * @throws Exception
  10. */
  11. public static function readXmlForArrayByFile($filePath)
  12. {
  13. set_error_handler('ServiceSchemaReader::errorHandler');
  14. $dom = new DOMDocument('1.0', 'utf-8');
  15. try {
  16. $dom->load($filePath);
  17. } catch (Exception $e) {
  18. throw new \Exception("XML格式错误,无法正常解析!");
  19. }
  20. return self::readXmlForList($dom->documentElement);
  21. }
  22. /**
  23. * @throws Exception
  24. */
  25. public static function readXmlForArrayByString($xmlString)
  26. {
  27. set_error_handler('ServiceSchemaReader::errorHandler');
  28. $dom = new DOMDocument('1.0', 'utf-8');
  29. try {
  30. $dom->loadXML($xmlString);
  31. } catch (Exception $e) {
  32. throw new \Exception("XML格式错误,无法正常解析!");
  33. }
  34. return self::readXmlForList($dom->documentElement);
  35. }
  36. /**
  37. * @throws Exception
  38. */
  39. public static function readXmlForList(DOMElement $rootEle)
  40. {
  41. $attributeArr = array();
  42. foreach ($rootEle->getElementsByTagName('attribute') as $item) {
  43. if ($item->parentNode === $rootEle) {
  44. $attribute = self::elementToAttribute($item);
  45. $attributeArr[$attribute->getId()] = $attribute;
  46. }
  47. }
  48. return $attributeArr;
  49. }
  50. /**
  51. * @throws Exception
  52. */
  53. public static function elementToAttribute($attributeElm)
  54. {
  55. $attributeId = $attributeElm->getAttribute("id");
  56. if (self::checkEmpty($attributeId)) {
  57. throw new \Exception("Attribute属性缺少id! 节点路径 [" . $attributeElm->getNodePath() . "]");
  58. }
  59. $attributeName = $attributeElm->getAttribute("name");
  60. $attributeType = $attributeElm->getAttribute("type");
  61. if (self::checkEmpty($attributeType)) {
  62. throw new \Exception("Attribute属性缺少type! 节点路径 [" . $attributeElm->getNodePath() . "].attributeId=" . $attributeId);
  63. }
  64. $valueType = $attributeElm->getAttribute("valueType");
  65. $attribute = ServiceSchemaFactory::createAttribute($attributeId, $attributeName, $attributeType, $valueType);
  66. if (!$attribute->checkAttributeValueType($valueType)) {
  67. throw new \Exception("Attribute属性valueType不正确! 节点路径 [" . $attributeElm->getNodePath() . "].attributeId=" . $attributeId);
  68. }
  69. if ("single" === $attributeType) {
  70. ServiceSchemaReader::elementToSingleAttribute($attributeElm, $attribute);
  71. } elseif ("multi" === $attributeType) {
  72. ServiceSchemaReader::elementToMultiAttribute($attributeElm, $attribute);
  73. } elseif ("complex" === $attributeType) {
  74. ServiceSchemaReader::elementToComplexAttribute($attributeElm, $attribute);
  75. } elseif ("multiComplex" === $attributeType) {
  76. ServiceSchemaReader::elementToMultiComplexAttribute($attributeElm, $attribute);
  77. } else {
  78. throw new \Exception("Attribute属性type类型不正确! 节点路径 [" . $attributeElm->getNodePath() . "].attributeId=" . $attributeId);
  79. }
  80. return $attribute;
  81. }
  82. /**
  83. * @throws Exception
  84. */
  85. private static function elementToSingleAttribute($attributeElm, $attribute)
  86. {
  87. self::elementToRule($attributeElm, $attribute);
  88. self::elementToOption($attributeElm, $attribute);
  89. foreach ($attributeElm->getElementsByTagName('value') as $value) {
  90. if ($value->parentNode === $attributeElm) {
  91. $attribute->setValues($value->nodeValue);
  92. //只取第一个<value>标签
  93. break;
  94. }
  95. }
  96. }
  97. /**
  98. * @throws Exception
  99. */
  100. private static function elementToMultiAttribute($attributeElm, $attribute)
  101. {
  102. self::elementToRule($attributeElm, $attribute);
  103. self::elementToOption($attributeElm, $attribute);
  104. foreach ($attributeElm->getElementsByTagName('values') as $values) {
  105. if ($values->parentNode === $attributeElm) {
  106. $valueArr = array();
  107. foreach ($values->getElementsByTagName('value') as $value) {
  108. if ($value->parentNode === $values) {
  109. $valueArr[] = $value->nodeValue;
  110. }
  111. }
  112. $attribute->setValues($valueArr);
  113. //只取第一个<values>标签
  114. break;
  115. }
  116. }
  117. }
  118. /**
  119. * @throws Exception
  120. */
  121. private static function elementToComplexAttribute($attributeElm, $attribute)
  122. {
  123. self::elementToRule($attributeElm, $attribute);
  124. foreach ($attributeElm->getElementsByTagName('attributes') as $attributes) {
  125. if ($attributes->parentNode === $attributeElm) {
  126. $attributeArr = array();
  127. foreach ($attributes->getElementsByTagName('attribute') as $item) {
  128. if ($item->parentNode === $attributes) {
  129. $attributeType = $item->getAttribute("type");
  130. if ("single" === $attributeType || "multi" === $attributeType) {
  131. $attributeArr[] = self::elementToAttribute($item);
  132. }
  133. } else {
  134. throw new \Exception("Attribute属性type类型不正确! 节点路径 [" . $item->getNodePath() . "]");
  135. }
  136. }
  137. $attribute->setValues($attributeArr);
  138. //只取第一个<attributes>标签
  139. break;
  140. }
  141. }
  142. }
  143. /**
  144. * @throws Exception
  145. */
  146. private static function elementToMultiComplexAttribute($attributeElm, $attribute)
  147. {
  148. $attributeArr = array();
  149. foreach ($attributeElm->getElementsByTagName('attributes') as $attributes) {
  150. if ($attributes->parentNode === $attributeElm) {
  151. $complexAttr = new XMLAttribute();//每一个$complexAttr都是一个ComplexAttribute对象
  152. $valuesArr = array();
  153. foreach ($attributes->getElementsByTagName('attribute') as $item) {
  154. if ($item->parentNode === $attributes) {
  155. $attributeType = $item->getAttribute("type");
  156. if ("single" === $attributeType || "multi" === $attributeType) {
  157. $valuesArr[] = self::elementToAttribute($item);
  158. }
  159. } else {
  160. throw new \Exception("Attribute属性type类型不正确! 节点路径 [" . $item->getNodePath() . "]");
  161. }
  162. }
  163. $complexAttr->setValues($valuesArr);
  164. $attributeArr[] = $complexAttr;
  165. }
  166. }
  167. $attribute->setValues($attributeArr);
  168. }
  169. /**
  170. * @throws Exception
  171. */
  172. private static function elementToRule($attributeElm, $attribute)
  173. {
  174. foreach ($attributeElm->getElementsByTagName('rules') as $rules) {
  175. if ($rules->parentNode === $attributeElm) {
  176. $ruleArr = array();
  177. foreach ($rules->getElementsByTagName('rule') as $rule) {
  178. if ($rule->parentNode === $rules) {
  179. $ruleType = $rule->getAttribute("type");
  180. $ruleName = $rule->getAttribute("name");
  181. $ruleValue = $rule->getAttribute("value");
  182. if (self::checkEmpty($ruleValue)) {
  183. throw new \Exception("id=[" . $attribute->getId() . "] AttrRule格式错误!AttrRule缺少value!");
  184. }
  185. $ruleObj = ServiceSchemaFactory::createRule($ruleType, $ruleName, $ruleValue);
  186. if (!$ruleObj->checkRuleType($ruleType)) {
  187. throw new \Exception("id=[" . $attribute->getId() . "] AttrRule类型type不合法!");
  188. }
  189. $ruleArr[] = $ruleObj;
  190. }
  191. }
  192. $attribute->setRules($ruleArr);
  193. //只取第一个<rules>标签
  194. break;
  195. }
  196. }
  197. }
  198. /**
  199. * @throws Exception
  200. */
  201. private static function elementToOption($attributeElm, $attribute)
  202. {
  203. foreach ($attributeElm->getElementsByTagName('options') as $options) {
  204. if ($options->parentNode === $attributeElm) {
  205. $optionsArr = array();
  206. foreach ($options->getElementsByTagName('option') as $option) {
  207. if ($option->parentNode === $options) {
  208. $displayName = $option->getAttribute("displayName");
  209. if (self::checkEmpty($displayName)) {
  210. throw new \Exception("id=[" . $attribute->getId() . "] Option格式错误!Option名称displayName不能为空!");
  211. }
  212. $value = $option->getAttribute("value");
  213. if (self::checkEmpty($value)) {
  214. throw new \Exception("id=[" . $attribute->getId() . "] Option格式错误!Option的值value不能为空!");
  215. }
  216. $optionObj = ServiceSchemaFactory::createOption($displayName, $value);
  217. $optionsArr[] = $optionObj;
  218. }
  219. }
  220. $attribute->setOptions($optionsArr);
  221. //只取第一个<options>标签
  222. break;
  223. }
  224. }
  225. }
  226. private static function checkEmpty($value)
  227. {
  228. if (is_null($value))
  229. return true;
  230. if (trim($value) == "")
  231. return true;
  232. return false;
  233. }
  234. /**
  235. * @throws Exception
  236. */
  237. static function errorHandler($errno, $errstr, $errfile, $errline)
  238. {
  239. throw new \Exception($errstr);
  240. return true;
  241. }
  242. }