123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- namespace crmeb\form;
- use crmeb\form\components\Addres;
- use crmeb\form\components\Alert;
- use crmeb\form\components\Card;
- use crmeb\form\components\DiyTable;
- use crmeb\form\components\Input;
- use crmeb\form\components\InputNumber;
- use crmeb\form\components\Map;
- use crmeb\form\components\Radio;
- use crmeb\form\components\Select;
- use crmeb\form\components\Switchs;
- use crmeb\form\components\Tabs;
- use crmeb\form\components\Time;
- use crmeb\form\components\UploadFrame;
- use crmeb\form\components\UploadImage;
- class Build
- {
-
- protected static $components = [
- 'input' => Input::class,
- 'tabs' => Tabs::class,
- 'card' => Card::class,
- 'inputNum' => InputNumber::class,
- 'select' => Select::class,
- 'uploadFrame' => UploadFrame::class,
- 'uploadImage' => UploadImage::class,
- 'radio' => Radio::class,
- 'switch' => Switchs::class,
- 'alert' => Alert::class,
- 'diyTable' => DiyTable::class,
- 'addres' => Addres::class,
- 'map' => Map::class,
- 'time' => Time::class,
- ];
-
- protected $rule = [];
-
- protected $url;
-
- protected $method = 'POST';
-
- protected $data = [];
-
- public function __construct(string $url = null, array $rule = [], string $method = null, array $data = [])
- {
- $this->url = $url;
- $this->rule = $rule;
- $this->method = $method ?: 'POST';
- $this->data = $data;
- }
-
- public function rule(array $rule = [])
- {
- $this->rule = $rule;
- return $this;
- }
-
- public function url(string $url)
- {
- $this->url = $url;
- return $this;
- }
-
- public function method(string $method)
- {
- $this->method = $method;
- return $this;
- }
-
- public function data(array $data)
- {
- $this->data = $data;
- return $this;
- }
-
- public function setValue($rule)
- {
- if (!$this->data) {
- return $rule;
- }
- foreach ($rule as &$value) {
- if (isset($value['value']) && $value['value'] !== '' && isset($value['field'])) {
- $value['value'] = $this->data[$value['field']];
- }
- if (isset($value['options']) && $value['options']) {
- foreach ($value['options'] as $i => $option) {
- if (isset($option['componentsModel']) && $option['componentsModel']) {
- $value['options'][$i] = $this->setValue($option['componentsModel']);
- }
- }
- }
- if (isset($value['control']) && $value['control']) {
- foreach ($value['control'] as $ii => $control) {
- if (isset($control['componentsModel']) && $control['componentsModel']) {
- $value['control'][$ii] = $this->setValue($control['componentsModel']);
- }
- }
- }
- if (isset($value['componentsModel']) && $value['componentsModel']) {
- $value['componentsModel'] = $this->setValue($value['componentsModel']);
- }
- }
- return $rule;
- }
-
- protected function getValidate($rule)
- {
- $validate = [];
- foreach ($rule as $value) {
- if (isset($value['field']) && isset($value['validate']) && $value['validate']) {
- $validate[$value['field']] = $value['validate'];
- }
- if (isset($value['options']) && $value['options']) {
- foreach ($value['options'] as $option) {
- if (isset($option['componentsModel']) && $option['componentsModel']) {
- $validate = array_merge($validate, $this->getValidate($option['componentsModel']));
- }
- }
- }
- if (isset($value['control']) && $value['control']) {
- foreach ($value['control'] as $control) {
- if (isset($control['componentsModel']) && $control['componentsModel']) {
- $validate = array_merge($validate, $this->getValidate($control['componentsModel']));
- }
- }
- }
- if (isset($value['componentsModel']) && $value['componentsModel']) {
- $validate = array_merge($validate, $this->getValidate($value['componentsModel']));
- }
- }
- return $validate;
- }
-
- public function toArray()
- {
- $rule = [];
- foreach ($this->rule as $item) {
- if ($item instanceof BuildInterface) {
- $rule[] = $item->toArray();
- }
- }
- $data = [
- 'rules' => $this->setValue($rule),
- 'validate' => $this->getValidate($rule),
- 'url' => $this->url,
- 'method' => $this->method
- ];
- $data['validate'] = $data['validate'] ?: (object)[];
- $this->url = null;
- $this->rule = [];
- $this->method = 'POST';
- return $data;
- }
-
- public function toString()
- {
- return json_encode($this->toArray());
- }
-
- public static function __callStatic($name, $arguments)
- {
- $compKeys = array_keys(self::$components);
- if (in_array($name, $compKeys)) {
- return new self::$components[$name](...$arguments);
- }
- throw new BuildException('Method does not exist');
- }
- }
|