1 | <?php |
||
28 | abstract class HashArray extends ArrayObject { |
||
29 | |||
30 | /** |
||
31 | * Maps element hashes to their offsets. |
||
32 | * |
||
33 | * @since 0.1 |
||
34 | * |
||
35 | * @var array [ element hash (string) => element offset (string|int) ] |
||
36 | */ |
||
37 | protected $offsetHashes = []; |
||
38 | |||
39 | /** |
||
40 | * @var integer |
||
41 | */ |
||
42 | protected $indexOffset = 0; |
||
43 | |||
44 | /** |
||
45 | * Returns the name of an interface/class that the element should implement/extend. |
||
46 | * |
||
47 | * @since 0.4 |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | abstract public function getObjectType(); |
||
52 | |||
53 | /** |
||
54 | * @see ArrayObject::__construct |
||
55 | * |
||
56 | * @param array|Traversable|null $input |
||
57 | * @param int $flags |
||
58 | * @param string $iteratorClass |
||
59 | * |
||
60 | * @throws InvalidArgumentException |
||
61 | */ |
||
62 | public function __construct( $input = null, $flags = 0, $iteratorClass = 'ArrayIterator' ) { |
||
75 | |||
76 | /** |
||
77 | * Finds a new offset for when appending an element. |
||
78 | * The base class does this, so it would be better to integrate, |
||
79 | * but there does not appear to be any way to do this... |
||
80 | * |
||
81 | * @return integer |
||
82 | */ |
||
83 | protected function getNewOffset() { |
||
90 | |||
91 | /** |
||
92 | * Gets called before a new element is added to the ArrayObject. |
||
93 | * |
||
94 | * At this point the index is always set (ie not null) and the |
||
95 | * value is always of the type returned by @see getObjectType. |
||
96 | * |
||
97 | * Should return a boolean. When false is returned the element |
||
98 | * does not get added to the ArrayObject. |
||
99 | * |
||
100 | * @since 0.1 |
||
101 | * |
||
102 | * @param int|string $index |
||
103 | * @param Hashable $hashable |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | protected function preSetElement( $index, $hashable ) { |
||
120 | |||
121 | /** |
||
122 | * Returns if there is an element with the provided hash. |
||
123 | * |
||
124 | * @since 0.1 |
||
125 | * |
||
126 | * @param string $elementHash |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function hasElementHash( $elementHash ) { |
||
133 | |||
134 | /** |
||
135 | * Returns if there is an element with the same hash as the provided element in the list. |
||
136 | * |
||
137 | * @since 0.1 |
||
138 | * |
||
139 | * @param Hashable $element |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function hasElement( Hashable $element ) { |
||
146 | |||
147 | /** |
||
148 | * Removes the element with the hash of the provided element, if there is such an element in the list. |
||
149 | * |
||
150 | * @since 0.1 |
||
151 | * |
||
152 | * @param Hashable $element |
||
153 | */ |
||
154 | public function removeElement( Hashable $element ) { |
||
157 | |||
158 | /** |
||
159 | * Removes the element with the provided hash, if there is such an element in the list. |
||
160 | * |
||
161 | * @since 0.1 |
||
162 | * |
||
163 | * @param string $elementHash |
||
164 | */ |
||
165 | public function removeByElementHash( $elementHash ) { |
||
171 | |||
172 | /** |
||
173 | * Adds the provided element to the list if there is no element with the same hash yet. |
||
174 | * |
||
175 | * @since 0.1 |
||
176 | * |
||
177 | * @param Hashable $element |
||
178 | * |
||
179 | * @return bool Indicates if the element was added or not. |
||
180 | */ |
||
181 | public function addElement( Hashable $element ) { |
||
190 | |||
191 | /** |
||
192 | * Returns the element with the provided hash or false if there is no such element. |
||
193 | * |
||
194 | * @since 0.1 |
||
195 | * |
||
196 | * @param string $elementHash |
||
197 | * |
||
198 | * @return mixed|bool |
||
199 | */ |
||
200 | public function getByElementHash( $elementHash ) { |
||
208 | |||
209 | /** |
||
210 | * @see ArrayObject::offsetUnset |
||
211 | * |
||
212 | * @since 0.1 |
||
213 | * |
||
214 | * @param mixed $index |
||
215 | */ |
||
216 | public function offsetUnset( $index ) { |
||
230 | |||
231 | /** |
||
232 | * @see ArrayObject::append |
||
233 | * |
||
234 | * @param mixed $value |
||
235 | */ |
||
236 | public function append( $value ) { |
||
239 | |||
240 | /** |
||
241 | * @see ArrayObject::offsetSet() |
||
242 | * |
||
243 | * @param mixed $index |
||
244 | * @param mixed $value |
||
245 | */ |
||
246 | public function offsetSet( $index, $value ) { |
||
249 | |||
250 | /** |
||
251 | * Returns if the provided value has the same type as the elements |
||
252 | * that can be added to this ArrayObject. |
||
253 | * |
||
254 | * @param mixed $value |
||
255 | * |
||
256 | * @return bool |
||
257 | */ |
||
258 | protected function hasValidType( $value ) { |
||
262 | |||
263 | /** |
||
264 | * Method that actually sets the element and holds |
||
265 | * all common code needed for set operations, including |
||
266 | * type checking and offset resolving. |
||
267 | * |
||
268 | * If you want to do additional indexing or have code that |
||
269 | * otherwise needs to be executed whenever an element is added, |
||
270 | * you can overload @see preSetElement. |
||
271 | * |
||
272 | * @param mixed $index |
||
273 | * @param mixed $value |
||
274 | * |
||
275 | * @throws InvalidArgumentException |
||
276 | */ |
||
277 | protected function setElement( $index, $value ) { |
||
292 | |||
293 | /** |
||
294 | * @see Serializable::serialize |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | public function serialize() { |
||
304 | |||
305 | /** |
||
306 | * @see Serializable::unserialize |
||
307 | * |
||
308 | * @param string $serialized |
||
309 | */ |
||
310 | public function unserialize( $serialized ) { |
||
321 | |||
322 | /** |
||
323 | * @return bool |
||
324 | */ |
||
325 | public function isEmpty() { |
||
328 | |||
329 | } |
||
330 |
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.