1 | <?php |
||
10 | trait HasChildrenTrait |
||
11 | { |
||
12 | /** |
||
13 | * List of the child element |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $elements = array(); |
||
18 | |||
19 | /** |
||
20 | * Value to keep track of the order the elements were added |
||
21 | * |
||
22 | * @var int |
||
23 | */ |
||
24 | protected $elementsIndex = PHP_INT_MAX; |
||
25 | |||
26 | /** |
||
27 | * @var ElementFactory |
||
28 | */ |
||
29 | protected $elementFactory; |
||
30 | |||
31 | /** |
||
32 | * Generates the actual name that will be used to identify the element in the input object |
||
33 | * For input objects the name of the child is the same as the name provided, |
||
34 | * For field-sets the name of the child is prefixed/name-spaced with the name of the field-set |
||
35 | * For collections the name of the child is prefixed with the name of the collection and an index placeholder |
||
36 | * |
||
37 | * @param string $name |
||
38 | * |
||
39 | * @return string |
||
40 | */ |
||
41 | 12 | protected function getFullChildName($name) |
|
45 | |||
46 | /** |
||
47 | * Sets the element factory. |
||
48 | * Objects that have children (eg: Fieldset, Collection) must be factory-aware |
||
49 | * This is passed down from form to fieldsets, collections or other elements that have children |
||
50 | * |
||
51 | * @param ElementFactory $elementFactory |
||
52 | * @return $this |
||
53 | */ |
||
54 | 9 | public function setElementFactory(ElementFactory $elementFactory) |
|
61 | |||
62 | /** |
||
63 | * Add an element to the children list |
||
64 | * |
||
65 | * @param string|\Sirius\Input\Element $nameOrElement |
||
66 | * @param array $specs |
||
67 | * |
||
68 | * @throws \RuntimeException |
||
69 | * @return $this |
||
70 | */ |
||
71 | 19 | public function addElement($nameOrElement, $specs = array()) |
|
103 | |||
104 | /** |
||
105 | * Create the children using the factory. |
||
106 | * This will be called by elements that are factory-aware (eg: Fieldsets) |
||
107 | */ |
||
108 | 9 | protected function createChildren() |
|
117 | |||
118 | /** |
||
119 | * Retrieve an element by name |
||
120 | * |
||
121 | * @param string $name |
||
122 | * |
||
123 | * @return \Sirius\Input\Element |
||
124 | */ |
||
125 | 9 | public function getElement($name) |
|
129 | |||
130 | /** |
||
131 | * Removes an element from the children list |
||
132 | * |
||
133 | * @param string $name |
||
134 | * |
||
135 | * @throws \RuntimeException |
||
136 | * @return $this |
||
137 | */ |
||
138 | 3 | public function removeElement($name) |
|
146 | |||
147 | /** |
||
148 | * Returns whether an element exist in the children list |
||
149 | * |
||
150 | * @param string $name |
||
151 | * |
||
152 | * @return boolean |
||
153 | */ |
||
154 | 3 | public function hasElement($name) |
|
158 | |||
159 | /** |
||
160 | * Input comparator callback |
||
161 | * |
||
162 | * @param \ArrayObject $childA |
||
163 | * @param \ArrayObject $childB |
||
164 | * |
||
165 | * @return integer |
||
166 | */ |
||
167 | 7 | protected function childComparator($childA, $childB) |
|
189 | |||
190 | /** |
||
191 | * Returns the list of the elements organized by priority |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | 17 | public function getElements() |
|
202 | |||
203 | |||
204 | /** |
||
205 | * Unset the group property for elements without a valid group (ie: existing group) |
||
206 | */ |
||
207 | 11 | protected function cleanUpMissingGroups() |
|
216 | |||
217 | } |
||
218 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: