1 | <?php |
||
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() |
|
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() |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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() |
|
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) |
|
192 | |||
193 | /** |
||
194 | * Returns the name of this class, without the namespace. |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | 2 | protected function getShortName() |
|
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) |
|
222 | } |
||
223 |