1 | <?php |
||
48 | class Collection implements CollectionInterface |
||
49 | { |
||
50 | /** |
||
51 | * Objects |
||
52 | * |
||
53 | * @var ObjectInterface[]|RepositoryPath[] |
||
54 | */ |
||
55 | protected $_objects = array(); |
||
56 | /** |
||
57 | * Object IDs |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $_objectIds = array(); |
||
62 | /** |
||
63 | * Internal object pointer |
||
64 | * |
||
65 | * @var int |
||
66 | */ |
||
67 | protected $_pointer = 0; |
||
68 | |||
69 | /******************************************************************************* |
||
70 | * PUBLIC METHODS |
||
71 | *******************************************************************************/ |
||
72 | |||
73 | /** |
||
74 | * Collection constructor |
||
75 | * |
||
76 | * @param array $objects Collection objects |
||
77 | * @throws InvalidArgumentException If the an invalid object or path is provided |
||
78 | */ |
||
79 | 2 | public function __construct(array $objects = []) |
|
80 | { |
||
81 | 2 | foreach ($objects as $object) { |
|
82 | |||
83 | // If it's an object |
||
84 | 2 | if ($object instanceof ObjectInterface) { |
|
85 | $this->_objects[$object->getId()->getId()] = $object; |
||
86 | |||
87 | // Else if it's an object path |
||
88 | } elseif ($object instanceof RepositoryPath) { |
||
89 | 2 | $this->_objects[$object->getId()->getId()] = $object; |
|
90 | |||
91 | // Else: Error |
||
92 | } else { |
||
93 | throw new InvalidArgumentException( |
||
94 | 'Invalid collection object or path', |
||
95 | 2 | InvalidArgumentException::INVALID_COLLECTION_OBJECT_OR_PATH |
|
96 | ); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | 2 | $this->_objectIds = array_keys($this->_objects); |
|
101 | 2 | } |
|
102 | |||
103 | /** |
||
104 | * Return the current object |
||
105 | * |
||
106 | * @return ObjectInterface Current object |
||
107 | */ |
||
108 | public function current() |
||
112 | |||
113 | /** |
||
114 | * Move forward to next object |
||
115 | * |
||
116 | * @return void |
||
117 | */ |
||
118 | public function next() |
||
122 | |||
123 | /** |
||
124 | * Return the ID of the current object |
||
125 | * |
||
126 | * @return int Object ID |
||
127 | */ |
||
128 | public function key() |
||
132 | |||
133 | /** |
||
134 | * Checks if current position is valid |
||
135 | * |
||
136 | * @return boolean The current position is valid |
||
137 | */ |
||
138 | public function valid() |
||
142 | |||
143 | /** |
||
144 | * Rewind the Iterator to the first object |
||
145 | * |
||
146 | * @return void |
||
147 | */ |
||
148 | public function rewind() |
||
152 | |||
153 | /** |
||
154 | * Whether an object ID exists |
||
155 | * |
||
156 | * @param int $offset Object ID |
||
157 | * @return boolean Whether the object ID exists |
||
158 | */ |
||
159 | public function offsetExists($offset) |
||
163 | |||
164 | /** |
||
165 | * Get an object with a particular ID |
||
166 | * |
||
167 | * @param int $offset Object ID |
||
168 | * @return ObjectInterface Object |
||
169 | */ |
||
170 | public function offsetGet($offset) |
||
174 | |||
175 | /** |
||
176 | * Set an object by ID |
||
177 | * |
||
178 | * @param int $offset Object ID |
||
179 | * @param ObjectInterface $value Object |
||
180 | * @return void |
||
181 | * @throws RuntimeException When an object should be set by ID |
||
182 | */ |
||
183 | public function offsetSet($offset, $value) |
||
187 | |||
188 | /** |
||
189 | * Unset an object by ID |
||
190 | * |
||
191 | * @param int $offset Object ID |
||
192 | * @return Collection Object collection with the object removed |
||
193 | */ |
||
194 | public function offsetUnset($offset) |
||
200 | |||
201 | /** |
||
202 | * Add an object to the collection |
||
203 | * |
||
204 | * @param string|ObjectInterface $object Object or object URL |
||
205 | * @return Collection Modified object collection |
||
206 | */ |
||
207 | public function addObject($object) |
||
208 | { |
||
209 | |||
210 | // If the object is not yet an object instance |
||
211 | if (!($object instanceof ObjectInterface)) { |
||
212 | if (!($object instanceof PathInterface)) { |
||
213 | $object = new LocalPath(strval($object)); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | $objects = $this->_objects; |
||
218 | $objects[] = $object; |
||
219 | return new self(array_values($objects)); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Remove an object out of this collection |
||
224 | * |
||
225 | * @param string|ObjectInterface $object Object or object ID |
||
226 | * @return Collection Modified object collection |
||
227 | */ |
||
228 | public function removeObject($object) |
||
229 | { |
||
230 | if ($object instanceof ObjectInterface) { |
||
231 | $object = $object->getId(); |
||
232 | } else { |
||
233 | $object = intval($object); |
||
234 | } |
||
235 | if (empty($this->_objects[$object])) { |
||
236 | throw new InvalidArgumentException( |
||
237 | sprintf('Unknown object ID "%s"', $object), |
||
238 | InvalidArgumentException::UNKNOWN_OBJECT_ID |
||
239 | ); |
||
240 | } |
||
241 | |||
242 | $objects = $this->_objects; |
||
243 | unset($objects[$object]); |
||
244 | return new self(array_values($objects)); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Count objects in this collection |
||
249 | * |
||
250 | * @return int The number of objects in this collection |
||
251 | */ |
||
252 | 2 | public function count() |
|
256 | |||
257 | /** |
||
258 | * Append another collection |
||
259 | * |
||
260 | * @param Collection $collection Collection |
||
261 | * @return Collection Combined collections |
||
262 | */ |
||
263 | public function append(Collection $collection) |
||
268 | |||
269 | /******************************************************************************* |
||
270 | * PRIVATE METHODS |
||
271 | *******************************************************************************/ |
||
272 | |||
273 | /** |
||
274 | * Load and return an object by ID |
||
275 | * |
||
276 | * @param int $objectId Object ID |
||
277 | * @return ObjectInterface Object |
||
278 | */ |
||
279 | protected function _loadObject($objectId) |
||
290 | } |
||
291 |