1 | <?php |
||
28 | class ReferenceList implements Comparable, Countable, IteratorAggregate, Serializable { |
||
29 | |||
30 | /** |
||
31 | * @var Reference[] |
||
32 | */ |
||
33 | private $references = array(); |
||
34 | |||
35 | /** |
||
36 | * @param Reference[]|Traversable $references |
||
37 | * |
||
38 | * @throws InvalidArgumentException |
||
39 | */ |
||
40 | public function __construct( $references = array() ) { |
||
41 | if ( !is_array( $references ) && !( $references instanceof Traversable ) ) { |
||
42 | throw new InvalidArgumentException( '$references must be an array or an instance of Traversable' ); |
||
43 | } |
||
44 | |||
45 | foreach ( $references as $reference ) { |
||
46 | 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 | * @since 0.1 |
||
59 | * |
||
60 | * @param Reference $reference |
||
61 | * @param int|null $index |
||
62 | * |
||
63 | * @throws InvalidArgumentException |
||
64 | */ |
||
65 | public function addReference( Reference $reference, $index = null ) { |
||
66 | if ( $index !== null && ( !is_int( $index ) || $index < 0 ) ) { |
||
67 | throw new InvalidArgumentException( '$index must be a non-negative integer or null' ); |
||
68 | } |
||
69 | |||
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 | } else { |
||
78 | $this->insertReferenceAtIndex( $reference, $index ); |
||
79 | } |
||
80 | } |
||
81 | |||
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 | |||
98 | /** |
||
99 | * @param Reference $reference |
||
100 | * @param int $index |
||
101 | */ |
||
102 | private function insertReferenceAtIndex( Reference $reference, $index ) { |
||
105 | |||
106 | /** |
||
107 | * Returns if the list contains a reference with the same hash as the provided reference. |
||
108 | * |
||
109 | * @since 0.1 |
||
110 | * |
||
111 | * @param Reference $reference |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | public function hasReference( Reference $reference ) { |
||
118 | |||
119 | /** |
||
120 | * Returns the index of the Reference object or false if the Reference could not be found. |
||
121 | * |
||
122 | * @since 0.5 |
||
123 | * |
||
124 | * @param Reference $reference |
||
125 | * |
||
126 | * @return int|bool |
||
127 | */ |
||
128 | public function indexOf( Reference $reference ) { |
||
137 | |||
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 | /** |
||
150 | * Returns if the list contains a reference with the provided hash. |
||
151 | * |
||
152 | * @since 0.3 |
||
153 | * |
||
154 | * @param string $referenceHash |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
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 | */ |
||
170 | public function removeReferenceHash( $referenceHash ) { |
||
185 | |||
186 | /** |
||
187 | * Returns the first Reference object with the provided hash, or |
||
188 | * null if there is no such reference in the list. |
||
189 | * |
||
190 | * @since 0.3 |
||
191 | * |
||
192 | * @param string $referenceHash |
||
193 | * |
||
194 | * @return Reference|null |
||
195 | */ |
||
196 | public function getReference( $referenceHash ) { |
||
205 | |||
206 | /** |
||
207 | * @see Serializable::serialize |
||
208 | * |
||
209 | * @since 2.1 |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | public function serialize() { |
||
216 | |||
217 | /** |
||
218 | * @see Serializable::unserialize |
||
219 | * |
||
220 | * @since 2.1 |
||
221 | * |
||
222 | * @param string $serialized |
||
223 | */ |
||
224 | public function unserialize( $serialized ) { |
||
227 | |||
228 | /** |
||
229 | * @since 4.4 |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | public function isEmpty() { |
||
236 | |||
237 | /** |
||
238 | * The hash is purely valuer based. Order of the elements in the array is not held into account. |
||
239 | * |
||
240 | * @since 0.3 |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | public function getValueHash() { |
||
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 Countable::count |
||
271 | * |
||
272 | * @return int |
||
273 | */ |
||
274 | public function count() { |
||
277 | |||
278 | /** |
||
279 | * @see IteratorAggregate::getIterator |
||
280 | * |
||
281 | * @since 5.0 |
||
282 | * |
||
283 | * @return Traversable |
||
284 | */ |
||
285 | public function getIterator() { |
||
288 | |||
289 | } |
||
290 |