Build.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\form;
  12. use crmeb\form\components\Addres;
  13. use crmeb\form\components\Alert;
  14. use crmeb\form\components\Card;
  15. use crmeb\form\components\DiyTable;
  16. use crmeb\form\components\Input;
  17. use crmeb\form\components\InputNumber;
  18. use crmeb\form\components\Map;
  19. use crmeb\form\components\Radio;
  20. use crmeb\form\components\Select;
  21. use crmeb\form\components\Switchs;
  22. use crmeb\form\components\Tabs;
  23. use crmeb\form\components\Time;
  24. use crmeb\form\components\UploadFrame;
  25. use crmeb\form\components\UploadImage;
  26. /**
  27. * Class Build
  28. * @package crmeb\form
  29. * @method Input input(string $field, string $title, $value = null)
  30. * @method Tabs tabs(array $options = [])
  31. * @method Card card(string $title)
  32. * @method InputNumber inputNum(string $field, string $title, $value = null)
  33. * @method Select select(string $field, string $title, $value = null)
  34. * @method UploadFrame uploadFrame(string $field, string $title, $value = null)
  35. * @method UploadImage uploadImage(string $field, string $title, $value = null)
  36. * @method Radio radio(string $field, string $title, $value = null)
  37. * @method Switchs switch (string $field, string $title, $value = null)
  38. * @method Alert alert (string $title, string $type = '', bool $closable = false, bool $showIcon = false)
  39. * @method DiyTable diyTable(string $field, string $title, array $value = [], array $options = [])
  40. * @method Map map(string $field, string $title, $value = null)
  41. * @method Addres addres(string $field, string $title, $value = null)
  42. * @method Time time(string $field, string $title, $value = null)
  43. */
  44. class Build
  45. {
  46. /**
  47. * 挂载组件
  48. * @var string[]
  49. */
  50. protected static $components = [
  51. 'input' => Input::class,
  52. 'tabs' => Tabs::class,
  53. 'card' => Card::class,
  54. 'inputNum' => InputNumber::class,
  55. 'select' => Select::class,
  56. 'uploadFrame' => UploadFrame::class,
  57. 'uploadImage' => UploadImage::class,
  58. 'radio' => Radio::class,
  59. 'switch' => Switchs::class,
  60. 'alert' => Alert::class,
  61. 'diyTable' => DiyTable::class,
  62. 'addres' => Addres::class,
  63. 'map' => Map::class,
  64. 'time' => Time::class,
  65. ];
  66. /**
  67. * @var array
  68. */
  69. protected $rule = [];
  70. /**
  71. * 请求地址
  72. * @var
  73. */
  74. protected $url;
  75. /**
  76. * @var string
  77. */
  78. protected $method = 'POST';
  79. /**
  80. * @var array
  81. */
  82. protected $data = [];
  83. /**
  84. * Build constructor.
  85. * @param string|null $url
  86. * @param array $rule
  87. * @param string|null $method
  88. * @param array $data
  89. */
  90. public function __construct(string $url = null, array $rule = [], string $method = null, array $data = [])
  91. {
  92. $this->url = $url;
  93. $this->rule = $rule;
  94. $this->method = $method ?: 'POST';
  95. $this->data = $data;
  96. }
  97. /**
  98. * @param array $rule
  99. * @return $this
  100. */
  101. public function rule(array $rule = [])
  102. {
  103. $this->rule = $rule;
  104. return $this;
  105. }
  106. /**
  107. * @param string $url
  108. * @return $this
  109. */
  110. public function url(string $url)
  111. {
  112. $this->url = $url;
  113. return $this;
  114. }
  115. /**
  116. * @param string $method
  117. * @return $this
  118. */
  119. public function method(string $method)
  120. {
  121. $this->method = $method;
  122. return $this;
  123. }
  124. /**
  125. * @param array $data
  126. * @return Build
  127. */
  128. public function data(array $data)
  129. {
  130. $this->data = $data;
  131. return $this;
  132. }
  133. /**
  134. * 批量设置数据
  135. * @param $rule
  136. * @return mixed
  137. */
  138. public function setValue($rule)
  139. {
  140. if (!$this->data) {
  141. return $rule;
  142. }
  143. foreach ($rule as &$value) {
  144. if (isset($value['value']) && $value['value'] !== '' && isset($value['field'])) {
  145. $value['value'] = $this->data[$value['field']];
  146. }
  147. if (isset($value['options']) && $value['options']) {
  148. foreach ($value['options'] as $i => $option) {
  149. if (isset($option['componentsModel']) && $option['componentsModel']) {
  150. $value['options'][$i] = $this->setValue($option['componentsModel']);
  151. }
  152. }
  153. }
  154. if (isset($value['control']) && $value['control']) {
  155. foreach ($value['control'] as $ii => $control) {
  156. if (isset($control['componentsModel']) && $control['componentsModel']) {
  157. $value['control'][$ii] = $this->setValue($control['componentsModel']);
  158. }
  159. }
  160. }
  161. if (isset($value['componentsModel']) && $value['componentsModel']) {
  162. $value['componentsModel'] = $this->setValue($value['componentsModel']);
  163. }
  164. }
  165. return $rule;
  166. }
  167. /**
  168. * 提取验证值
  169. * @param $rule
  170. * @return array
  171. */
  172. protected function getValidate($rule)
  173. {
  174. $validate = [];
  175. foreach ($rule as $value) {
  176. if (isset($value['field']) && isset($value['validate']) && $value['validate']) {
  177. $validate[$value['field']] = $value['validate'];
  178. }
  179. if (isset($value['options']) && $value['options']) {
  180. foreach ($value['options'] as $option) {
  181. if (isset($option['componentsModel']) && $option['componentsModel']) {
  182. $validate = array_merge($validate, $this->getValidate($option['componentsModel']));
  183. }
  184. }
  185. }
  186. if (isset($value['control']) && $value['control']) {
  187. foreach ($value['control'] as $control) {
  188. if (isset($control['componentsModel']) && $control['componentsModel']) {
  189. $validate = array_merge($validate, $this->getValidate($control['componentsModel']));
  190. }
  191. }
  192. }
  193. if (isset($value['componentsModel']) && $value['componentsModel']) {
  194. $validate = array_merge($validate, $this->getValidate($value['componentsModel']));
  195. }
  196. }
  197. return $validate;
  198. }
  199. /**
  200. * @return array
  201. */
  202. public function toArray()
  203. {
  204. $rule = [];
  205. foreach ($this->rule as $item) {
  206. if ($item instanceof BuildInterface) {
  207. $rule[] = $item->toArray();
  208. }
  209. }
  210. $data = [
  211. 'rules' => $this->setValue($rule),
  212. 'validate' => $this->getValidate($rule),
  213. 'url' => $this->url,
  214. 'method' => $this->method
  215. ];
  216. $data['validate'] = $data['validate'] ?: (object)[];
  217. $this->url = null;
  218. $this->rule = [];
  219. $this->method = 'POST';
  220. return $data;
  221. }
  222. /**
  223. * @return false|string
  224. */
  225. public function toString()
  226. {
  227. return json_encode($this->toArray());
  228. }
  229. /**
  230. * @param $name
  231. * @param $arguments
  232. * @return mixed
  233. */
  234. public static function __callStatic($name, $arguments)
  235. {
  236. $compKeys = array_keys(self::$components);
  237. if (in_array($name, $compKeys)) {
  238. return new self::$components[$name](...$arguments);
  239. }
  240. throw new BuildException('Method does not exist');
  241. }
  242. }