|
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
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
private $validators = [ |
|
46
|
|
|
Rule::REQUIRED => RequiredConstraintValidator::class, |
|
47
|
|
|
Rule::MAX => MaxConstraintValidator::class, |
|
48
|
|
|
Rule::MAXLENGTH => MaxConstraintValidator::class, |
|
49
|
|
|
Rule::MAXCHECK => MaxConstraintValidator::class, |
|
50
|
|
|
Rule::MIN => MinConstraintValidator::class, |
|
51
|
|
|
Rule::MINLENGTH => MinConstraintValidator::class, |
|
52
|
|
|
Rule::MINCHECK => MinConstraintValidator::class, |
|
53
|
|
|
Rule::EQUALTO => EqualToConstraintValidator::class, |
|
54
|
|
|
Rule::TYPE => PropertyTypeValidator::class, |
|
55
|
|
|
Rule::LENGTH => RangeConstraintValidator::class, |
|
56
|
|
|
Rule::CHECK => RangeConstraintValidator::class, |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param array<string, mixed> $constraints |
|
61
|
|
|
*/ |
|
62
|
92 |
|
public function validate(array $constraints): ConstraintViolationListInterface |
|
63
|
|
|
{ |
|
64
|
92 |
|
$violations = new ConstraintViolationList(); |
|
65
|
|
|
|
|
66
|
92 |
|
$validator = Validation::createValidator(); |
|
67
|
|
|
|
|
68
|
92 |
|
foreach ($constraints as $rule => $options) { |
|
69
|
79 |
|
$errors = $validator->validate($rule, [ |
|
70
|
79 |
|
new Choice([ |
|
71
|
79 |
|
'choices' => self::ALLOWED_RULES, |
|
72
|
79 |
|
'message' => 'is not valid rule.', |
|
73
|
|
|
]), |
|
74
|
|
|
]); |
|
75
|
|
|
|
|
76
|
79 |
|
if (!\count($errors)) { |
|
77
|
73 |
|
$errors = $this->validateRule($rule, $options); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
79 |
|
$violations->addAll($errors); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
92 |
|
return $violations; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param mixed $options |
|
88
|
|
|
*/ |
|
89
|
73 |
|
private function validateRule(string $rule, $options): ConstraintViolationListInterface |
|
90
|
|
|
{ |
|
91
|
73 |
|
return (new $this->validators[$rule])->validate($options); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|