1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Respect/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Respect\Validation; |
13
|
|
|
|
14
|
|
|
use IteratorAggregate; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Handles validation's results. |
18
|
|
|
* |
19
|
|
|
* @author Henrique Moody <[email protected]> |
20
|
|
|
* |
21
|
|
|
* @since 2.0.0 |
22
|
|
|
*/ |
23
|
|
|
final class Result implements IteratorAggregate |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
private $isValid; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var mixed |
32
|
|
|
*/ |
33
|
|
|
private $input; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Rule |
37
|
|
|
*/ |
38
|
|
|
private $rule; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Result[] |
42
|
|
|
*/ |
43
|
|
|
private $children; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
private $properties; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var bool |
52
|
|
|
*/ |
53
|
|
|
private $isInverted = false; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initializes the object. |
57
|
|
|
* |
58
|
|
|
* @param bool $isValid |
59
|
|
|
* @param mixed $input |
60
|
|
|
* @param Rule $rule |
61
|
|
|
* @param array $properties |
62
|
|
|
* @param Result $child |
63
|
|
|
* @param Result ...$child2 |
64
|
|
|
*/ |
65
|
15 |
|
public function __construct(bool $isValid, $input, Rule $rule, array $properties = [], Result ...$child) |
66
|
|
|
{ |
67
|
15 |
|
$this->isValid = $isValid; |
68
|
15 |
|
$this->input = $input; |
69
|
15 |
|
$this->rule = $rule; |
70
|
15 |
|
$this->properties = $properties; |
71
|
15 |
|
$this->children = $child; |
72
|
15 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns whether the result is valid or not. |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
2 |
|
public function isValid(): bool |
80
|
|
|
{ |
81
|
2 |
|
return $this->isValid; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the input that was used on the validation. |
86
|
|
|
* |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
2 |
|
public function getInput() |
90
|
|
|
{ |
91
|
2 |
|
return $this->input; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns the rule that performed the validation. |
96
|
|
|
* |
97
|
|
|
* @return Rule |
98
|
|
|
*/ |
99
|
2 |
|
public function getRule(): Rule |
100
|
|
|
{ |
101
|
2 |
|
return $this->rule; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns the children of the result. |
106
|
|
|
* |
107
|
|
|
* @return Result[] |
108
|
|
|
*/ |
109
|
2 |
|
public function getChildren(): array |
110
|
|
|
{ |
111
|
2 |
|
return $this->children; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the properties of the result. |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
3 |
|
public function getProperties(): array |
120
|
|
|
{ |
121
|
3 |
|
return $this->properties; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns whether the result is inverted or not. |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
3 |
|
public function isInverted(): bool |
130
|
|
|
{ |
131
|
3 |
|
return $this->isInverted; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Creates a new object with the inverted validation. |
136
|
|
|
* |
137
|
|
|
* @return Result |
138
|
|
|
*/ |
139
|
9 |
|
public function invert(): Result |
140
|
|
|
{ |
141
|
9 |
|
$result = clone $this; |
142
|
9 |
|
$result->isValid = !$result->isValid; |
143
|
9 |
|
$result->isInverted = !$result->isInverted; |
144
|
|
|
|
145
|
9 |
|
return $result; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getIterator(): ResultIterator |
149
|
|
|
{ |
150
|
|
|
return new ResultIterator($this); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function hasChildren(): bool |
154
|
|
|
{ |
155
|
|
|
return count($this->children) > 0; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|