1 | <?php |
||
8 | abstract class AbstractSpec |
||
9 | { |
||
10 | /** |
||
11 | * Failure Mode for a halting failure. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | const HALTING_FAILURE = 'HALTING_FAILURE'; |
||
16 | |||
17 | /** |
||
18 | * Failure Mode for a hard failure. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | const HARD_FAILURE = 'HARD_FAILURE'; |
||
23 | |||
24 | /** |
||
25 | * Failure Mode for a soft failure. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | const SOFT_FAILURE = 'SOFT_FAILURE'; |
||
30 | |||
31 | /** |
||
32 | * Field name for the spec to operate on. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $field; |
||
37 | |||
38 | /** |
||
39 | * Rule to invoke. |
||
40 | * |
||
41 | * @var callable |
||
42 | */ |
||
43 | protected $rule; |
||
44 | |||
45 | /** |
||
46 | * Arguments supplied to the rule |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $args = []; |
||
51 | |||
52 | /** |
||
53 | * Failure message to be used instead of the default message. |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $message; |
||
58 | |||
59 | /** |
||
60 | * Name of the rule to execute. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $ruleClass; |
||
65 | |||
66 | /** |
||
67 | * Flag that determines the allowance of blank values. |
||
68 | * |
||
69 | * @var bool |
||
70 | */ |
||
71 | protected $allowBlanks = false; |
||
72 | |||
73 | /** |
||
74 | * An array of values to be considered blank. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $blankWhiteList = []; |
||
79 | |||
80 | /** |
||
81 | * AbstractSpec constructor. |
||
82 | * |
||
83 | * @param string $field |
||
84 | */ |
||
85 | 120 | public function __construct(string $field) |
|
89 | |||
90 | /** |
||
91 | * Invokes the rule that this spec is configured for. |
||
92 | * |
||
93 | * @param object $subject |
||
94 | * |
||
95 | * @return bool |
||
96 | */ |
||
97 | 33 | public function __invoke($subject): bool |
|
101 | |||
102 | /** |
||
103 | * Set a custom message. |
||
104 | * |
||
105 | * @param string $message |
||
106 | * |
||
107 | * @return self |
||
108 | */ |
||
109 | 3 | public function setMessage(string $message): self |
|
115 | |||
116 | /** |
||
117 | * Returns the field this spec applies to. |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | 54 | public function getField(): string |
|
125 | |||
126 | /** |
||
127 | * Returns the failure message for this rule specification. |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | 27 | public function getMessage(): string |
|
139 | |||
140 | /** |
||
141 | * Returns the args this spec is configured to use. |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | 24 | public function getArgs(): array |
|
149 | |||
150 | /** |
||
151 | * Returns the name of the rule that was ran. |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | 6 | public function getRuleClass(): string |
|
159 | |||
160 | /** |
||
161 | * Sets the rule and ruleClass for the spec. |
||
162 | * |
||
163 | * @param string $ruleClass |
||
164 | * |
||
165 | * @throws RuleClassNotDefinedException |
||
166 | * |
||
167 | * @return AbstractSpec |
||
168 | */ |
||
169 | 69 | public function setRule(string $ruleClass): self |
|
180 | |||
181 | /** |
||
182 | * Sets the white list of values that should be considered blank. |
||
183 | * |
||
184 | * @param array $blankWhiteList |
||
185 | * |
||
186 | * @return ValidateSpec |
||
187 | */ |
||
188 | 12 | public function setBlankValues(array $blankWhiteList): self |
|
194 | |||
195 | /** |
||
196 | * Sets the rule to halt the entire validation process on the subject. |
||
197 | * |
||
198 | * @return AbstractSpec |
||
199 | */ |
||
200 | 3 | public function asHaltingRule(): self |
|
206 | |||
207 | /** |
||
208 | * Sets the rule to stop further rules from applying to the same field. |
||
209 | * |
||
210 | * @return AbstractSpec |
||
211 | */ |
||
212 | 3 | public function asHardRule(): self |
|
218 | |||
219 | /** |
||
220 | * Sets the rule to allow other rules to operate on the same field. |
||
221 | * |
||
222 | * @return AbstractSpec |
||
223 | */ |
||
224 | 3 | public function asSoftRule(): self |
|
230 | |||
231 | /** |
||
232 | * Returns the current failure mode. |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | 24 | public function getFailureMode(): string |
|
240 | |||
241 | /** |
||
242 | * Determines if the field is a `valid` blank value. |
||
243 | * |
||
244 | * Values are considered blank if they are, not sent, null, or strings that trim down to nothing. integers, floats, |
||
245 | * arrays, resources, objects, etc., are never considered blank. Even a value of `(int) 0` will *not* evaluate as |
||
246 | * blank. |
||
247 | * The optional second argument is used to supply an array of white listed items that should be considered blank. |
||
248 | * |
||
249 | * @param mixed $subject |
||
250 | * @param array $blankWhiteList |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | 81 | public function subjectFieldIsBlank($subject): bool |
|
275 | |||
276 | /** |
||
277 | * Returns the default failure message for this rule specification. |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | 24 | protected function getDefaultMessage(): string |
|
285 | |||
286 | /** |
||
287 | * Converts the args to a string. |
||
288 | * |
||
289 | * @return string |
||
290 | */ |
||
291 | 24 | protected function argsTostring(): string |
|
299 | } |
||
300 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: