1 | <?php |
||
28 | class ReferenceList implements Comparable, Countable, IteratorAggregate, Serializable { |
||
29 | |||
30 | /** |
||
31 | * @var Reference[] Ordered list or references, indexed by SPL object hash. |
||
32 | */ |
||
33 | private $references = array(); |
||
34 | 29 | ||
35 | 29 | /** |
|
36 | 8 | * @param Reference[]|Traversable $references |
|
37 | * |
||
38 | * @throws InvalidArgumentException |
||
39 | 21 | */ |
|
40 | 12 | public function __construct( $references = array() ) { |
|
41 | 4 | if ( !is_array( $references ) && !( $references instanceof Traversable ) ) { |
|
42 | throw new InvalidArgumentException( '$references must be an array or an instance of Traversable' ); |
||
43 | } |
||
44 | 8 | ||
45 | 17 | foreach ( $references as $reference ) { |
|
46 | 17 | if ( !( $reference instanceof Reference ) ) { |
|
47 | throw new InvalidArgumentException( 'Every element in $references must be an instance of Reference' ); |
||
48 | } |
||
49 | |||
50 | $this->addReference( $reference ); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Adds the provided reference to the list. |
||
56 | * Empty references are ignored. |
||
57 | * |
||
58 | 14 | * @since 0.1 |
|
59 | 14 | * |
|
60 | * @param Reference $reference |
||
61 | * @param int|null $index |
||
62 | * |
||
63 | 14 | * @throws InvalidArgumentException |
|
64 | */ |
||
65 | 14 | public function addReference( Reference $reference, $index = null ) { |
|
66 | 14 | if ( $index !== null && ( !is_int( $index ) || $index < 0 ) ) { |
|
67 | 2 | throw new InvalidArgumentException( '$index must be a non-negative integer or null' ); |
|
68 | } |
||
69 | 14 | ||
70 | if ( $reference->isEmpty() ) { |
||
71 | return; |
||
72 | } |
||
73 | |||
74 | if ( $index === null || $index >= count( $this->references ) ) { |
||
75 | // Append object to the end of the reference list. |
||
76 | $this->references[spl_object_hash( $reference )] = $reference; |
||
77 | 14 | } else { |
|
78 | 14 | $this->insertReferenceAtIndex( $reference, $index ); |
|
79 | 14 | } |
|
80 | 14 | } |
|
81 | 14 | ||
82 | /** |
||
83 | * @since 1.1 |
||
84 | * |
||
85 | * @param Snak[]|Snak $snaks |
||
86 | * @param Snak [$snak2,...] |
||
87 | * |
||
88 | * @throws InvalidArgumentException |
||
89 | */ |
||
90 | public function addNewReference( $snaks = array() /*...*/ ) { |
||
97 | 5 | ||
98 | /** |
||
99 | * @param Reference $reference |
||
100 | * @param int $index |
||
101 | */ |
||
102 | private function insertReferenceAtIndex( Reference $reference, $index ) { |
||
109 | 2 | ||
110 | 2 | /** |
|
111 | 2 | * Returns if the list contains a reference with the same hash as the provided reference. |
|
112 | 2 | * |
|
113 | * @since 0.1 |
||
114 | 2 | * |
|
115 | 2 | * @param Reference $reference |
|
116 | 2 | * |
|
117 | * @return bool |
||
118 | */ |
||
119 | 2 | public function hasReference( Reference $reference ) { |
|
122 | 2 | ||
123 | 2 | /** |
|
124 | 2 | * Returns the index of the Reference object or false if the Reference could not be found. |
|
125 | * |
||
126 | * @since 0.5 |
||
127 | * |
||
128 | * @param Reference $reference |
||
129 | * |
||
130 | * @return int|bool |
||
131 | */ |
||
132 | public function indexOf( Reference $reference ) { |
||
145 | |||
146 | /** |
||
147 | * Removes the reference with the same hash as the provided reference if such a reference exists in the list. |
||
148 | * |
||
149 | 2 | * @since 0.1 |
|
150 | 2 | * |
|
151 | * @param Reference $reference |
||
152 | 2 | */ |
|
153 | 1 | public function removeReference( Reference $reference ) { |
|
156 | 1 | ||
157 | 2 | /** |
|
158 | * Returns if the list contains a reference with the provided hash. |
||
159 | 2 | * |
|
160 | * @since 0.3 |
||
161 | * |
||
162 | * @param string $referenceHash |
||
163 | * |
||
164 | * @return bool |
||
165 | */ |
||
166 | public function hasReferenceHash( $referenceHash ) { |
||
169 | 3 | ||
170 | 3 | /** |
|
171 | 3 | * Looks for the first Reference object in this list with the provided hash. |
|
172 | * Removes all occurences of that object. |
||
173 | * |
||
174 | * @since 0.3 |
||
175 | * |
||
176 | * @param string $referenceHash ` |
||
177 | */ |
||
178 | public function removeReferenceHash( $referenceHash ) { |
||
191 | |||
192 | /** |
||
193 | 5 | * Returns the first Reference object with the provided hash, or |
|
194 | 5 | * null if there is no such reference in the list. |
|
195 | * |
||
196 | 5 | * @since 0.3 |
|
197 | 3 | * |
|
198 | 3 | * @param string $referenceHash |
|
199 | 5 | * |
|
200 | * @return Reference|null |
||
201 | */ |
||
202 | public function getReference( $referenceHash ) { |
||
211 | |||
212 | /** |
||
213 | * @see Serializable::serialize |
||
214 | 14 | * |
|
215 | 10 | * @since 2.1 |
|
216 | 10 | * |
|
217 | * @return string |
||
218 | 9 | */ |
|
219 | public function serialize() { |
||
222 | |||
223 | /** |
||
224 | * @see Serializable::unserialize |
||
225 | * |
||
226 | * @since 2.1 |
||
227 | * |
||
228 | * @param string $serialized |
||
229 | */ |
||
230 | 3 | public function unserialize( $serialized ) { |
|
233 | |||
234 | /** |
||
235 | * @since 4.4 |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function isEmpty() { |
||
240 | return empty( $this->references ); |
||
241 | 3 | } |
|
242 | 3 | ||
243 | 3 | /** |
|
244 | * The hash is purely valuer based. Order of the elements in the array is not held into account. |
||
245 | * |
||
246 | * @since 0.3 |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | 3 | public function getValueHash() { |
|
254 | |||
255 | /** |
||
256 | * @see Comparable::equals |
||
257 | * |
||
258 | * The comparison is done purely value based, ignoring the order of the elements in the array. |
||
259 | * |
||
260 | * @since 0.3 |
||
261 | * |
||
262 | * @param mixed $target |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function equals( $target ) { |
||
274 | |||
275 | /** |
||
276 | * @see Countable::count |
||
277 | * |
||
278 | * @return int |
||
279 | */ |
||
280 | public function count() { |
||
283 | |||
284 | /** |
||
285 | * @see IteratorAggregate::getIterator |
||
286 | * |
||
287 | * @since 5.0 |
||
288 | * |
||
289 | * @return Traversable |
||
290 | */ |
||
291 | public function getIterator() { |
||
294 | |||
295 | } |
||
296 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..