1 | <?php |
||
30 | abstract class HashArray extends ArrayObject implements Hashable, Comparable { |
||
31 | |||
32 | /** |
||
33 | * Maps element hashes to their offsets. |
||
34 | * |
||
35 | * @since 0.1 |
||
36 | * |
||
37 | * @var array [ element hash (string) => element offset (string|int) ] |
||
38 | */ |
||
39 | protected $offsetHashes = []; |
||
40 | |||
41 | /** |
||
42 | * @var integer |
||
43 | */ |
||
44 | protected $indexOffset = 0; |
||
45 | |||
46 | /** |
||
47 | * Returns the name of an interface/class that the element should implement/extend. |
||
48 | * |
||
49 | * @since 0.4 |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | abstract public function getObjectType(); |
||
54 | |||
55 | /** |
||
56 | * @see ArrayObject::__construct |
||
57 | * |
||
58 | * @param array|Traversable|null $input |
||
59 | * @param int $flags |
||
60 | * @param string $iteratorClass |
||
61 | * |
||
62 | * @throws InvalidArgumentException |
||
63 | */ |
||
64 | public function __construct( $input = null, $flags = 0, $iteratorClass = 'ArrayIterator' ) { |
||
77 | |||
78 | /** |
||
79 | * Finds a new offset for when appending an element. |
||
80 | * The base class does this, so it would be better to integrate, |
||
81 | * but there does not appear to be any way to do this... |
||
82 | * |
||
83 | * @return integer |
||
84 | */ |
||
85 | protected function getNewOffset() { |
||
92 | |||
93 | /** |
||
94 | * Gets called before a new element is added to the ArrayObject. |
||
95 | * |
||
96 | * At this point the index is always set (ie not null) and the |
||
97 | * value is always of the type returned by @see getObjectType. |
||
98 | * |
||
99 | * Should return a boolean. When false is returned the element |
||
100 | * does not get added to the ArrayObject. |
||
101 | * |
||
102 | * @since 0.1 |
||
103 | * |
||
104 | * @param int|string $index |
||
105 | * @param Hashable $hashable |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | protected function preSetElement( $index, $hashable ) { |
||
123 | |||
124 | /** |
||
125 | * Returns if there is an element with the provided hash. |
||
126 | * |
||
127 | * @since 0.1 |
||
128 | * |
||
129 | * @param string $elementHash |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function hasElementHash( $elementHash ) { |
||
136 | |||
137 | /** |
||
138 | * Returns if there is an element with the same hash as the provided element in the list. |
||
139 | * |
||
140 | * @since 0.1 |
||
141 | * |
||
142 | * @param Hashable $element |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | public function hasElement( Hashable $element ) { |
||
149 | |||
150 | /** |
||
151 | * Removes the element with the hash of the provided element, if there is such an element in the list. |
||
152 | * |
||
153 | * @since 0.1 |
||
154 | * |
||
155 | * @param Hashable $element |
||
156 | */ |
||
157 | public function removeElement( Hashable $element ) { |
||
160 | |||
161 | /** |
||
162 | * Removes the element with the provided hash, if there is such an element in the list. |
||
163 | * |
||
164 | * @since 0.1 |
||
165 | * |
||
166 | * @param string $elementHash |
||
167 | */ |
||
168 | public function removeByElementHash( $elementHash ) { |
||
174 | |||
175 | /** |
||
176 | * Adds the provided element to the list if there is no element with the same hash yet. |
||
177 | * |
||
178 | * @since 0.1 |
||
179 | * |
||
180 | * @param Hashable $element |
||
181 | * |
||
182 | * @return bool Indicates if the element was added or not. |
||
183 | */ |
||
184 | public function addElement( Hashable $element ) { |
||
193 | |||
194 | /** |
||
195 | * Returns the element with the provided hash or false if there is no such element. |
||
196 | * |
||
197 | * @since 0.1 |
||
198 | * |
||
199 | * @param string $elementHash |
||
200 | * |
||
201 | * @return mixed|bool |
||
202 | */ |
||
203 | public function getByElementHash( $elementHash ) { |
||
212 | |||
213 | /** |
||
214 | * @see ArrayObject::offsetUnset |
||
215 | * |
||
216 | * @since 0.1 |
||
217 | * |
||
218 | * @param mixed $index |
||
219 | */ |
||
220 | public function offsetUnset( $index ) { |
||
234 | |||
235 | /** |
||
236 | * @see Hashable::getHash |
||
237 | * |
||
238 | * The hash is purely valuer based. Order of the elements in the array is not held into account. |
||
239 | * |
||
240 | * @since 0.1 |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | public function getHash() { |
||
248 | |||
249 | /** |
||
250 | * @see Comparable::equals |
||
251 | * |
||
252 | * The comparison is done purely value based, ignoring the order of the elements in the array. |
||
253 | * |
||
254 | * @since 0.3 |
||
255 | * |
||
256 | * @param mixed $target |
||
257 | * |
||
258 | * @return bool |
||
259 | */ |
||
260 | public function equals( $target ) { |
||
268 | |||
269 | /** |
||
270 | * @see ArrayObject::append |
||
271 | * |
||
272 | * @param mixed $value |
||
273 | */ |
||
274 | public function append( $value ) { |
||
277 | |||
278 | /** |
||
279 | * @see ArrayObject::offsetSet() |
||
280 | * |
||
281 | * @param mixed $index |
||
282 | * @param mixed $value |
||
283 | */ |
||
284 | public function offsetSet( $index, $value ) { |
||
287 | |||
288 | /** |
||
289 | * Returns if the provided value has the same type as the elements |
||
290 | * that can be added to this ArrayObject. |
||
291 | * |
||
292 | * @param mixed $value |
||
293 | * |
||
294 | * @return bool |
||
295 | */ |
||
296 | protected function hasValidType( $value ) { |
||
300 | |||
301 | /** |
||
302 | * Method that actually sets the element and holds |
||
303 | * all common code needed for set operations, including |
||
304 | * type checking and offset resolving. |
||
305 | * |
||
306 | * If you want to do additional indexing or have code that |
||
307 | * otherwise needs to be executed whenever an element is added, |
||
308 | * you can overload @see preSetElement. |
||
309 | * |
||
310 | * @param mixed $index |
||
311 | * @param mixed $value |
||
312 | * |
||
313 | * @throws InvalidArgumentException |
||
314 | */ |
||
315 | protected function setElement( $index, $value ) { |
||
330 | |||
331 | /** |
||
332 | * @see Serializable::serialize |
||
333 | * |
||
334 | * @return string |
||
335 | */ |
||
336 | public function serialize() { |
||
342 | |||
343 | /** |
||
344 | * @see Serializable::unserialize |
||
345 | * |
||
346 | * @param string $serialized |
||
347 | */ |
||
348 | public function unserialize( $serialized ) { |
||
359 | |||
360 | /** |
||
361 | * Returns if the ArrayObject has no elements. |
||
362 | * |
||
363 | * @return bool |
||
364 | */ |
||
365 | public function isEmpty() { |
||
368 | |||
369 | } |
||
370 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.