|
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\Constraints\NotBlank; |
|
20
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
|
21
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
|
22
|
|
|
use Symfony\Component\Validator\Validation; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @Annotation |
|
26
|
|
|
*/ |
|
27
|
|
|
class PropertyConstraintsValidator |
|
28
|
|
|
{ |
|
29
|
|
|
public const ALLOWED_RULES = [ |
|
30
|
|
|
Rule::REQUIRED, |
|
31
|
|
|
Rule::MINLENGTH, |
|
32
|
|
|
Rule::MAXLENGTH, |
|
33
|
|
|
Rule::LENGTH, |
|
34
|
|
|
Rule::MINCHECK, |
|
35
|
|
|
Rule::MAXCHECK, |
|
36
|
|
|
Rule::CHECK, |
|
37
|
|
|
Rule::MIN, |
|
38
|
|
|
Rule::MAX, |
|
39
|
|
|
Rule::EQUALTO, |
|
40
|
|
|
Rule::TYPE, |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param mixed $constraints |
|
45
|
|
|
*/ |
|
46
|
41 |
|
public function validate($constraints): ConstraintViolationListInterface |
|
47
|
|
|
{ |
|
48
|
41 |
|
$violations = new ConstraintViolationList(); |
|
49
|
|
|
|
|
50
|
41 |
|
if (empty($constraints)) { |
|
51
|
5 |
|
return $violations; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
38 |
|
$validator = Validation::createValidator(); |
|
55
|
|
|
|
|
56
|
38 |
|
foreach ($constraints as $rule => $options) { |
|
57
|
38 |
|
if ($rule === '') { |
|
58
|
2 |
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
38 |
|
if (\is_string($options) && $options === Rule::REQUIRED) { |
|
62
|
1 |
|
$rule = $options; |
|
63
|
1 |
|
$options = true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
38 |
|
$errors = $validator->validate($rule, [ |
|
67
|
38 |
|
new NotBlank(), |
|
68
|
38 |
|
new Choice(self::ALLOWED_RULES), |
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
38 |
|
if (\count($errors) === 0) { |
|
72
|
34 |
|
$errors = $this->validateRule($rule, $options); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
38 |
|
if (\count($errors) !== 0) { |
|
76
|
17 |
|
foreach ($errors as $error) { |
|
77
|
38 |
|
$violations->add($error); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
38 |
|
return $violations; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param mixed $options |
|
87
|
|
|
*/ |
|
88
|
34 |
|
private function validateRule(string $rule, $options): ConstraintViolationListInterface |
|
89
|
|
|
{ |
|
90
|
34 |
|
$errors = new ConstraintViolationList(); |
|
91
|
|
|
|
|
92
|
|
|
switch ($rule) { |
|
93
|
34 |
|
case Rule::REQUIRED: |
|
94
|
8 |
|
$errors = (new RequiredConstraintValidator())->validate($options); |
|
95
|
|
|
|
|
96
|
8 |
|
break; |
|
97
|
31 |
|
case Rule::MAX: |
|
98
|
29 |
|
case Rule::MAXLENGTH: |
|
99
|
27 |
|
case Rule::MAXCHECK: |
|
100
|
11 |
|
$errors = (new MaxConstraintValidator())->validate($options); |
|
101
|
|
|
|
|
102
|
11 |
|
break; |
|
103
|
25 |
|
case Rule::MIN: |
|
104
|
22 |
|
case Rule::MINLENGTH: |
|
105
|
19 |
|
case Rule::MINCHECK: |
|
106
|
13 |
|
$errors = (new MinConstraintValidator())->validate($options); |
|
107
|
|
|
|
|
108
|
13 |
|
break; |
|
109
|
16 |
|
case Rule::EQUALTO: |
|
110
|
3 |
|
$errors = (new EqualToConstraintValidator())->validate($options); |
|
111
|
|
|
|
|
112
|
3 |
|
break; |
|
113
|
14 |
|
case Rule::TYPE: |
|
114
|
8 |
|
$errors = (new PropertyTypeValidator())->validate($options); |
|
115
|
|
|
|
|
116
|
8 |
|
break; |
|
117
|
7 |
|
case Rule::LENGTH: |
|
118
|
3 |
|
case Rule::CHECK: |
|
119
|
7 |
|
$errors = (new RangeConstraintValidator())->validate($options); |
|
120
|
|
|
|
|
121
|
7 |
|
break; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
34 |
|
return $errors; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|