BaseRule.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /**
  3. * PHP表单生成器
  4. *
  5. * @package FormBuilder
  6. * @author xaboy <xaboy2005@qq.com>
  7. * @version 2.0
  8. * @license MIT
  9. * @link https://github.com/xaboy/form-builder
  10. * @document http://php.form-create.com
  11. */
  12. namespace FormBuilder\Rule;
  13. use FormBuilder\Contract\ColComponentInterface;
  14. trait BaseRule
  15. {
  16. /**
  17. * 组件类型
  18. *
  19. * @var string
  20. */
  21. protected $type;
  22. /**
  23. * 组件字段名
  24. *
  25. * @var string
  26. */
  27. protected $field;
  28. /**
  29. * 字段昵称
  30. *
  31. * @var string
  32. */
  33. protected $title;
  34. /**
  35. * 组件名称
  36. *
  37. * @var string
  38. */
  39. protected $name;
  40. /**
  41. * 组件的提示信息
  42. *
  43. * @var string
  44. */
  45. protected $info;
  46. /**
  47. * 组件 class
  48. *
  49. * @var string
  50. */
  51. protected $className;
  52. /**
  53. * 是否原样生成组件,不嵌套的FormItem中
  54. *
  55. * @var bool
  56. */
  57. protected $native;
  58. /**
  59. * 事件注入时的自定义数据
  60. *
  61. * @var mixed
  62. */
  63. protected $inject;
  64. /**
  65. * 组件布局规则
  66. *
  67. * @var ColComponentInterface|array
  68. */
  69. protected $col;
  70. /**
  71. * 组件的值
  72. *
  73. * @var mixed
  74. */
  75. protected $value = '';
  76. /**
  77. * 组件显示状态
  78. *
  79. * @var bool
  80. */
  81. protected $hidden;
  82. /**
  83. * 组件显示状态
  84. *
  85. * @var bool
  86. */
  87. protected $visibility;
  88. /**
  89. * @var array
  90. */
  91. protected $effect;
  92. /**
  93. * 组件显示状态
  94. *
  95. * @param bool $hidden
  96. * @return $this
  97. */
  98. public function hiddenStatus($hidden = true)
  99. {
  100. $this->hidden = !!$hidden;
  101. return $this;
  102. }
  103. /**
  104. * 组件显示状态
  105. *
  106. * @param bool $visibility
  107. * @return $this
  108. */
  109. public function visibilityStatus($visibility = true)
  110. {
  111. $this->visibility = !!$visibility;
  112. return $this;
  113. }
  114. /**
  115. * @param string $type
  116. * @return $this
  117. */
  118. protected function setRuleType($type)
  119. {
  120. $this->type = (string)$type;
  121. return $this;
  122. }
  123. /**
  124. * @param string $field
  125. * @return $this
  126. */
  127. public function field($field)
  128. {
  129. $this->field = (string)$field;
  130. return $this;
  131. }
  132. /**
  133. * @param string $title
  134. * @return $this
  135. */
  136. public function title($title)
  137. {
  138. $this->title = (string)$title;
  139. return $this;
  140. }
  141. /**
  142. * @param string $name
  143. * @return $this
  144. */
  145. public function name($name)
  146. {
  147. $this->name = (string)$name;
  148. return $this;
  149. }
  150. /**
  151. * @param string $className
  152. * @return $this
  153. */
  154. public function className($className)
  155. {
  156. $this->className = (string)$className;
  157. return $this;
  158. }
  159. /**
  160. * @param bool $native
  161. * @return $this
  162. */
  163. public function native($native)
  164. {
  165. $this->native = !!$native;
  166. return $this;
  167. }
  168. /**
  169. * @param mixed $inject
  170. * @return $this
  171. */
  172. public function inject($inject)
  173. {
  174. $this->inject = $inject;
  175. return $this;
  176. }
  177. /**
  178. * @param string $info
  179. * @return $this
  180. */
  181. public function info($info)
  182. {
  183. $this->info = $info;
  184. return $this;
  185. }
  186. /**
  187. * @param array|int|ColComponentInterface $col
  188. * @return $this
  189. */
  190. public function col($col)
  191. {
  192. if (is_integer($col)) $col = ['span' => $col];
  193. $this->col = $col;
  194. return $this;
  195. }
  196. public function effect(array $effect)
  197. {
  198. $this->effect = $effect;
  199. return $this;
  200. }
  201. /**
  202. * @param mixed $value
  203. * @return $this
  204. */
  205. public function value($value)
  206. {
  207. if (is_null($value)) $value = '';
  208. $this->value = $value;
  209. return $this;
  210. }
  211. public function getHiddenStatus()
  212. {
  213. return $this->hidden;
  214. }
  215. public function getVisibilityStatus()
  216. {
  217. return $this->visibility;
  218. }
  219. public function getField()
  220. {
  221. return $this->field;
  222. }
  223. public function getTitle()
  224. {
  225. return $this->title;
  226. }
  227. public function getName()
  228. {
  229. return $this->name;
  230. }
  231. public function getClassName()
  232. {
  233. return $this->className;
  234. }
  235. public function getInfo()
  236. {
  237. return $this->info;
  238. }
  239. public function getNative()
  240. {
  241. return $this->native;
  242. }
  243. public function getInject()
  244. {
  245. return $this->inject;
  246. }
  247. public function getCol()
  248. {
  249. return $this->col;
  250. }
  251. public function getEffect()
  252. {
  253. return $this->effect;
  254. }
  255. public function getValue()
  256. {
  257. return $this->value;
  258. }
  259. protected function parseCol($col)
  260. {
  261. return $col instanceof ColComponentInterface ? $col->getCol() : $col;
  262. }
  263. protected function parseBaseRule()
  264. {
  265. $rule = [
  266. 'type' => $this->type
  267. ];
  268. if (!is_null($this->field))
  269. $rule['field'] = $this->field;
  270. if (!is_null($this->value))
  271. $rule['value'] = $this->value;
  272. if (!is_null($this->title))
  273. $rule['title'] = $this->title;
  274. if (!is_null($this->className))
  275. $rule['className'] = $this->className;
  276. if (!is_null($this->name))
  277. $rule['name'] = $this->name;
  278. if (!is_null($this->native))
  279. $rule['native'] = $this->native;
  280. if (!is_null($this->info))
  281. $rule['info'] = $this->info;
  282. if (!is_null($this->effect))
  283. $rule['effect'] = $this->effect;
  284. if (!is_null($this->inject))
  285. $rule['inject'] = $this->inject;
  286. if (!is_null($this->hidden))
  287. $rule['hidden'] = $this->hidden;
  288. if (!is_null($this->visibility))
  289. $rule['visibility'] = $this->visibility;
  290. if (!is_null($this->col))
  291. $rule['col'] = $this->parseCol($this->col);
  292. return $rule;
  293. }
  294. }