1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Albert221\Validation; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
10
|
|
|
|
11
|
|
|
class Validator |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var RuleValidatorFactory |
15
|
|
|
*/ |
16
|
|
|
private $ruleValidatorFactory; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Field[] |
20
|
|
|
*/ |
21
|
|
|
private $fields = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return Validator |
25
|
|
|
*/ |
26
|
2 |
|
public static function build() |
27
|
|
|
{ |
28
|
2 |
|
return new self(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Validator constructor. |
33
|
|
|
*/ |
34
|
2 |
|
public function __construct() |
35
|
|
|
{ |
36
|
2 |
|
$this->ruleValidatorFactory = new RuleValidatorFactory(); |
37
|
2 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return Field[] |
41
|
|
|
*/ |
42
|
|
|
public function getFields(): array |
43
|
|
|
{ |
44
|
|
|
return $this->fields; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Merges given Validator fields with already existing ones. |
49
|
|
|
* NOTE: It does not merge single rules in fields but the fields as whole. |
50
|
|
|
* |
51
|
|
|
* @param Validator $validatorBuilder |
52
|
|
|
* |
53
|
|
|
* @return Validator |
54
|
|
|
*/ |
55
|
|
|
public function merge(Validator $validatorBuilder): self |
56
|
|
|
{ |
57
|
|
|
foreach ($validatorBuilder->getFields() as $field) { |
58
|
|
|
$this->addRawField($field); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Field $field |
66
|
2 |
|
* |
67
|
|
|
* @return Field |
68
|
2 |
|
*/ |
69
|
|
|
public function addRawField(Field $field) |
70
|
|
|
{ |
71
|
|
|
if (array_key_exists($field->getName(), $this->fields)) { |
72
|
|
|
throw new InvalidArgumentException(sprintf( |
73
|
|
|
'Field with name "%s" already exists.', |
74
|
|
|
$field->getName() |
75
|
2 |
|
)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->fields[$field->getName()] = $field; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $name |
83
|
2 |
|
* |
84
|
|
|
* @return Field |
85
|
2 |
|
*/ |
86
|
|
|
public function addField(string $name): Field |
87
|
|
|
{ |
88
|
|
|
return $this->addRawField(new Field($name, $this)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $data |
93
|
2 |
|
* |
94
|
|
|
* @return VerdictList |
95
|
2 |
|
*/ |
96
|
|
|
public function validate($data): VerdictList |
97
|
2 |
|
{ |
98
|
2 |
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
99
|
|
|
|
100
|
2 |
|
$verdicts = []; |
101
|
2 |
|
foreach ($this->fields as $field) { |
102
|
2 |
|
try { |
103
|
|
|
$value = $propertyAccessor->getValue( |
104
|
|
|
$data, |
105
|
|
|
is_array($data) ? "[" . $field->getName() . "]" : $field->getName() |
106
|
|
|
); |
107
|
|
|
} catch (RuntimeException $e) { |
108
|
2 |
|
$value = null; |
109
|
2 |
|
} |
110
|
|
|
|
111
|
2 |
|
foreach ($field->getRules() as $rule) { |
112
|
|
|
$ruleValidator = $this->ruleValidatorFactory->getInstance($rule); |
113
|
|
|
|
114
|
|
|
$verdicts[] = $ruleValidator->verdict($value, $rule); |
115
|
2 |
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return new VerdictList($verdicts); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|