1 | <?php |
||
15 | abstract class AbstractForm implements FormInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $data = []; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private $types = []; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $validators = []; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $cleanup = []; |
||
36 | |||
37 | 13 | public function __construct() |
|
47 | |||
48 | /** |
||
49 | * @return static |
||
50 | */ |
||
51 | 1 | public static function create() |
|
55 | |||
56 | /** |
||
57 | * @return array |
||
58 | */ |
||
59 | abstract protected function fields(): array; |
||
60 | |||
61 | /** |
||
62 | * {@inheritDoc} |
||
63 | */ |
||
64 | public function setData(array $data) |
||
73 | |||
74 | /** |
||
75 | * {@inheritDoc} |
||
76 | */ |
||
77 | public function getData(): array |
||
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | 7 | public function handleRequest(RequestInterface $request) |
|
101 | |||
102 | /** |
||
103 | * {@inheritDoc} |
||
104 | */ |
||
105 | 2 | public function validate() |
|
115 | |||
116 | /** |
||
117 | * @param array $data |
||
118 | * @return AbstractForm |
||
119 | */ |
||
120 | 6 | protected function fetchRequestData(array $data) |
|
143 | |||
144 | /** |
||
145 | * @param array $fields |
||
146 | */ |
||
147 | private function parseConfiguration(array $fields) |
||
148 | { |
||
149 | 12 | array_walk($fields, function (array $config, string $field) { |
|
150 | 12 | $this->assertFieldConfigHasValidators($field, $config); |
|
151 | 11 | $this->validators[$field] = $config['validators']; |
|
152 | 11 | if (isset($config['type'])) { |
|
153 | 11 | $this->types[$field] = $config['type']; |
|
154 | } |
||
155 | 11 | if (isset($config['cleanup']) && $config['cleanup'] === true) { |
|
156 | 10 | $this->cleanup[$field] = true; |
|
157 | } |
||
158 | 12 | }); |
|
159 | 11 | } |
|
160 | |||
161 | /** |
||
162 | * @param string $field |
||
163 | * @param array $config |
||
164 | */ |
||
165 | 12 | private function assertFieldConfigHasValidators(string $field, array $config) |
|
166 | { |
||
167 | 12 | if (!isset($config['validators']) || !is_array($config['validators'])) { |
|
168 | 1 | throw new ConfigurationException('fields validation rules are not set: ' . $field); |
|
169 | } |
||
170 | 11 | } |
|
171 | |||
172 | /** |
||
173 | * @return array |
||
174 | */ |
||
175 | 2 | private function buildValidationRules(): array |
|
191 | } |