FormComponentDriver.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * FormBuilder表单生成器
  4. * Author: xaboy
  5. * Github: https://github.com/xaboy/form-builder
  6. */
  7. namespace FormBuilder;
  8. use FormBuilder\interfaces\FormComponentInterFace;
  9. abstract class FormComponentDriver implements FormComponentInterFace
  10. {
  11. /**
  12. * 字段名
  13. * @var String
  14. */
  15. protected $field;
  16. /**
  17. * 字段昵称
  18. * @var String
  19. */
  20. protected $title;
  21. /**
  22. * 组件名称
  23. * @var String
  24. */
  25. protected $name;
  26. /**
  27. * 组件的规则
  28. * @var array
  29. */
  30. protected $props = [];
  31. /**
  32. * 字段的值
  33. * @var
  34. */
  35. protected $value = '';
  36. /**
  37. * 字段验证规则
  38. * @var array
  39. */
  40. protected $validate = [];
  41. /**
  42. * 组件属性设置规则
  43. * @var array
  44. */
  45. protected static $propsRule = [];
  46. /**
  47. * 设置组件属性
  48. * @param $name
  49. * @param $arguments
  50. * @return $this
  51. * @throws \Exception
  52. */
  53. public function __call($name, $arguments)
  54. {
  55. if (isset(static::$propsRule[$name])) {
  56. if (is_array(static::$propsRule[$name])) {
  57. $this->props[static::$propsRule[$name][1]] = Helper::toType(
  58. $arguments[0],
  59. static::$propsRule[$name][0]
  60. );
  61. } else {
  62. $this->props[$name] = Helper::toType(
  63. $arguments[0],
  64. static::$propsRule[$name]
  65. );
  66. }
  67. return $this;
  68. } else {
  69. throw new \Exception($name . '方法不存在');
  70. }
  71. }
  72. /**
  73. * FormComponentDriver constructor.
  74. * @param String $field 字段名
  75. * @param String $title 字段昵称
  76. * @param String $value 字段值
  77. */
  78. public function __construct($field, $title, $value = null)
  79. {
  80. $this->field = (string)$field;
  81. $this->title = (string)$title;
  82. static::value($value);
  83. static::init();
  84. }
  85. /**
  86. * 组件初始化
  87. */
  88. protected function init()
  89. {
  90. }
  91. /**
  92. * 批量设置组件的规则
  93. * @param array $props
  94. */
  95. public function setProps(array $props = [])
  96. {
  97. foreach ($props as $k => $v) {
  98. $this->{$k}($v);
  99. }
  100. }
  101. /**
  102. * 获取组件的规则
  103. * @param $name
  104. * @return mixed|null
  105. */
  106. public function getProps($name)
  107. {
  108. return isset($this->props[$name]) ? $this->props[$name] : null;
  109. }
  110. /**
  111. * 设置组件的值
  112. * @param $value
  113. * @param string $default
  114. * @return $this
  115. */
  116. public function value($value)
  117. {
  118. if ($value === null) $value = '';
  119. $this->value = (string)$value;
  120. return $this;
  121. }
  122. /**
  123. * 获取组件的值
  124. * @return string
  125. */
  126. public function getValue()
  127. {
  128. return $this->value;
  129. }
  130. /**
  131. * 获取组件的字段名
  132. * @return String
  133. */
  134. public function getField()
  135. {
  136. return $this->field;
  137. }
  138. /**
  139. * 设置组件的昵称
  140. * @return String
  141. */
  142. public function getTitle()
  143. {
  144. return $this->title;
  145. }
  146. /**
  147. * 设置组件的值为必填
  148. * @param null $message
  149. * @return $this
  150. */
  151. protected function setRequired($message = '', $trigger = 'change', $type = null)
  152. {
  153. $validate = [
  154. 'required' => true,
  155. 'message' => $message,
  156. 'trigger' => $trigger
  157. ];
  158. if ($type !== null) $validate['type'] = $type;
  159. $this->validate[] = $validate;
  160. }
  161. /**
  162. * 添加一条验证规则
  163. * @param array $validate
  164. * @return $this
  165. */
  166. public function validate(array $validate)
  167. {
  168. $this->validate[] = $validate;
  169. return $this;
  170. }
  171. }