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