FormComponentDriver.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * FormBuilder表单生成器
  4. * Author: xaboy
  5. * Github: https://github.com/xaboy/form-builder
  6. */
  7. namespace FormBuilder;
  8. use FormBuilder\components\Col;
  9. use FormBuilder\components\Validate;
  10. use FormBuilder\interfaces\FormComponentInterFace;
  11. use FormBuilder\traits\component\CallPropsTrait;
  12. /**
  13. * Class FormComponentDriver
  14. *
  15. * @package FormBuilder
  16. */
  17. abstract class FormComponentDriver implements FormComponentInterFace
  18. {
  19. use CallPropsTrait;
  20. /**
  21. * 字段名
  22. *
  23. * @var String
  24. */
  25. protected $field;
  26. /**
  27. * 字段昵称
  28. *
  29. * @var String
  30. */
  31. protected $title;
  32. /**
  33. * 组件名称
  34. *
  35. * @var String
  36. */
  37. protected $name;
  38. /**
  39. * 组件的规则
  40. *
  41. * @var array
  42. */
  43. protected $props = [];
  44. /**
  45. * info
  46. *
  47. * @var String
  48. */
  49. public $info;
  50. /**
  51. * 字段的值
  52. *
  53. * @var
  54. */
  55. protected $value = '';
  56. /**
  57. * 栅格规则
  58. *
  59. * @var array
  60. */
  61. protected $col = [];
  62. /**
  63. * 字段验证规则
  64. *
  65. * @var array
  66. */
  67. protected $validate = [];
  68. protected $validateHandler = null;
  69. /**
  70. * 组件属性设置规则
  71. *
  72. * @var array
  73. */
  74. protected static $propsRule = [];
  75. /**
  76. * FormComponentDriver constructor.
  77. *
  78. * @param String $field 字段名
  79. * @param String $title 字段昵称
  80. * @param String $value 字段值
  81. */
  82. public function __construct($field, $title, $value = null)
  83. {
  84. $this->field = (string)$field;
  85. $this->title = (string)$title;
  86. static::value($value);
  87. static::init();
  88. }
  89. /**
  90. * 组件初始化
  91. */
  92. protected function init()
  93. {
  94. }
  95. /**
  96. * @param $span
  97. * @return $this
  98. */
  99. public function col($span)
  100. {
  101. if ($span instanceof Col)
  102. $this->col = $span->build();
  103. else if (is_numeric($span))
  104. $this->col['span'] = $span;
  105. return $this;
  106. }
  107. /**
  108. * 批量设置组件的规则
  109. *
  110. * @param array $props
  111. * @return $this
  112. */
  113. public function setProps(array $props = [])
  114. {
  115. foreach ($props as $k => $v) {
  116. $this->{$k}($v);
  117. }
  118. return $this;
  119. }
  120. /**
  121. * 获取组件的规则
  122. *
  123. * @param $name
  124. * @return mixed|null
  125. */
  126. public function getProps($name)
  127. {
  128. return isset($this->props[$name]) ? $this->props[$name] : null;
  129. }
  130. /**
  131. * 设置组件的值
  132. *
  133. * @param $value
  134. * @return $this
  135. */
  136. public function value($value)
  137. {
  138. if (is_null($value)) $value = '';
  139. $this->value = (string)$value;
  140. return $this;
  141. }
  142. /**
  143. * 设置组件的info
  144. *
  145. * @param string $info
  146. * @return $this
  147. */
  148. public function info($info)
  149. {
  150. $this->info = (string)$info;
  151. return $this;
  152. }
  153. /**
  154. * 获取组件的值
  155. *
  156. * @return string
  157. */
  158. public function getValue()
  159. {
  160. return $this->value;
  161. }
  162. /**
  163. * 获取组件的字段名
  164. *
  165. * @return String
  166. */
  167. public function getField()
  168. {
  169. return $this->field;
  170. }
  171. /**
  172. * 设置组件的昵称
  173. *
  174. * @return String
  175. */
  176. public function getTitle()
  177. {
  178. return $this->title;
  179. }
  180. /**
  181. * @param string|null $message
  182. * @return $this
  183. */
  184. public function required($message = null)
  185. {
  186. $this->validate()->required($message ?: $this->getPlaceHolder());
  187. $this->props['required'] = true;
  188. return $this;
  189. }
  190. /**
  191. * @param string $pre
  192. * @return string
  193. */
  194. protected function getPlaceHolder($pre = '请选择')
  195. {
  196. return $pre . $this->title;
  197. }
  198. /**
  199. * 添加验证规则
  200. *
  201. * @param array $validate
  202. * @return $this
  203. */
  204. public function validateAs(array $validate)
  205. {
  206. $this->validate = array_merge($this->validate, $validate);
  207. return $this;
  208. }
  209. /**
  210. * @return Validate
  211. */
  212. abstract protected function getValidateHandler();
  213. /**
  214. * @param bool $clear
  215. * @return Validate
  216. */
  217. public function validate($clear = false)
  218. {
  219. if ($clear || is_null($this->validateHandler))
  220. $this->validateHandler = $this->getValidateHandler();
  221. return $this->validateHandler;
  222. }
  223. public function validateFn($callback, $clear = false)
  224. {
  225. if (is_callable($callback)) $callback($this->validate($clear));
  226. return $this;
  227. }
  228. public function setValidate(Validate $validate)
  229. {
  230. $this->validateHandler = $validate;
  231. return $this;
  232. }
  233. }