XMLAttribute.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. class XMLAttribute
  3. {
  4. private $id;
  5. private $name;
  6. private $type;
  7. private $valueType;
  8. private $rules;
  9. private $options;
  10. private $values;
  11. public function checkAttributeValueType($attributeValueType)
  12. {
  13. $valueTypeArr = array(
  14. "text" => "文本类型",
  15. "boolean" => "boolean类型",
  16. "numeric" => "数据类型",
  17. "enum" => "枚举型",
  18. "object" => "对象型"
  19. );
  20. if (empty($attributeValueType)) return false;
  21. // foreach ($valueTypeArr as $key => $value){
  22. // if($attributeValueType === $key) return true;
  23. // }
  24. if (array_key_exists($attributeValueType, $valueTypeArr)) return true;
  25. return false;
  26. }
  27. /**
  28. * @throws Exception
  29. */
  30. public function toElement($dom, $parent)
  31. {
  32. $this->checkAttribute();
  33. if ("single" === $this->type || "multi" === $this->type) {
  34. $attributeElm = $this->appendAttributeValues($dom, $parent, $this);
  35. $this->appendRulesElement($dom, $attributeElm, $this->rules, $this->id);
  36. $this->appendOptionsElement($dom, $attributeElm, $this->options, $this->id);
  37. } elseif ("complex" === $this->type) {
  38. $attributeElm = $dom->createElement("attribute");
  39. $attributeElm->setAttribute("id", $this->id);
  40. $attributeElm->setAttribute("type", $this->type);
  41. if ($this->name != "" && !is_null($this->name)) {
  42. $attributeElm->setAttribute("name", $this->name);
  43. }
  44. if ($this->checkAttributeValueType($this->valueType)) {
  45. $attributeElm->setAttribute("valueType", $this->valueType);
  46. }
  47. $attributesElm = $dom->createElement("attributes");
  48. if (is_array($this->values)) {
  49. foreach ($this->values as $attribute) {
  50. if ($attribute instanceof XMLAttribute) {
  51. $attributeElmTmp = $this->appendAttributeValues($dom, $attributesElm, $attribute);
  52. $this->appendRulesElement($dom, $attributeElmTmp, $attribute->rules, $attribute->id);
  53. $this->appendOptionsElement($dom, $attributeElmTmp, $attribute->options, $attribute->id);
  54. }
  55. }
  56. } elseif (!empty($this->values)) {//complex类型的values必须是数组 或 空
  57. throw new \Exception("id=[" . $this->id . "] XMLAttribute属性values不合法!");
  58. }
  59. $attributeElm->appendChild($attributesElm);
  60. $this->appendRulesElement($dom, $attributeElm, $this->rules, $this->id);
  61. $parent->appendChild($attributeElm);
  62. } elseif ("multiComplex" === $this->type) {
  63. $attributeElm = $dom->createElement("attribute");
  64. $attributeElm->setAttribute("id", $this->id);
  65. if ($this->name != "" && !is_null($this->name)) {
  66. $attributeElm->setAttribute("name", $this->name);
  67. }
  68. $attributeElm->setAttribute("type", $this->type);
  69. $attributeElm->setAttribute("valueType", "object");
  70. if (is_array($this->values)) {
  71. foreach ($this->values as $complexAttribute) {
  72. if ($complexAttribute instanceof XMLAttribute) {
  73. $attributesElm = $dom->createElement("attributes");
  74. if (is_array($complexAttribute->getValues())) {
  75. foreach ($complexAttribute->getValues() as $attribute) {
  76. if ($attribute instanceof XMLAttribute) {
  77. if (!$this->checkAttributeValueType($attribute->getValueType())) {
  78. throw new \Exception("id=[" . $attribute->getId() . "] XMLAttribute属性valueType不正确!");
  79. }
  80. $attributeElmTmp = $this->appendAttributeValues($dom, $attributesElm, $attribute);
  81. $this->appendRulesElement($dom, $attributeElmTmp, $attribute->rules, $attribute->id);
  82. $this->appendOptionsElement($dom, $attributeElmTmp, $attribute->options, $attribute->id);
  83. }
  84. }
  85. } elseif (!empty($complexAttribute->getValues())) {//complex类型的values必须是数组 或 空
  86. throw new \Exception("id=[" . $complexAttribute->getId() . "] XMLAttribute属性values不合法!");
  87. }
  88. $attributeElm->appendChild($attributesElm);
  89. }
  90. }
  91. } elseif (!empty($this->values)) {//multiComplex类型的values必须是数组 或 空
  92. throw new \Exception("id=[" . $this->id . "] XMLAttribute属性values不合法!");
  93. }
  94. $parent->appendChild($attributeElm);
  95. } else {
  96. throw new \Exception("id=[" . $this->id . "] XMLAttribute属性type类型不正确!");
  97. }
  98. }
  99. /**
  100. * @throws Exception
  101. */
  102. public function toValueElement($dom, $parent)
  103. {
  104. $this->checkAttribute();
  105. if ("single" === $this->type || "multi" === $this->type) {
  106. $this->appendAttributeValues($dom, $parent, $this);
  107. } elseif ("complex" === $this->type) {
  108. $attributeElm = $dom->createElement("attribute");
  109. $attributeElm->setAttribute("id", $this->id);
  110. $attributeElm->setAttribute("type", $this->type);
  111. if ($this->name != "" && !is_null($this->name)) {
  112. $attributeElm->setAttribute("name", $this->name);
  113. }
  114. if ($this->checkAttributeValueType($this->valueType)) {
  115. $attributeElm->setAttribute("valueType", $this->valueType);
  116. }
  117. $attributesElm = $dom->createElement("attributes");
  118. if (is_array($this->values)) {
  119. foreach ($this->values as $attribute) {
  120. if ($attribute instanceof XMLAttribute) {
  121. $this->appendAttributeValues($dom, $attributesElm, $attribute);
  122. }
  123. }
  124. } elseif (!empty($this->values)) {//complex类型的values必须是数组 或 空
  125. throw new \Exception("id=[" . $this->id . "] XMLAttribute属性values不合法!");
  126. }
  127. $attributeElm->appendChild($attributesElm);
  128. $parent->appendChild($attributeElm);
  129. } elseif ("multiComplex" === $this->type) {
  130. $attributeElm = $dom->createElement("attribute");
  131. $attributeElm->setAttribute("id", $this->id);
  132. if ($this->name != "" && !is_null($this->name)) {
  133. $attributeElm->setAttribute("name", $this->name);
  134. }
  135. $attributeElm->setAttribute("type", $this->type);
  136. $attributeElm->setAttribute("valueType", "object");
  137. if (is_array($this->values)) {
  138. foreach ($this->values as $complexAttribute) {
  139. if ($complexAttribute instanceof XMLAttribute) {
  140. $attributesElm = $dom->createElement("attributes");
  141. if (is_array($complexAttribute->getValues())) {
  142. foreach ($complexAttribute->getValues() as $attribute) {
  143. if ($attribute instanceof XMLAttribute) {
  144. if (!$this->checkAttributeValueType($attribute->getValueType())) {
  145. throw new \Exception("id=[" . $attribute->getId() . "] XMLAttribute属性valueType不正确!");
  146. }
  147. $this->appendAttributeValues($dom, $attributesElm, $attribute);
  148. }
  149. }
  150. } elseif (!empty($complexAttribute->getValues())) {//complex类型的values必须是数组 或 空
  151. throw new \Exception("id=[" . $this->id . "] XMLAttribute属性values不合法!");
  152. }
  153. $attributeElm->appendChild($attributesElm);
  154. }
  155. }
  156. } elseif (!empty($this->values)) {//multiComplex类型的values必须是数组 或 空
  157. throw new \Exception("id=[" . $this->id . "] XMLAttribute属性values不合法!");
  158. }
  159. $parent->appendChild($attributeElm);
  160. } else {
  161. throw new \Exception("id=[" . $this->id . "] XMLAttribute属性type类型不正确!");
  162. }
  163. }
  164. /**
  165. * @throws Exception
  166. */
  167. protected function appendAttributeValues($dom, $parent, $attribute)
  168. {
  169. $attributeElm = $dom->createElement("attribute");
  170. if ($attribute->getId() != "" && !is_null($attribute->getId())) {
  171. $attributeElm->setAttribute("id", $attribute->getId());
  172. }
  173. if ($attribute->getName() != "" && !is_null($attribute->getName())) {
  174. $attributeElm->setAttribute("name", $attribute->getName());
  175. }
  176. if ($attribute->getType() != "" && !is_null($attribute->getType())) {
  177. $attributeElm->setAttribute("type", $attribute->getType());
  178. }
  179. if ($this->checkAttributeValueType($attribute->getValueType())) {
  180. $attributeElm->setAttribute("valueType", $attribute->getValueType());
  181. }
  182. $values = $attribute->getValues();
  183. if ("single" === $attribute->getType()) {
  184. $valueElm = $dom->createElement("value");
  185. if (is_string($values)) {
  186. $text = $dom->createTextNode($values);
  187. $valueElm->appendChild($text);
  188. } elseif (!is_null($values)) {//single类型的values必须是字符串 或 空
  189. throw new \Exception("id=[" . $attribute->getId() . "] XMLAttribute属性values不合法!");
  190. }
  191. $attributeElm->appendChild($valueElm);
  192. $parent->appendChild($attributeElm);
  193. } elseif ("multi" === $attribute->getType()) {
  194. $valuesElm = $dom->createElement("values");
  195. if (is_array($values)) {
  196. foreach ($values as $value) {
  197. if (is_string($value) || is_null($value)) {
  198. $valueElm = $dom->createElement("value", $value);
  199. $valuesElm->appendChild($valueElm);
  200. }
  201. }
  202. } elseif (!empty($values)) {//multi类型的values必须是数组 或 空
  203. throw new \Exception("id=[" . $attribute->getId() . "] XMLAttribute属性values不合法!");
  204. }
  205. $attributeElm->appendChild($valuesElm);
  206. $parent->appendChild($attributeElm);
  207. } else {
  208. throw new \Exception("id=[" . $attribute->getId() . "] XMLAttribute属性type类型不正确!");
  209. }
  210. return $attributeElm;
  211. }
  212. /**
  213. * @throws Exception
  214. */
  215. protected function appendRulesElement($dom, $parent, $rules, $attributeId)
  216. {
  217. if (empty($rules)) return;
  218. if (!is_array($rules)) {
  219. throw new \Exception("id=[" . $attributeId . "] XMLAttribute属性rules不合法!");
  220. }
  221. $rulesElm = $dom->createElement("rules");
  222. foreach ($rules as $rule) {
  223. if ($rule instanceof AttributeRule) {
  224. $rule->toElement($dom, $rulesElm, $attributeId);
  225. }
  226. }
  227. $parent->appendChild($rulesElm);
  228. }
  229. /**
  230. * @throws Exception
  231. */
  232. protected function appendOptionsElement($dom, $parent, $options, $attributeId)
  233. {
  234. if (empty($options)) return;
  235. if (!is_array($options)) {
  236. throw new \Exception("id=[" . $attributeId . "] XMLAttribute属性options不合法!");
  237. }
  238. $optionsElm = $dom->createElement("options");
  239. foreach ($options as $option) {
  240. if ($option instanceof Option) {
  241. $option->toElement($dom, $optionsElm, $attributeId);
  242. }
  243. }
  244. $parent->appendChild($optionsElm);
  245. }
  246. /**
  247. * @throws Exception
  248. */
  249. protected function checkAttribute()
  250. {
  251. if (empty($this->id) && $this->id != '0') {
  252. throw new \Exception("XMLAttribute属性缺少id!");
  253. }
  254. if (empty($this->type) && $this->type != '0') {
  255. throw new \Exception("id=[" . $this->id . "] XMLAttribute属性缺少type!");
  256. }
  257. }
  258. public function getId()
  259. {
  260. return $this->id;
  261. }
  262. public function setId($id)
  263. {
  264. $this->id = $id;
  265. }
  266. public function getName()
  267. {
  268. return $this->name;
  269. }
  270. public function setName($name)
  271. {
  272. $this->name = $name;
  273. }
  274. public function getType()
  275. {
  276. return $this->type;
  277. }
  278. public function setType($type)
  279. {
  280. $this->type = $type;
  281. }
  282. public function getValueType()
  283. {
  284. return $this->valueType;
  285. }
  286. public function setValueType($valueType)
  287. {
  288. $this->valueType = $valueType;
  289. }
  290. public function getRules()
  291. {
  292. return $this->rules;
  293. }
  294. public function setRules($rules)
  295. {
  296. $this->rules = $rules;
  297. }
  298. public function getOptions()
  299. {
  300. return $this->options;
  301. }
  302. public function setOptions($options)
  303. {
  304. $this->options = $options;
  305. }
  306. public function getValues()
  307. {
  308. return $this->values;
  309. }
  310. public function setValues($values)
  311. {
  312. $this->values = $values;
  313. }
  314. }