1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
use ReflectionNamedType; |
11
|
|
|
use Stringable; |
12
|
|
|
use Yiisoft\Strings\Inflector; |
13
|
|
|
use Yiisoft\Strings\StringHelper; |
14
|
|
|
use Yiisoft\Validator\PostValidationHookInterface; |
15
|
|
|
use Yiisoft\Validator\Result; |
16
|
|
|
use Yiisoft\Validator\RulesProviderInterface; |
17
|
|
|
|
18
|
|
|
use function array_key_exists; |
19
|
|
|
use function explode; |
20
|
|
|
use function is_subclass_of; |
21
|
|
|
use function property_exists; |
22
|
|
|
use function strpos; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Form model represents an HTML form: its data, validation and presentation. |
26
|
|
|
*/ |
27
|
|
|
abstract class FormModel implements FormModelInterface, PostValidationHookInterface, RulesProviderInterface |
28
|
|
|
{ |
29
|
|
|
private string $formErrorsClass = FormErrors::class; |
30
|
|
|
private array $attributes; |
31
|
|
|
private FormErrorsInterface $formErrors; |
32
|
|
|
private ?Inflector $inflector = null; |
33
|
|
|
private bool $validated = false; |
34
|
|
|
|
35
|
771 |
|
public function __construct() |
36
|
|
|
{ |
37
|
771 |
|
$this->attributes = $this->collectAttributes(); |
38
|
771 |
|
$this->formErrors = $this->createFormErrors($this->formErrorsClass); |
39
|
771 |
|
} |
40
|
|
|
|
41
|
1 |
|
public function attributes(): array |
42
|
|
|
{ |
43
|
1 |
|
return array_keys($this->attributes); |
44
|
|
|
} |
45
|
|
|
|
46
|
359 |
|
public function getAttributeHint(string $attribute): string |
47
|
|
|
{ |
48
|
359 |
|
$attributeHints = $this->getAttributeHints(); |
49
|
359 |
|
$hint = $attributeHints[$attribute] ?? ''; |
50
|
359 |
|
$nestedAttributeHint = $this->getNestedAttributeValue('getAttributeHint', $attribute); |
51
|
|
|
|
52
|
359 |
|
return $nestedAttributeHint !== '' ? $nestedAttributeHint : $hint; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string[] |
57
|
|
|
*/ |
58
|
344 |
|
public function getAttributeHints(): array |
59
|
|
|
{ |
60
|
344 |
|
return []; |
61
|
|
|
} |
62
|
|
|
|
63
|
379 |
|
public function getAttributeLabel(string $attribute): string |
64
|
|
|
{ |
65
|
379 |
|
$label = $this->generateAttributeLabel($attribute); |
66
|
379 |
|
$labels = $this->getAttributeLabels(); |
67
|
|
|
|
68
|
379 |
|
if (array_key_exists($attribute, $labels)) { |
69
|
2 |
|
$label = $labels[$attribute]; |
70
|
|
|
} |
71
|
|
|
|
72
|
379 |
|
$nestedAttributeLabel = $this->getNestedAttributeValue('getAttributeLabel', $attribute); |
73
|
|
|
|
74
|
379 |
|
return $nestedAttributeLabel !== '' ? $nestedAttributeLabel : $label; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string[] |
79
|
|
|
*/ |
80
|
377 |
|
public function getAttributeLabels(): array |
81
|
|
|
{ |
82
|
377 |
|
return []; |
83
|
|
|
} |
84
|
|
|
|
85
|
368 |
|
public function getAttributePlaceholder(string $attribute): string |
86
|
|
|
{ |
87
|
368 |
|
$attributePlaceHolders = $this->getAttributePlaceholders(); |
88
|
368 |
|
$placeholder = $attributePlaceHolders[$attribute] ?? ''; |
89
|
368 |
|
$nestedAttributePlaceholder = $this->getNestedAttributeValue('getAttributePlaceholder', $attribute); |
90
|
|
|
|
91
|
368 |
|
return $nestedAttributePlaceholder !== '' ? $nestedAttributePlaceholder : $placeholder; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string[] |
96
|
|
|
*/ |
97
|
366 |
|
public function getAttributePlaceholders(): array |
98
|
|
|
{ |
99
|
366 |
|
return []; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return iterable|object|scalar|Stringable|null |
104
|
|
|
*/ |
105
|
695 |
|
public function getAttributeValue(string $attribute) |
106
|
|
|
{ |
107
|
695 |
|
return $this->readProperty($attribute); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return FormErrorsInterface Get FormErrors object. |
112
|
|
|
*/ |
113
|
376 |
|
public function getFormErrors(): FormErrorsInterface |
114
|
|
|
{ |
115
|
376 |
|
return $this->formErrors; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string Returns classname without a namespace part or empty string when class is anonymous |
120
|
|
|
*/ |
121
|
734 |
|
public function getFormName(): string |
122
|
|
|
{ |
123
|
734 |
|
if (strpos(static::class, '@anonymous') !== false) { |
124
|
6 |
|
return ''; |
125
|
|
|
} |
126
|
|
|
|
127
|
729 |
|
$className = strrchr(static::class, '\\'); |
128
|
729 |
|
if ($className === false) { |
129
|
1 |
|
return static::class; |
130
|
|
|
} |
131
|
|
|
|
132
|
728 |
|
return substr($className, 1); |
133
|
|
|
} |
134
|
|
|
|
135
|
714 |
|
public function hasAttribute(string $attribute): bool |
136
|
|
|
{ |
137
|
714 |
|
[$attribute, $nested] = $this->getNestedAttribute($attribute); |
138
|
|
|
|
139
|
713 |
|
return $nested !== null ? true : array_key_exists($attribute, $this->attributes); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param array $data |
144
|
|
|
* @param string|null $formName |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
30 |
|
public function load(array $data, ?string $formName = null): bool |
149
|
|
|
{ |
150
|
30 |
|
$scope = $formName ?? $this->getFormName(); |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @psalm-var array<string, scalar|Stringable|null> |
154
|
|
|
*/ |
155
|
30 |
|
$values = []; |
156
|
|
|
|
157
|
30 |
|
if ($scope === '' && !empty($data)) { |
158
|
3 |
|
$values = $data; |
159
|
28 |
|
} elseif (isset($data[$scope])) { |
160
|
|
|
/** @var mixed */ |
161
|
27 |
|
$values = $data[$scope]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** @var array<string, scalar|Stringable|null> $values */ |
165
|
30 |
|
foreach ($values as $name => $value) { |
166
|
30 |
|
$this->setAttribute($name, $value); |
167
|
|
|
} |
168
|
|
|
|
169
|
30 |
|
return $values !== []; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param iterable|object|scalar|Stringable|null $value |
174
|
|
|
* |
175
|
|
|
* @psalm-suppress PossiblyInvalidCast |
176
|
|
|
*/ |
177
|
77 |
|
public function setAttribute(string $name, $value): void |
178
|
|
|
{ |
179
|
77 |
|
[$realName] = $this->getNestedAttribute($name); |
180
|
|
|
|
181
|
77 |
|
if (isset($this->attributes[$realName])) { |
182
|
76 |
|
switch ($this->attributes[$realName]) { |
183
|
76 |
|
case 'bool': |
184
|
7 |
|
$this->writeProperty($name, (bool) $value); |
185
|
7 |
|
break; |
186
|
76 |
|
case 'float': |
187
|
1 |
|
$this->writeProperty($name, (float) $value); |
188
|
1 |
|
break; |
189
|
76 |
|
case 'int': |
190
|
17 |
|
$this->writeProperty($name, (int) $value); |
191
|
17 |
|
break; |
192
|
74 |
|
case 'string': |
193
|
65 |
|
$this->writeProperty($name, (string) $value); |
194
|
65 |
|
break; |
195
|
|
|
default: |
196
|
13 |
|
$this->writeProperty($name, $value); |
197
|
13 |
|
break; |
198
|
|
|
} |
199
|
|
|
} |
200
|
77 |
|
} |
201
|
|
|
|
202
|
31 |
|
public function processValidationResult(Result $result): void |
203
|
|
|
{ |
204
|
31 |
|
$this->validated = false; |
205
|
|
|
|
206
|
31 |
|
foreach ($result->getErrorMessagesIndexedByAttribute() as $attribute => $errors) { |
207
|
29 |
|
if ($this->hasAttribute($attribute)) { |
208
|
29 |
|
$this->formErrors->clear($attribute); |
209
|
29 |
|
$this->addErrors([$attribute => $errors]); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
31 |
|
$this->validated = true; |
214
|
31 |
|
} |
215
|
|
|
|
216
|
537 |
|
public function getRules(): array |
217
|
|
|
{ |
218
|
537 |
|
return []; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Returns the list of attribute types indexed by attribute names. |
223
|
|
|
* |
224
|
|
|
* By default, this method returns all non-static properties of the class. |
225
|
|
|
* |
226
|
|
|
* @return array list of attribute types indexed by attribute names. |
227
|
|
|
*/ |
228
|
771 |
|
protected function collectAttributes(): array |
229
|
|
|
{ |
230
|
771 |
|
$class = new ReflectionClass($this); |
231
|
771 |
|
$attributes = []; |
232
|
|
|
|
233
|
771 |
|
foreach ($class->getProperties() as $property) { |
234
|
766 |
|
if ($property->isStatic()) { |
235
|
32 |
|
continue; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** @var ReflectionNamedType|null $type */ |
239
|
766 |
|
$type = $property->getType(); |
240
|
|
|
|
241
|
766 |
|
$attributes[$property->getName()] = $type !== null ? $type->getName() : ''; |
242
|
|
|
} |
243
|
|
|
|
244
|
771 |
|
return $attributes; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @psalm-param non-empty-array<string, non-empty-list<string>> $items |
249
|
|
|
*/ |
250
|
29 |
|
private function addErrors(array $items): void |
251
|
|
|
{ |
252
|
29 |
|
foreach ($items as $attribute => $errors) { |
253
|
29 |
|
foreach ($errors as $error) { |
254
|
29 |
|
$this->formErrors->addError($attribute, $error); |
255
|
|
|
} |
256
|
|
|
} |
257
|
29 |
|
} |
258
|
|
|
|
259
|
771 |
|
private function createFormErrors(string $formErrorsClass): FormErrorsInterface |
260
|
|
|
{ |
261
|
771 |
|
$formErrors = new $formErrorsClass(); |
262
|
|
|
|
263
|
771 |
|
if (!$formErrors instanceof FormErrorsInterface) { |
264
|
|
|
throw new InvalidArgumentException('Form errors class must implement ' . FormErrorsInterface::class); |
265
|
|
|
} |
266
|
|
|
|
267
|
771 |
|
return $formErrors; |
268
|
|
|
} |
269
|
|
|
|
270
|
379 |
|
private function getInflector(): Inflector |
271
|
|
|
{ |
272
|
379 |
|
if ($this->inflector === null) { |
273
|
379 |
|
$this->inflector = new Inflector(); |
274
|
|
|
} |
275
|
379 |
|
return $this->inflector; |
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Generates a user friendly attribute label based on the give attribute name. |
280
|
|
|
* |
281
|
|
|
* This is done by replacing underscores, dashes and dots with blanks and changing the first letter of each word to |
282
|
|
|
* upper case. |
283
|
|
|
* |
284
|
|
|
* For example, 'department_name' or 'DepartmentName' will generate 'Department Name'. |
285
|
|
|
* |
286
|
|
|
* @param string $name the column name. |
287
|
|
|
* |
288
|
|
|
* @return string the attribute label. |
289
|
|
|
*/ |
290
|
379 |
|
private function generateAttributeLabel(string $name): string |
291
|
|
|
{ |
292
|
379 |
|
return StringHelper::uppercaseFirstCharacterInEachWord( |
293
|
379 |
|
$this->getInflector()->toWords($name) |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return iterable|scalar|Stringable|null |
299
|
|
|
* |
300
|
|
|
* @psalm-suppress MixedReturnStatement |
301
|
|
|
* @psalm-suppress MixedInferredReturnType |
302
|
|
|
* @psalm-suppress MissingClosureReturnType |
303
|
|
|
*/ |
304
|
695 |
|
private function readProperty(string $attribute) |
305
|
|
|
{ |
306
|
695 |
|
$class = static::class; |
307
|
|
|
|
308
|
695 |
|
[$attribute, $nested] = $this->getNestedAttribute($attribute); |
309
|
|
|
|
310
|
694 |
|
if (!property_exists($class, $attribute)) { |
311
|
1 |
|
throw new InvalidArgumentException("Undefined property: \"$class::$attribute\"."); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** @psalm-suppress MixedMethodCall */ |
315
|
693 |
|
$getter = static fn (FormModelInterface $class, string $attribute) => $nested === null |
316
|
693 |
|
? $class->$attribute |
317
|
693 |
|
: $class->$attribute->getAttributeValue($nested); |
318
|
|
|
|
319
|
693 |
|
$getter = Closure::bind($getter, null, $this); |
320
|
|
|
|
321
|
|
|
/** @var Closure $getter */ |
322
|
693 |
|
return $getter($this, $attribute); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param string $attribute |
327
|
|
|
* @param iterable|object|scalar|Stringable|null $value |
328
|
|
|
* |
329
|
|
|
* @psalm-suppress MissingClosureReturnType |
330
|
|
|
*/ |
331
|
76 |
|
private function writeProperty(string $attribute, $value): void |
332
|
|
|
{ |
333
|
76 |
|
[$attribute, $nested] = $this->getNestedAttribute($attribute); |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @psalm-suppress MissingClosureParamType |
337
|
|
|
* @psalm-suppress MixedMethodCall |
338
|
|
|
*/ |
339
|
76 |
|
$setter = static fn (FormModelInterface $class, string $attribute, $value) => $nested === null |
340
|
76 |
|
? $class->$attribute = $value |
341
|
76 |
|
: $class->$attribute->setAttribute($nested, $value); |
342
|
|
|
|
343
|
76 |
|
$setter = Closure::bind($setter, null, $this); |
344
|
|
|
|
345
|
|
|
/** @var Closure $setter */ |
346
|
76 |
|
$setter($this, $attribute, $value); |
347
|
76 |
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @return string[] |
351
|
|
|
* |
352
|
|
|
* @psalm-return array{0: string, 1: null|string} |
353
|
|
|
*/ |
354
|
729 |
|
private function getNestedAttribute(string $attribute): array |
355
|
|
|
{ |
356
|
729 |
|
if (strpos($attribute, '.') === false) { |
357
|
727 |
|
return [$attribute, null]; |
358
|
|
|
} |
359
|
|
|
|
360
|
9 |
|
[$attribute, $nested] = explode('.', $attribute, 2); |
361
|
|
|
|
362
|
|
|
/** @var string */ |
363
|
9 |
|
$attributeNested = $this->attributes[$attribute] ?? ''; |
364
|
|
|
|
365
|
9 |
|
if (!is_subclass_of($attributeNested, self::class)) { |
366
|
1 |
|
throw new InvalidArgumentException("Attribute \"$attribute\" is not a nested attribute."); |
367
|
|
|
} |
368
|
|
|
|
369
|
8 |
|
if (!property_exists($attributeNested, $nested)) { |
370
|
1 |
|
throw new InvalidArgumentException("Undefined property: \"$attributeNested::$nested\"."); |
371
|
|
|
} |
372
|
|
|
|
373
|
7 |
|
return [$attribute, $nested]; |
374
|
|
|
} |
375
|
|
|
|
376
|
414 |
|
private function getNestedAttributeValue(string $method, string $attribute): string |
377
|
|
|
{ |
378
|
414 |
|
$result = ''; |
379
|
|
|
|
380
|
414 |
|
[$attribute, $nested] = $this->getNestedAttribute($attribute); |
381
|
|
|
|
382
|
414 |
|
if ($nested !== null) { |
383
|
|
|
/** @var FormModelInterface $attributeNestedValue */ |
384
|
3 |
|
$attributeNestedValue = $this->getAttributeValue($attribute); |
385
|
|
|
/** @var string */ |
386
|
3 |
|
$result = $attributeNestedValue->$method($nested); |
387
|
|
|
} |
388
|
|
|
|
389
|
414 |
|
return $result; |
390
|
|
|
} |
391
|
|
|
|
392
|
5 |
|
public function isValidated(): bool |
393
|
|
|
{ |
394
|
5 |
|
return $this->validated; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|