1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object\Domain |
8
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
9
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/*********************************************************************************** |
14
|
|
|
* The MIT License (MIT) |
15
|
|
|
* |
16
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
17
|
|
|
* |
18
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
19
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
20
|
|
|
* the Software without restriction, including without limitation the rights to |
21
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
22
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
23
|
|
|
* subject to the following conditions: |
24
|
|
|
* |
25
|
|
|
* The above copyright notice and this permission notice shall be included in all |
26
|
|
|
* copies or substantial portions of the Software. |
27
|
|
|
* |
28
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
29
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
30
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
31
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
32
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
33
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
34
|
|
|
***********************************************************************************/ |
35
|
|
|
|
36
|
|
|
namespace Apparat\Object\Domain\Model\Object; |
37
|
|
|
|
38
|
|
|
use Apparat\Object\Domain\Model\Path\RepositoryPath; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Lazy loading object collection |
42
|
|
|
* |
43
|
|
|
* @package Apparat\Object |
44
|
|
|
* @subpackage Apparat\Object\Domain |
45
|
|
|
*/ |
46
|
|
|
class Collection implements CollectionInterface |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* Objects |
50
|
|
|
* |
51
|
|
|
* @var RepositoryPath[]|ObjectInterface[] |
52
|
|
|
*/ |
53
|
|
|
protected $objects = array(); |
54
|
|
|
/** |
55
|
|
|
* Object IDs |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $objectIds = array(); |
60
|
|
|
/** |
61
|
|
|
* Internal object pointer |
62
|
|
|
* |
63
|
|
|
* @var int |
64
|
|
|
*/ |
65
|
|
|
protected $pointer = 0; |
66
|
|
|
|
67
|
|
|
/******************************************************************************* |
68
|
|
|
* PUBLIC METHODS |
69
|
|
|
*******************************************************************************/ |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Collection constructor |
73
|
|
|
* |
74
|
|
|
* @param array $objects Collection objects |
75
|
|
|
* @throws InvalidArgumentException If the an invalid object or path is provided |
76
|
|
|
*/ |
77
|
7 |
|
public function __construct(array $objects = []) |
78
|
|
|
{ |
79
|
7 |
|
foreach ($objects as $object) { |
80
|
|
|
// If it's an object |
81
|
7 |
|
if ($object instanceof ObjectInterface) { |
82
|
3 |
|
$this->objects[$object->getId()->getId()] = $object; |
83
|
3 |
|
continue; |
84
|
|
|
|
85
|
|
|
// Else if it's an object path |
86
|
|
|
} elseif ($object instanceof RepositoryPath) { |
87
|
7 |
|
$this->objects[$object->getId()->getId()] = $object; |
88
|
7 |
|
continue; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
throw new InvalidArgumentException( |
92
|
1 |
|
'Invalid collection object or path', |
93
|
1 |
|
InvalidArgumentException::INVALID_COLLECTION_OBJECT_OR_PATH |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
7 |
|
$this->objectIds = array_keys($this->objects); |
98
|
7 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return the current object |
102
|
|
|
* |
103
|
|
|
* @return ObjectInterface Current object |
104
|
|
|
*/ |
105
|
3 |
|
public function current() |
106
|
|
|
{ |
107
|
3 |
|
return $this->loadObject($this->objectIds[$this->pointer]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Load and return an object by ID |
112
|
|
|
* |
113
|
|
|
* @param int $objectId Object ID |
114
|
|
|
* @return ObjectInterface Object |
115
|
|
|
*/ |
116
|
3 |
|
protected function loadObject($objectId) |
117
|
|
|
{ |
118
|
|
|
// Lazy-load the object once |
119
|
3 |
|
$object = $this->objects[$objectId]; |
120
|
3 |
|
if ($object instanceof RepositoryPath) { |
121
|
3 |
|
$object = $this->objects[$objectId] = $object->getRepository()->loadObject($object); |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
return $object; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Move forward to next object |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
1 |
|
public function next() |
133
|
|
|
{ |
134
|
1 |
|
++$this->pointer; |
135
|
1 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Return the ID of the current object |
139
|
|
|
* |
140
|
|
|
* @return int Object ID |
141
|
|
|
*/ |
142
|
1 |
|
public function key() |
143
|
|
|
{ |
144
|
1 |
|
return $this->objectIds[$this->pointer]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Checks if current position is valid |
149
|
|
|
* |
150
|
|
|
* @return boolean The current position is valid |
151
|
|
|
*/ |
152
|
3 |
|
public function valid() |
153
|
|
|
{ |
154
|
3 |
|
return isset($this->objectIds[$this->pointer]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Rewind the Iterator to the first object |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
3 |
|
public function rewind() |
163
|
|
|
{ |
164
|
3 |
|
$this->pointer = 0; |
165
|
3 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Whether an object ID exists |
169
|
|
|
* |
170
|
|
|
* @param int $offset Object ID |
171
|
|
|
* @return boolean Whether the object ID exists |
172
|
|
|
*/ |
173
|
1 |
|
public function offsetExists($offset) |
174
|
|
|
{ |
175
|
1 |
|
return isset($this->objects[$offset]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get an object with a particular ID |
180
|
|
|
* |
181
|
|
|
* @param int $offset Object ID |
182
|
|
|
* @return RepositoryPath|ObjectInterface Object |
183
|
|
|
*/ |
184
|
2 |
|
public function offsetGet($offset) |
185
|
|
|
{ |
186
|
2 |
|
return $this->objects[$offset]; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Set an object by ID |
191
|
|
|
* |
192
|
|
|
* @param int $offset Object ID |
193
|
|
|
* @param ObjectInterface $value Object |
194
|
|
|
* @throws RuntimeException When an object should be set by ID |
195
|
|
|
*/ |
196
|
1 |
|
public function offsetSet($offset, $value) |
197
|
|
|
{ |
198
|
1 |
|
throw new RuntimeException( |
199
|
|
|
sprintf( |
200
|
1 |
|
'Cannot modify collection by index (%s / %s). Use add() / remove() instead', |
201
|
|
|
$offset, |
202
|
|
|
gettype($value) |
203
|
|
|
), |
204
|
1 |
|
RuntimeException::CANNOT_MODIFY_COLLECTION_BY_INDEX |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Unset an object by ID |
210
|
|
|
* |
211
|
|
|
* @param int $offset Object ID |
212
|
|
|
* @throws RuntimeException When an object should be set by ID |
213
|
|
|
*/ |
214
|
1 |
|
public function offsetUnset($offset) |
215
|
|
|
{ |
216
|
1 |
|
throw new RuntimeException( |
217
|
1 |
|
sprintf('Cannot modify collection by index (%s). Use add() / remove() instead', $offset), |
218
|
1 |
|
RuntimeException::CANNOT_MODIFY_COLLECTION_BY_INDEX |
219
|
|
|
); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Add an object to the collection |
224
|
|
|
* |
225
|
|
|
* @param string|ObjectInterface $object Object or object URL |
226
|
|
|
* @return Collection Modified object collection |
227
|
|
|
*/ |
228
|
2 |
|
public function add($object) |
229
|
|
|
{ |
230
|
2 |
|
$objects = $this->objects; |
231
|
2 |
|
$objects[] = $object; |
232
|
2 |
|
return new self(array_values($objects)); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Remove an object out of this collection |
237
|
|
|
* |
238
|
|
|
* @param string|ObjectInterface $object Object or object ID |
239
|
|
|
* @return Collection Modified object collection |
240
|
|
|
*/ |
241
|
1 |
|
public function remove($object) |
242
|
|
|
{ |
243
|
1 |
|
$object = ($object instanceof ObjectInterface) ? $object->getId()->getId() : intval($object); |
244
|
1 |
|
if (empty($this->objects[$object])) { |
245
|
1 |
|
throw new InvalidArgumentException( |
246
|
1 |
|
sprintf('Unknown object ID "%s"', $object), |
247
|
1 |
|
InvalidArgumentException::UNKNOWN_OBJECT_ID |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
1 |
|
$objects = $this->objects; |
252
|
1 |
|
unset($objects[$object]); |
253
|
1 |
|
return new self(array_values($objects)); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Count objects in this collection |
258
|
|
|
* |
259
|
|
|
* @return int The number of objects in this collection |
260
|
|
|
*/ |
261
|
6 |
|
public function count() |
262
|
|
|
{ |
263
|
6 |
|
return count($this->objects); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/******************************************************************************* |
267
|
|
|
* PRIVATE METHODS |
268
|
|
|
*******************************************************************************/ |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Append another collection |
272
|
|
|
* |
273
|
|
|
* @param Collection $collection Collection |
274
|
|
|
* @return Collection Combined collections |
275
|
|
|
*/ |
276
|
1 |
|
public function append(Collection $collection) |
277
|
|
|
{ |
278
|
1 |
|
$objects = array_merge($this->objects, $collection->objects); |
279
|
1 |
|
return new self(array_values($objects)); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|