1 | <?php |
||
28 | class ReferenceList implements Comparable, Countable, IteratorAggregate, Serializable { |
||
29 | |||
30 | /** |
||
31 | * @var Reference[] |
||
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[] = $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 ) { |
||
105 | 2 | ||
106 | /** |
||
107 | * Returns if the list contains a reference with the same hash as the provided reference. |
||
108 | 2 | * |
|
109 | 2 | * @since 0.1 |
|
110 | 2 | * |
|
111 | 2 | * @param Reference $reference |
|
112 | 2 | * |
|
113 | * @return bool |
||
114 | 2 | */ |
|
115 | 2 | public function hasReference( Reference $reference ) { |
|
118 | |||
119 | 2 | /** |
|
120 | * Returns the index of the Reference object or false if the Reference could not be found. |
||
121 | 2 | * |
|
122 | 2 | * @since 0.5 |
|
123 | 2 | * |
|
124 | 2 | * @param Reference $reference |
|
125 | * |
||
126 | * @return int|bool |
||
127 | */ |
||
128 | public function indexOf( Reference $reference ) { |
||
137 | 6 | ||
138 | /** |
||
139 | * Removes the reference with the same hash as the provided reference if such a reference exists in the list. |
||
140 | * |
||
141 | * @since 0.1 |
||
142 | * |
||
143 | * @param Reference $reference |
||
144 | */ |
||
145 | public function removeReference( Reference $reference ) { |
||
148 | |||
149 | 2 | /** |
|
150 | 2 | * Returns if the list contains a reference with the provided hash. |
|
151 | * |
||
152 | 2 | * @since 0.3 |
|
153 | 1 | * |
|
154 | 1 | * @param string $referenceHash |
|
155 | * |
||
156 | 1 | * @return bool |
|
157 | 2 | */ |
|
158 | public function hasReferenceHash( $referenceHash ) { |
||
161 | |||
162 | /** |
||
163 | * Looks for the first Reference object in this list with the provided hash. |
||
164 | * Removes all occurences of that object. |
||
165 | * |
||
166 | * @since 0.3 |
||
167 | * |
||
168 | * @param string $referenceHash ` |
||
169 | 3 | */ |
|
170 | 3 | public function removeReferenceHash( $referenceHash ) { |
|
181 | |||
182 | 8 | /** |
|
183 | 8 | * Returns the first Reference object with the provided hash, or |
|
184 | * null if there is no such reference in the list. |
||
185 | * |
||
186 | * @since 0.3 |
||
187 | * |
||
188 | * @param string $referenceHash |
||
189 | * |
||
190 | * @return Reference|null |
||
191 | */ |
||
192 | public function getReference( $referenceHash ) { |
||
201 | |||
202 | /** |
||
203 | * @see Serializable::serialize |
||
204 | * |
||
205 | * @since 2.1 |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public function serialize() { |
||
212 | |||
213 | /** |
||
214 | 14 | * @see Serializable::unserialize |
|
215 | 10 | * |
|
216 | 10 | * @since 2.1 |
|
217 | * |
||
218 | 9 | * @param string $serialized |
|
219 | */ |
||
220 | 9 | public function unserialize( $serialized ) { |
|
223 | |||
224 | /** |
||
225 | * @since 4.4 |
||
226 | * |
||
227 | * @return bool |
||
228 | */ |
||
229 | public function isEmpty() { |
||
232 | |||
233 | /** |
||
234 | * The hash is purely valuer based. Order of the elements in the array is not held into account. |
||
235 | * |
||
236 | * @since 0.3 |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | public function getValueHash() { |
||
244 | |||
245 | /** |
||
246 | * @see Comparable::equals |
||
247 | * |
||
248 | * The comparison is done purely value based, ignoring the order of the elements in the array. |
||
249 | * |
||
250 | 3 | * @since 0.3 |
|
251 | 3 | * |
|
252 | * @param mixed $target |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | public function equals( $target ) { |
||
264 | |||
265 | /** |
||
266 | * @see Countable::count |
||
267 | * |
||
268 | * @return int |
||
269 | */ |
||
270 | public function count() { |
||
273 | |||
274 | /** |
||
275 | * @see IteratorAggregate::getIterator |
||
276 | * |
||
277 | * @since 5.0 |
||
278 | * |
||
279 | * @return Traversable |
||
280 | */ |
||
281 | public function getIterator() { |
||
284 | |||
285 | } |
||
286 |