|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Albert221\Validation; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayAccess; |
|
8
|
|
|
use OutOfBoundsException; |
|
9
|
|
|
use OutOfRangeException; |
|
10
|
|
|
|
|
11
|
|
|
class ValidationState implements ArrayAccess |
|
12
|
|
|
{ |
|
13
|
|
|
const FLAT = 0; |
|
14
|
|
|
const GROUPED = 1; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Verdicts |
|
18
|
|
|
*/ |
|
19
|
|
|
private $verdicts; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* ValidationState constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $verdicts |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(array $verdicts) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->verdicts = new Verdicts($verdicts); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return bool |
|
33
|
|
|
*/ |
|
34
|
|
|
public function isValid(): bool |
|
35
|
|
|
{ |
|
36
|
|
|
return count($this->verdicts->failing()) === 0; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param int $strategy |
|
|
|
|
|
|
41
|
|
|
* |
|
42
|
|
|
* @return Verdict[] |
|
43
|
|
|
*/ |
|
44
|
|
|
public function all(): array |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->verdicts->toArray(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $name |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
public function isFieldValid(string $name): bool |
|
55
|
|
|
{ |
|
56
|
|
|
$verdictsForField = $this->verdicts->forField($name); |
|
57
|
|
|
if ($verdictsForField->count() === 0) { |
|
58
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
59
|
|
|
'Field "%s" does not exist in the validation state.', |
|
60
|
|
|
$name |
|
61
|
|
|
)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $verdictsForField->failing()->count === 0; |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $name |
|
69
|
|
|
* |
|
70
|
|
|
* @return Verdict[] |
|
71
|
|
|
*/ |
|
72
|
|
|
public function field(string $name): array |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->verdicts->forField($name)->toArray(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param int|string $offset |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function offsetExists($offset): bool |
|
83
|
|
|
{ |
|
84
|
|
|
if (is_int($offset)) { |
|
85
|
|
|
return isset($this->verdicts->toArray()[$offset]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (!is_string($offset)) { |
|
89
|
|
|
throw new OutOfRangeException(sprintf( |
|
90
|
|
|
'Offset should be an integer index for FLAT strategy or string for GROUPED, %s given.', |
|
91
|
|
|
is_scalar($offset) ? gettype($offset): get_class($offset) |
|
92
|
|
|
)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this->verdicts->forField($offset)->count() > 0; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param int|string $offset |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
public function offsetGet($offset): array |
|
104
|
|
|
{ |
|
105
|
|
|
if (!$this->offsetExists($offset)) { |
|
106
|
|
|
throw new OutOfBoundsException(sprintf( |
|
107
|
|
|
'Item with offset "%s" does not exist.', |
|
108
|
|
|
$offset |
|
109
|
|
|
)); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (is_int($offset)) { |
|
113
|
|
|
return $this->all()[$offset]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $this->field($offset); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function offsetSet($offset, $value) |
|
120
|
|
|
{ |
|
121
|
|
|
// You cannot set any value. |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function offsetUnset($offset) |
|
125
|
|
|
{ |
|
126
|
|
|
// You cannot unset any value. |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.