1 | <?php |
||
29 | abstract class HashArray extends ArrayObject implements Comparable { |
||
30 | |||
31 | /** |
||
32 | * Maps element hashes to their offsets. |
||
33 | * |
||
34 | * @since 0.1 |
||
35 | * |
||
36 | * @var array [ element hash (string) => element offset (string|int) ] |
||
37 | */ |
||
38 | protected $offsetHashes = []; |
||
39 | |||
40 | /** |
||
41 | * @var integer |
||
42 | */ |
||
43 | protected $indexOffset = 0; |
||
44 | |||
45 | /** |
||
46 | * Returns the name of an interface/class that the element should implement/extend. |
||
47 | * |
||
48 | * @since 0.4 |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | abstract public function getObjectType(); |
||
53 | |||
54 | /** |
||
55 | * @see ArrayObject::__construct |
||
56 | * |
||
57 | * @param array|Traversable|null $input |
||
58 | * @param int $flags |
||
59 | * @param string $iteratorClass |
||
60 | * |
||
61 | * @throws InvalidArgumentException |
||
62 | */ |
||
63 | public function __construct( $input = null, $flags = 0, $iteratorClass = 'ArrayIterator' ) { |
||
76 | |||
77 | /** |
||
78 | * Finds a new offset for when appending an element. |
||
79 | * The base class does this, so it would be better to integrate, |
||
80 | * but there does not appear to be any way to do this... |
||
81 | * |
||
82 | * @return integer |
||
83 | */ |
||
84 | protected function getNewOffset() { |
||
91 | |||
92 | /** |
||
93 | * Gets called before a new element is added to the ArrayObject. |
||
94 | * |
||
95 | * At this point the index is always set (ie not null) and the |
||
96 | * value is always of the type returned by @see getObjectType. |
||
97 | * |
||
98 | * Should return a boolean. When false is returned the element |
||
99 | * does not get added to the ArrayObject. |
||
100 | * |
||
101 | * @since 0.1 |
||
102 | * |
||
103 | * @param int|string $index |
||
104 | * @param Hashable $hashable |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | protected function preSetElement( $index, $hashable ) { |
||
122 | |||
123 | /** |
||
124 | * Returns if there is an element with the provided hash. |
||
125 | * |
||
126 | * @since 0.1 |
||
127 | * |
||
128 | * @param string $elementHash |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | public function hasElementHash( $elementHash ) { |
||
135 | |||
136 | /** |
||
137 | * Returns if there is an element with the same hash as the provided element in the list. |
||
138 | * |
||
139 | * @since 0.1 |
||
140 | * |
||
141 | * @param Hashable $element |
||
142 | * |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function hasElement( Hashable $element ) { |
||
148 | |||
149 | /** |
||
150 | * Removes the element with the hash of the provided element, if there is such an element in the list. |
||
151 | * |
||
152 | * @since 0.1 |
||
153 | * |
||
154 | * @param Hashable $element |
||
155 | */ |
||
156 | public function removeElement( Hashable $element ) { |
||
159 | |||
160 | /** |
||
161 | * Removes the element with the provided hash, if there is such an element in the list. |
||
162 | * |
||
163 | * @since 0.1 |
||
164 | * |
||
165 | * @param string $elementHash |
||
166 | */ |
||
167 | public function removeByElementHash( $elementHash ) { |
||
173 | |||
174 | /** |
||
175 | * Adds the provided element to the list if there is no element with the same hash yet. |
||
176 | * |
||
177 | * @since 0.1 |
||
178 | * |
||
179 | * @param Hashable $element |
||
180 | * |
||
181 | * @return bool Indicates if the element was added or not. |
||
182 | */ |
||
183 | public function addElement( Hashable $element ) { |
||
192 | |||
193 | /** |
||
194 | * Returns the element with the provided hash or false if there is no such element. |
||
195 | * |
||
196 | * @since 0.1 |
||
197 | * |
||
198 | * @param string $elementHash |
||
199 | * |
||
200 | * @return mixed|bool |
||
201 | */ |
||
202 | public function getByElementHash( $elementHash ) { |
||
211 | |||
212 | /** |
||
213 | * @see ArrayObject::offsetUnset |
||
214 | * |
||
215 | * @since 0.1 |
||
216 | * |
||
217 | * @param mixed $index |
||
218 | */ |
||
219 | public function offsetUnset( $index ) { |
||
233 | |||
234 | /** |
||
235 | * @see Comparable::equals |
||
236 | * |
||
237 | * The comparison is done purely value based, ignoring the order of the elements in the array. |
||
238 | * |
||
239 | * @since 0.3 |
||
240 | * |
||
241 | * @param mixed $target |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | public function equals( $target ) { |
||
253 | |||
254 | /** |
||
255 | * @see ArrayObject::append |
||
256 | * |
||
257 | * @param mixed $value |
||
258 | */ |
||
259 | public function append( $value ) { |
||
262 | |||
263 | /** |
||
264 | * @see ArrayObject::offsetSet() |
||
265 | * |
||
266 | * @param mixed $index |
||
267 | * @param mixed $value |
||
268 | */ |
||
269 | public function offsetSet( $index, $value ) { |
||
272 | |||
273 | /** |
||
274 | * Returns if the provided value has the same type as the elements |
||
275 | * that can be added to this ArrayObject. |
||
276 | * |
||
277 | * @param mixed $value |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | protected function hasValidType( $value ) { |
||
285 | |||
286 | /** |
||
287 | * Method that actually sets the element and holds |
||
288 | * all common code needed for set operations, including |
||
289 | * type checking and offset resolving. |
||
290 | * |
||
291 | * If you want to do additional indexing or have code that |
||
292 | * otherwise needs to be executed whenever an element is added, |
||
293 | * you can overload @see preSetElement. |
||
294 | * |
||
295 | * @param mixed $index |
||
296 | * @param mixed $value |
||
297 | * |
||
298 | * @throws InvalidArgumentException |
||
299 | */ |
||
300 | protected function setElement( $index, $value ) { |
||
315 | |||
316 | /** |
||
317 | * @see Serializable::serialize |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | public function serialize() { |
||
327 | |||
328 | /** |
||
329 | * @see Serializable::unserialize |
||
330 | * |
||
331 | * @param string $serialized |
||
332 | */ |
||
333 | public function unserialize( $serialized ) { |
||
344 | |||
345 | /** |
||
346 | * Returns if the ArrayObject has no elements. |
||
347 | * |
||
348 | * @return bool |
||
349 | */ |
||
350 | public function isEmpty() { |
||
353 | |||
354 | } |
||
355 |
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: