1 | <?php |
||
7 | class ValueValidator |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * The error messages generated after validation or set manually |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $messages = array(); |
||
16 | |||
17 | /** |
||
18 | * Will be used to construct the rules |
||
19 | * |
||
20 | * @var \Sirius\Validation\RuleFactory |
||
21 | */ |
||
22 | protected $ruleFactory; |
||
23 | |||
24 | /** |
||
25 | * The prototype that will be used to generate the error message |
||
26 | * |
||
27 | * @var \Sirius\Validation\ErrorMessage |
||
28 | */ |
||
29 | protected $errorMessagePrototype; |
||
30 | |||
31 | /** |
||
32 | * The rule collections for the validation |
||
33 | * |
||
34 | * @var \Sirius\Validation\RuleCollection |
||
35 | */ |
||
36 | protected $rules; |
||
37 | |||
38 | /** |
||
39 | * The label of the value to be validated |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $label; |
||
44 | |||
45 | |||
46 | 22 | public function __construct( |
|
64 | |||
65 | 1 | public function setLabel($label = null) |
|
71 | |||
72 | /** |
||
73 | * Add 1 or more validation rules |
||
74 | * |
||
75 | * @example |
||
76 | * // add multiple rules at once |
||
77 | * $validator->add(array( |
||
78 | * 'required', |
||
79 | * array('required', array('email', null, '{label} must be an email', 'Field B')), |
||
80 | * )); |
||
81 | * |
||
82 | * // add multiple rules using a string |
||
83 | * $validator->add('required | email'); |
||
84 | * |
||
85 | * // add validator with options |
||
86 | * $validator->add('minlength', array('min' => 2), '{label} should have at least {min} characters', 'Field label'); |
||
87 | * |
||
88 | * // add validator with string and parameters as JSON string |
||
89 | * $validator->add('minlength({"min": 2})({label} should have at least {min} characters)(Field label)'); |
||
90 | * |
||
91 | * // add validator with string and parameters as query string |
||
92 | * $validator->add('minlength(min=2)({label} should have at least {min} characters)(Field label)'); |
||
93 | * |
||
94 | * @param string|callback $name |
||
95 | * @param string|array $options |
||
96 | * @param string $messageTemplate |
||
97 | * @param string $label |
||
98 | * |
||
99 | * @return ValueValidator |
||
100 | */ |
||
101 | 22 | public function add($name, $options = null, $messageTemplate = null, $label = null) |
|
126 | |||
127 | /** |
||
128 | * @param array $rules |
||
129 | * |
||
130 | * @return ValueValidator |
||
131 | */ |
||
132 | 4 | public function addMultiple($rules) |
|
150 | |||
151 | /** |
||
152 | * @param AbstractValidator $validationRule |
||
153 | * |
||
154 | * @return ValueValidator |
||
155 | */ |
||
156 | 20 | public function addRule(AbstractRule $validationRule) |
|
163 | |||
164 | /** |
||
165 | * Remove validation rule |
||
166 | * |
||
167 | * @param mixed $name |
||
168 | * rule name or true if all rules should be deleted for that selector |
||
169 | * @param mixed $options |
||
170 | * rule options, necessary for rules that depend on params for their ID |
||
171 | * |
||
172 | * @throws \InvalidArgumentException |
||
173 | * @internal param string $selector data selector |
||
174 | * @return self |
||
175 | */ |
||
176 | 4 | public function remove($name = true, $options = null) |
|
188 | |||
189 | /** |
||
190 | * Converts a rule that was supplied as string into a set of options that define the rule |
||
191 | * |
||
192 | * @example 'minLength({"min":2})({label} must have at least {min} characters)(Street)' |
||
193 | * |
||
194 | * will be converted into |
||
195 | * |
||
196 | * array( |
||
197 | * 'minLength', // validator name |
||
198 | * array('min' => 2'), // validator options |
||
199 | * '{label} must have at least {min} characters', |
||
200 | * 'Street' // label |
||
201 | * ) |
||
202 | * |
||
203 | * @param string $ruleAsString |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | 5 | protected function parseRule($ruleAsString) |
|
238 | |||
239 | |||
240 | 20 | public function validate($value, $valueIdentifier = null, DataWrapper\WrapperInterface $context = null) |
|
270 | |||
271 | 18 | public function getMessages() |
|
275 | |||
276 | 19 | public function addMessage($message) |
|
282 | |||
283 | 1 | public function getRules() |
|
287 | } |
||
288 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: