1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of FlexPHP. |
4
|
|
|
* |
5
|
|
|
* (c) Freddie Gar <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace FlexPHP\Schema\Validators; |
11
|
|
|
|
12
|
|
|
use FlexPHP\Schema\Constants\Rule; |
13
|
|
|
use FlexPHP\Schema\Validators\Constraints\EqualToConstraintValidator; |
14
|
|
|
use FlexPHP\Schema\Validators\Constraints\MaxConstraintValidator; |
15
|
|
|
use FlexPHP\Schema\Validators\Constraints\MinConstraintValidator; |
16
|
|
|
use FlexPHP\Schema\Validators\Constraints\RangeConstraintValidator; |
17
|
|
|
use FlexPHP\Schema\Validators\Constraints\RequiredConstraintValidator; |
18
|
|
|
use Symfony\Component\Validator\Constraints\Choice; |
19
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
20
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
21
|
|
|
use Symfony\Component\Validator\Validation; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @Annotation |
25
|
|
|
*/ |
26
|
|
|
class PropertyConstraintsValidator |
27
|
|
|
{ |
28
|
|
|
public const ALLOWED_RULES = [ |
29
|
|
|
Rule::REQUIRED, |
30
|
|
|
Rule::MINLENGTH, |
31
|
|
|
Rule::MAXLENGTH, |
32
|
|
|
Rule::LENGTH, |
33
|
|
|
Rule::MINCHECK, |
34
|
|
|
Rule::MAXCHECK, |
35
|
|
|
Rule::CHECK, |
36
|
|
|
Rule::MIN, |
37
|
|
|
Rule::MAX, |
38
|
|
|
Rule::EQUALTO, |
39
|
|
|
Rule::TYPE, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array<string, mixed> $constraints |
44
|
|
|
*/ |
45
|
86 |
|
public function validate(array $constraints): ConstraintViolationListInterface |
46
|
|
|
{ |
47
|
86 |
|
$violations = new ConstraintViolationList(); |
48
|
|
|
|
49
|
86 |
|
$validator = Validation::createValidator(); |
50
|
|
|
|
51
|
86 |
|
foreach ($constraints as $rule => $options) { |
52
|
75 |
|
$errors = $validator->validate($rule, [ |
53
|
75 |
|
new Choice(self::ALLOWED_RULES), |
54
|
|
|
]); |
55
|
|
|
|
56
|
75 |
|
if (!\count($errors)) { |
57
|
69 |
|
$errors = $this->validateRule($rule, $options); |
58
|
|
|
} |
59
|
|
|
|
60
|
75 |
|
$violations->addAll($errors); |
61
|
|
|
} |
62
|
|
|
|
63
|
86 |
|
return $violations; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param mixed $options |
68
|
|
|
*/ |
69
|
69 |
|
private function validateRule(string $rule, $options): ConstraintViolationListInterface |
70
|
|
|
{ |
71
|
69 |
|
$errors = new ConstraintViolationList(); |
72
|
|
|
|
73
|
|
|
switch ($rule) { |
74
|
69 |
|
case Rule::REQUIRED: |
75
|
19 |
|
$errors = (new RequiredConstraintValidator())->validate($options); |
76
|
|
|
|
77
|
19 |
|
break; |
78
|
57 |
|
case Rule::MAX: |
79
|
52 |
|
case Rule::MAXLENGTH: |
80
|
47 |
|
case Rule::MAXCHECK: |
81
|
22 |
|
$errors = (new MaxConstraintValidator())->validate($options); |
82
|
|
|
|
83
|
22 |
|
break; |
84
|
42 |
|
case Rule::MIN: |
85
|
35 |
|
case Rule::MINLENGTH: |
86
|
29 |
|
case Rule::MINCHECK: |
87
|
24 |
|
$errors = (new MinConstraintValidator())->validate($options); |
88
|
|
|
|
89
|
24 |
|
break; |
90
|
23 |
|
case Rule::EQUALTO: |
91
|
7 |
|
$errors = (new EqualToConstraintValidator())->validate($options); |
92
|
|
|
|
93
|
7 |
|
break; |
94
|
19 |
|
case Rule::TYPE: |
95
|
10 |
|
$errors = (new PropertyTypeValidator())->validate($options); |
96
|
|
|
|
97
|
10 |
|
break; |
98
|
9 |
|
case Rule::LENGTH: |
99
|
3 |
|
case Rule::CHECK: |
100
|
9 |
|
$errors = (new RangeConstraintValidator())->validate($options); |
101
|
|
|
|
102
|
9 |
|
break; |
103
|
|
|
} |
104
|
|
|
|
105
|
69 |
|
return $errors; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|