AttributeRule.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. class AttributeRule
  3. {
  4. private $type;
  5. private $name;
  6. private $value;
  7. /**
  8. * @param $name
  9. * @param $type
  10. * @param $value
  11. */
  12. public function __construct($type = null, $name = null, $value = null)
  13. {
  14. $this->type = $type;
  15. $this->name = $name;
  16. $this->value = $value;
  17. }
  18. public function checkRuleType($ruleType)
  19. {
  20. $ruleTypeArr = array(
  21. "required" => "是否必填,true/false",
  22. "maxLength" => "字符串要求最大长度",
  23. "minLength" => "字符串要求最小长度",
  24. "exclusiveMaximum" => "最大值,包含最大值,<=",
  25. "exclusiveMinimum" => "最小值,包含最小值,>=",
  26. "maximum" => "最大值,不包含最大值,<",
  27. "minimum" => "最小值,不包含最小值,>",
  28. "maxSize" => "最大个数",
  29. "minSize" => "最大个数",
  30. "precision" => "精度",
  31. "pattern" => "正则表达式",
  32. "valueType" => "值类型",
  33. "devTip" => "输入提示",
  34. "height" => "高度",
  35. "width" => "长度",
  36. "urlSchema" => "url格式"
  37. );
  38. if (empty($ruleType)) return false;
  39. if (array_key_exists($ruleType, $ruleTypeArr)) return true;
  40. return false;
  41. }
  42. /**
  43. * @throws Exception
  44. */
  45. public function toElement($dom, $parent, $attributeId)
  46. {
  47. if (empty($this->name) && $this->name != '0') {
  48. throw new \Exception("id=[" . $attributeId . "] AttrRule格式错误!AttrRule缺少name!");
  49. }
  50. if (!$this->checkRuleType($this->type)) {
  51. throw new \Exception("id=[" . $attributeId . "] AttrRule类型type不合法!");
  52. }
  53. if (empty($this->value) && $this->value != '0') {
  54. throw new \Exception("id=[" . $attributeId . "] AttrRule格式错误!AttrRule缺少value!");
  55. }
  56. $rule = $dom->createElement("rule");
  57. $rule->setAttribute("name", $this->name);
  58. $rule->setAttribute("type", $this->type);
  59. $rule->setAttribute("value", $this->value);
  60. $parent->appendChild($rule);
  61. }
  62. /**
  63. * @return mixed
  64. */
  65. public function getName()
  66. {
  67. return $this->name;
  68. }
  69. /**
  70. * @param mixed $name
  71. */
  72. public function setName($name)
  73. {
  74. $this->name = $name;
  75. }
  76. /**
  77. * @return mixed
  78. */
  79. public function getType()
  80. {
  81. return $this->type;
  82. }
  83. /**
  84. * @param mixed $type
  85. */
  86. public function setType($type)
  87. {
  88. $this->type = $type;
  89. }
  90. /**
  91. * @return mixed
  92. */
  93. public function getValue()
  94. {
  95. return $this->value;
  96. }
  97. /**
  98. * @param mixed $value
  99. */
  100. public function setValue($value)
  101. {
  102. $this->value = $value;
  103. }
  104. }