1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Fuel\Validation |
4
|
|
|
* @version 2.0 |
5
|
|
|
* @author Fuel Development Team |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2010 - 2013 Fuel Development Team |
8
|
|
|
* @link http://fuelphp.com |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fuel\Validation; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Defines common functionality for interacting with validation results |
15
|
|
|
* |
16
|
|
|
* @package Fuel\Validation |
17
|
|
|
* @author Fuel Development Team |
18
|
|
|
* |
19
|
|
|
* @since 2.0 |
20
|
|
|
*/ |
21
|
|
|
class Result implements ResultInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Boolean flag to indicate overall failure or success |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
protected $result; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Contains an array of errors that occurred during validation |
33
|
|
|
* |
34
|
|
|
* @var string[] |
35
|
|
|
*/ |
36
|
|
|
protected $errors = array(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Contains the rule that caused a given field to fail |
40
|
|
|
* |
41
|
|
|
* @var RuleInterface[] |
42
|
|
|
*/ |
43
|
|
|
protected $failedRules = array(); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Contains a list of fields that passed validation |
47
|
|
|
* |
48
|
|
|
* @var string[] |
49
|
|
|
*/ |
50
|
|
|
protected $validated = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param bool $isValid |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
* |
57
|
|
|
* @since 2.0 |
58
|
|
|
*/ |
59
|
17 |
|
public function setResult($isValid) |
60
|
|
|
{ |
61
|
17 |
|
$this->result = $isValid; |
62
|
|
|
|
63
|
17 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return bool |
68
|
|
|
* |
69
|
|
|
* @since 2.0 |
70
|
|
|
*/ |
71
|
16 |
|
public function isValid() |
72
|
|
|
{ |
73
|
16 |
|
return $this->result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $field |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
* @throws InvalidFieldException |
81
|
|
|
* |
82
|
|
|
* @since 2.0 |
83
|
|
|
*/ |
84
|
3 |
|
public function getError($field) |
85
|
|
|
{ |
86
|
3 |
|
if ( ! isset($this->errors[$field])) |
87
|
3 |
|
{ |
88
|
1 |
|
throw new InvalidFieldException($field); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
return $this->errors[$field]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string[] |
96
|
|
|
* |
97
|
|
|
* @since 2.0 |
98
|
|
|
*/ |
99
|
3 |
|
public function getErrors() |
100
|
|
|
{ |
101
|
3 |
|
return $this->errors; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Sets the error message for the given field |
106
|
|
|
* |
107
|
|
|
* @param string $field |
108
|
|
|
* @param string $message |
109
|
|
|
* @param string $rule |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
* |
113
|
|
|
* @since 2.0 |
114
|
|
|
*/ |
115
|
8 |
|
public function setError($field, $message, $rule) |
116
|
|
|
{ |
117
|
8 |
|
$this->errors[$field] = $message; |
118
|
8 |
|
$this->failedRules[$field] = $rule; |
119
|
|
|
|
120
|
8 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string[] |
125
|
|
|
* |
126
|
|
|
* @since 2.0 |
127
|
|
|
*/ |
128
|
3 |
|
public function getValidated() |
129
|
|
|
{ |
130
|
3 |
|
return $this->validated; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $field |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
* |
138
|
|
|
* @since 2.0 |
139
|
|
|
*/ |
140
|
12 |
|
public function setValidated($field) |
141
|
|
|
{ |
142
|
12 |
|
$this->validated[] = $field; |
143
|
|
|
|
144
|
12 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns a list of rules that caused fields to fail, indexed by the field name. |
149
|
|
|
* |
150
|
|
|
* @return RuleInterface[] |
151
|
|
|
* |
152
|
|
|
* @since 2.0 |
153
|
|
|
*/ |
154
|
3 |
|
public function getFailedRules() |
155
|
|
|
{ |
156
|
3 |
|
return $this->failedRules; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
2 |
|
public function merge(ResultInterface $resultInterface, $fieldPrefix = '') |
163
|
|
|
{ |
164
|
2 |
|
foreach ($resultInterface->getErrors() as $key => $error) |
165
|
|
|
{ |
166
|
1 |
|
$this->errors[$fieldPrefix . $key] = $error; |
167
|
2 |
|
} |
168
|
|
|
|
169
|
2 |
|
foreach ($resultInterface->getFailedRules() as $key => $rules) |
170
|
|
|
{ |
171
|
1 |
|
$this->failedRules[$fieldPrefix . $key] = $rules; |
172
|
2 |
|
} |
173
|
|
|
|
174
|
2 |
|
foreach ($resultInterface->getValidated() as $name) |
175
|
|
|
{ |
176
|
1 |
|
$this->validated[] = $fieldPrefix . $name; |
177
|
2 |
|
} |
178
|
|
|
|
179
|
2 |
|
if ( ! $resultInterface->isValid()) |
180
|
2 |
|
{ |
181
|
1 |
|
$this->result = false; |
182
|
1 |
|
} |
183
|
|
|
|
184
|
2 |
|
return $this; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|