|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Particle. |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/particle-php for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com) |
|
7
|
|
|
* @license https://github.com/particle-php/validator/blob/master/LICENSE New BSD License |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Particle\Validator; |
|
10
|
|
|
|
|
11
|
|
|
use Particle\Validator\Output\Subject; |
|
12
|
|
|
use Particle\Validator\Value\Container; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The Rule class is the abstract parent of all rules of Particle and defines their common behaviour. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Particle\Validator |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class Rule |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Contains an array of all values to be validated. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $values; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Contains an array of messages to be returned on validation errors. |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $messageTemplates = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Contains a reference to the MessageStack to append errors to. |
|
37
|
|
|
* |
|
38
|
|
|
* @var MessageStack |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $messageStack; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The key we have to validate the value of. |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $key; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The name may be used in validation error messages. |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $name; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* This method should validate, possibly log errors, and return the result as a boolean. |
|
58
|
|
|
* |
|
59
|
|
|
* @param mixed $value |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
|
abstract public function validate($value); |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* This indicates whether or not the rule can and should break the chain it's in. |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
202 |
|
public function shouldBreakChain() |
|
70
|
|
|
{ |
|
71
|
202 |
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* This indicates whether or not the rule should break the chain it's in on validation failure. |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
92 |
|
public function shouldBreakChainOnError() |
|
80
|
|
|
{ |
|
81
|
92 |
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Registers the message stack to append errors to. |
|
86
|
|
|
* |
|
87
|
|
|
* @param MessageStack $messageStack |
|
88
|
|
|
* @return $this |
|
89
|
|
|
*/ |
|
90
|
256 |
|
public function setMessageStack(MessageStack $messageStack) |
|
91
|
|
|
{ |
|
92
|
256 |
|
$this->messageStack = $messageStack; |
|
93
|
256 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Sets the default parameters for each validation rule (key and name). |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $key |
|
100
|
|
|
* @param string $name |
|
101
|
|
|
* @return $this |
|
102
|
|
|
*/ |
|
103
|
258 |
|
public function setParameters($key, $name) |
|
104
|
|
|
{ |
|
105
|
258 |
|
$this->key = $key; |
|
106
|
258 |
|
$this->name = $name; |
|
107
|
258 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Determines whether or not the value of $key is valid in the array $values and returns the result as a bool. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $key |
|
114
|
|
|
* @param Container $input |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
231 |
|
public function isValid($key, Container $input) |
|
118
|
|
|
{ |
|
119
|
231 |
|
return $this->validate($input->get($key)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Attach a representation of this rule to the Output\Subject $subject. |
|
124
|
|
|
* |
|
125
|
|
|
* @internal |
|
126
|
|
|
* @param Subject $subject |
|
127
|
|
|
* @param MessageStack $messageStack |
|
128
|
|
|
*/ |
|
129
|
2 |
|
public function output(Subject $subject, MessageStack $messageStack) |
|
130
|
|
|
{ |
|
131
|
2 |
|
$this->setParameters($subject->getKey(), $subject->getName()); |
|
132
|
|
|
|
|
133
|
2 |
|
$outputRule = new Output\Rule( |
|
134
|
2 |
|
$this->getShortName(), |
|
135
|
2 |
|
$this->getMessageTemplates($messageStack), |
|
136
|
2 |
|
$this->getMessageParameters() |
|
137
|
2 |
|
); |
|
138
|
|
|
|
|
139
|
2 |
|
$subject->addRule($outputRule); |
|
140
|
2 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Appends the error for reason $reason to the MessageStack. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $reason |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
121 |
|
protected function error($reason) |
|
149
|
|
|
{ |
|
150
|
121 |
|
$this->messageStack->append( |
|
151
|
121 |
|
new Failure( |
|
152
|
121 |
|
$this->key, |
|
153
|
121 |
|
$reason, |
|
154
|
121 |
|
$this->getMessage($reason), |
|
155
|
121 |
|
$this->getMessageParameters() |
|
156
|
121 |
|
) |
|
157
|
121 |
|
); |
|
158
|
|
|
|
|
159
|
121 |
|
return false; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Return an array of all parameters that might be replaced in the validation error messages. |
|
164
|
|
|
* |
|
165
|
|
|
* @return array |
|
166
|
|
|
*/ |
|
167
|
123 |
|
protected function getMessageParameters() |
|
168
|
|
|
{ |
|
169
|
123 |
|
$name = isset($this->name) ? $this->name : str_replace('_', ' ', $this->key); |
|
170
|
|
|
|
|
171
|
|
|
return [ |
|
172
|
123 |
|
'key' => $this->key, |
|
173
|
123 |
|
'name' => $name, |
|
174
|
123 |
|
]; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Returns an error message for the reason $reason, or an empty string if it doesn't exist. |
|
179
|
|
|
* |
|
180
|
|
|
* @param mixed $reason |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
121 |
|
protected function getMessage($reason) |
|
184
|
|
|
{ |
|
185
|
121 |
|
$messageTemplate = ''; |
|
186
|
121 |
|
if (array_key_exists($reason, $this->messageTemplates)) { |
|
187
|
121 |
|
$messageTemplate = $this->messageTemplates[$reason]; |
|
188
|
121 |
|
} |
|
189
|
|
|
|
|
190
|
121 |
|
return $messageTemplate; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Returns the name of this class, without the namespace. |
|
195
|
|
|
* |
|
196
|
|
|
* @return string |
|
197
|
|
|
*/ |
|
198
|
2 |
|
protected function getShortName() |
|
199
|
|
|
{ |
|
200
|
2 |
|
return substr(get_class($this), strrpos(get_class($this), '\\') + 1); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Get an array of Message Templates to be returned in output. |
|
205
|
|
|
* |
|
206
|
|
|
* @param MessageStack $messageStack |
|
207
|
|
|
* @return array |
|
208
|
|
|
*/ |
|
209
|
2 |
|
protected function getMessageTemplates(MessageStack $messageStack) |
|
210
|
|
|
{ |
|
211
|
2 |
|
$messages = $this->messageTemplates; |
|
212
|
2 |
|
foreach ($messages as $reason => $message) { |
|
213
|
2 |
|
$overwrite = $messageStack->getOverwrite($reason, $this->key); |
|
214
|
|
|
|
|
215
|
2 |
|
if (is_string($overwrite)) { |
|
216
|
1 |
|
$messages[$reason] = $overwrite; |
|
217
|
1 |
|
} |
|
218
|
2 |
|
} |
|
219
|
|
|
|
|
220
|
2 |
|
return $messages; |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|