1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Micro framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Stanislau Komar <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Micro\Library\DTO\Preparation\Processor\Property; |
15
|
|
|
|
16
|
|
|
use Micro\Library\DTO\ClassDef\ClassDefinition; |
17
|
|
|
use Micro\Library\DTO\ClassDef\PropertyDefinition; |
18
|
|
|
|
19
|
|
|
class AttributeValidationProcessor implements PropertyProcessorInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param iterable<PropertyProcessorInterface> $validatorProcessor |
23
|
|
|
*/ |
24
|
6 |
|
public function __construct( |
25
|
|
|
private iterable $validatorProcessor |
26
|
|
|
) { |
27
|
6 |
|
} |
28
|
|
|
|
29
|
1 |
|
public function process(PropertyDefinition $propertyDefinition, ClassDefinition $classDefinition, array $propertyData, array $classList): void |
30
|
|
|
{ |
31
|
1 |
|
if (!\array_key_exists('validation', $propertyData)) { |
32
|
1 |
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
$propertyValidationConstraints = $propertyData['validation']; |
36
|
1 |
|
foreach ($propertyValidationConstraints as $constraint) { |
37
|
1 |
|
$this->processAddConstraints($propertyDefinition, $classDefinition, $constraint, $classList); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param PropertyDefinition $propertyDefinition |
43
|
|
|
* @param ClassDefinition $classDefinition |
44
|
|
|
* @param array<array<string, mixed>> $constraints |
45
|
|
|
* @param string[] $classList |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
1 |
|
protected function processAddConstraints(PropertyDefinition $propertyDefinition, ClassDefinition $classDefinition, array $constraints, array $classList): void |
50
|
|
|
{ |
51
|
1 |
|
foreach ($constraints as $constraintName => $constraintConfigs) { |
52
|
1 |
|
$this->processAddConstraint($propertyDefinition, $classDefinition, $constraintName, $constraintConfigs, $classList); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param PropertyDefinition $propertyDefinition |
58
|
|
|
* @param ClassDefinition $classDefinition |
59
|
|
|
* @param string $constraintName |
60
|
|
|
* @param array<array<string, mixed>> $constraintConfigs |
61
|
|
|
* @param string[] $classList |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
1 |
|
protected function processAddConstraint( |
66
|
|
|
PropertyDefinition $propertyDefinition, |
67
|
|
|
ClassDefinition $classDefinition, |
68
|
|
|
string $constraintName, |
69
|
|
|
array $constraintConfigs, |
70
|
|
|
array $classList |
71
|
|
|
): void { |
72
|
1 |
|
foreach ($constraintConfigs as $config) { |
73
|
1 |
|
foreach ($this->validatorProcessor as $processor) { |
74
|
|
|
/** |
75
|
|
|
* @psalm-suppress InvalidArrayOffset |
76
|
|
|
*/ |
77
|
1 |
|
$processor->process($propertyDefinition, $classDefinition, [$constraintName => $config], $classList); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|