1 | <?php |
||
27 | abstract class AbstractElement implements FormElementInterface, Iterator |
||
28 | { |
||
29 | /** @var string */ |
||
30 | protected $type = ''; |
||
31 | /** @var int */ |
||
32 | protected static $tabIndex = 1; |
||
33 | |||
34 | /** @var string */ |
||
35 | protected $name; |
||
36 | /** @var string */ |
||
37 | protected $label; |
||
38 | /** @var mixed */ |
||
39 | protected $value; |
||
40 | /** @var array */ |
||
41 | protected $attributes = []; |
||
42 | /** @var array<ValidatorInterface> */ |
||
43 | protected $validators = []; |
||
44 | /** @var array */ |
||
45 | protected $errors = []; |
||
46 | /** @var FormElementInterface */ |
||
47 | protected $parentNode; |
||
48 | /** @var array */ |
||
49 | protected $mandatoryParentTypes = []; |
||
50 | |||
51 | // The implementation of the Iterator interface. |
||
52 | use IteratorTrait; |
||
53 | // CamelCase to under_score converter |
||
54 | use CamelCaseToUnderScoreTrait; |
||
55 | |||
56 | /** |
||
57 | * AbstractFormElement constructor. |
||
58 | * |
||
59 | * @param string $name |
||
60 | * @param string $label |
||
61 | * @param mixed $value |
||
62 | */ |
||
63 | 21 | public function __construct($name = '', $label = '', $value = null) |
|
69 | |||
70 | /** |
||
71 | * Returns the element type. |
||
72 | * |
||
73 | * @throws Exception |
||
74 | * @return string |
||
75 | */ |
||
76 | 2 | final public function getType() |
|
84 | |||
85 | /** |
||
86 | * Sets element name. The implementation should decide if it is allowed after init. |
||
87 | * |
||
88 | * @param string $name |
||
89 | * @return AbstractElement |
||
90 | */ |
||
91 | 9 | public function setName($name) |
|
97 | |||
98 | /** |
||
99 | * Returns the element name. If parameter is TRUE, then the method should include all the parents' names as well. |
||
100 | * |
||
101 | * @param boolean $getFulNodeName |
||
102 | * @return string |
||
103 | */ |
||
104 | 12 | public function getName($getFulNodeName = true) |
|
116 | |||
117 | /** |
||
118 | * Sets element label. |
||
119 | * |
||
120 | * @param string $label |
||
121 | * @return AbstractElement |
||
122 | */ |
||
123 | 1 | public function setLabel($label) |
|
129 | |||
130 | /** |
||
131 | * Returns the element label. |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | 1 | public function getLabel() |
|
139 | |||
140 | /** |
||
141 | * Sets element value. |
||
142 | * |
||
143 | * @param mixed $value |
||
144 | * @return AbstractElement |
||
145 | */ |
||
146 | 8 | public function setValue($value) |
|
152 | |||
153 | /** |
||
154 | * Returns element value. |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | 1 | public function getValue() |
|
162 | |||
163 | /** |
||
164 | * Gets element Id. |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | 1 | public function getId() |
|
183 | |||
184 | /** |
||
185 | * Sets multiple attributes. |
||
186 | * |
||
187 | * @param array $attributes |
||
188 | * @return AbstractElement |
||
189 | */ |
||
190 | 11 | public function setAttributes(array $attributes) |
|
200 | |||
201 | /** |
||
202 | * Sets element attribute. |
||
203 | * |
||
204 | * @param string $key |
||
205 | * @param mixed $value |
||
206 | * @throws InvalidArgumentException |
||
207 | * @return AbstractElement |
||
208 | */ |
||
209 | 11 | protected function setAttribute($key, $value) |
|
219 | |||
220 | /** |
||
221 | * Gets all the attributes. |
||
222 | * |
||
223 | * @return array |
||
224 | */ |
||
225 | 6 | public function getAttributes() |
|
229 | |||
230 | /** |
||
231 | * Gets element attribute. |
||
232 | * |
||
233 | * @param string $name |
||
234 | * @return mixed |
||
235 | */ |
||
236 | 2 | public function getAttribute($name) |
|
244 | |||
245 | /** |
||
246 | * Sets and increments the tabulator index globally. This method should be used only on visible elements. |
||
247 | * |
||
248 | * @param bool $reset |
||
249 | * @return AbstractElement |
||
250 | */ |
||
251 | 20 | public function setTabIndex($reset = false) |
|
261 | |||
262 | /** |
||
263 | * Sets the element errors. Usually the validator should set it, but it is allowed to set from outside too. |
||
264 | * |
||
265 | * @param array $errors |
||
266 | * @return AbstractElement |
||
267 | */ |
||
268 | 1 | public function setErrors(array $errors) |
|
274 | |||
275 | /** |
||
276 | * Checks if there are error messages set. |
||
277 | * |
||
278 | * @return boolean |
||
279 | */ |
||
280 | 1 | public function hasErrors() |
|
284 | |||
285 | /** |
||
286 | * Gets validation errors. |
||
287 | * |
||
288 | * @return array |
||
289 | */ |
||
290 | 2 | public function getErrors() |
|
294 | |||
295 | /** |
||
296 | * Sets the element validators. |
||
297 | * |
||
298 | * @param array<ValidatorInterface> $validators |
||
299 | * @return FormElementInterface |
||
300 | */ |
||
301 | 1 | public function setValidators(array $validators) |
|
311 | |||
312 | /** |
||
313 | * Adds validator to the form. |
||
314 | * |
||
315 | * @param ValidatorInterface $validator |
||
316 | * @return AbstractElement |
||
317 | */ |
||
318 | 1 | protected function addValidator(ValidatorInterface $validator) |
|
324 | |||
325 | /** |
||
326 | * Gets the element validators. |
||
327 | * |
||
328 | * @return array<ValidatorInterface> |
||
329 | */ |
||
330 | 1 | public function getValidators() |
|
334 | |||
335 | /** |
||
336 | * Validates element value. |
||
337 | * |
||
338 | * @param bool $reValidate |
||
339 | * @return bool |
||
340 | */ |
||
341 | 3 | public function isValid($reValidate = false) |
|
356 | |||
357 | /** |
||
358 | * Sets the parent element. |
||
359 | * |
||
360 | * @param FormElementInterface $formElement |
||
361 | * @throws RuntimeException |
||
362 | * @return AbstractElement |
||
363 | */ |
||
364 | 10 | public function setParentNode(FormElementInterface $formElement) |
|
380 | |||
381 | /** |
||
382 | * Gets the parent element. |
||
383 | * |
||
384 | * @return FormElementInterface |
||
385 | */ |
||
386 | 1 | public function getParentNode() |
|
390 | } |
||
391 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.