You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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