1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/repository package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Puli\Repository\Resource\Collection; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use IteratorAggregate; |
16
|
|
|
use OutOfBoundsException; |
17
|
|
|
use Puli\Repository\Api\Resource\PuliResource; |
18
|
|
|
use Puli\Repository\Api\ResourceCollection; |
19
|
|
|
use Puli\Repository\Api\UnsupportedResourceException; |
20
|
|
|
use Puli\Repository\Resource\Iterator\ResourceCollectionIterator; |
21
|
|
|
use Traversable; |
22
|
|
|
use Webmozart\Assert\Assert; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* A collection of {@link PuliResource} instances backed by an array. |
26
|
|
|
* |
27
|
|
|
* @since 1.0 |
28
|
|
|
* |
29
|
|
|
* @author Bernhard Schussek <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class ArrayResourceCollection implements IteratorAggregate, ResourceCollection |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var PuliResource[]|Traversable |
35
|
|
|
*/ |
36
|
|
|
private $resources; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates a new collection. |
40
|
|
|
* |
41
|
|
|
* You can pass the resources that you want to initially store in the |
42
|
|
|
* collection as argument. |
43
|
|
|
* |
44
|
|
|
* @param PuliResource[]|Traversable $resources The resources to store in the collection. |
45
|
|
|
* |
46
|
|
|
* @throws InvalidArgumentException If the resources are not an array |
47
|
|
|
* and not a traversable object. |
48
|
|
|
* @throws UnsupportedResourceException If a resource does not implement |
49
|
|
|
* {@link PuliResource}. |
50
|
|
|
*/ |
51
|
682 |
|
public function __construct($resources = array()) |
52
|
|
|
{ |
53
|
682 |
|
$this->replace($resources); |
54
|
679 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
4 |
|
public function add(PuliResource $resource) |
60
|
|
|
{ |
61
|
4 |
|
$this->resources[] = $resource; |
62
|
4 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
91 |
|
public function set($key, PuliResource $resource) |
68
|
|
|
{ |
69
|
91 |
|
$this->resources[$key] = $resource; |
70
|
91 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
83 |
|
public function get($key) |
76
|
|
|
{ |
77
|
83 |
|
if (!isset($this->resources[$key])) { |
78
|
1 |
|
throw new OutOfBoundsException(sprintf( |
79
|
1 |
|
'The offset "%s" does not exist.', |
80
|
|
|
$key |
81
|
|
|
)); |
82
|
|
|
} |
83
|
|
|
|
84
|
82 |
|
return $this->resources[$key]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
2 |
|
public function remove($key) |
91
|
|
|
{ |
92
|
2 |
|
unset($this->resources[$key]); |
93
|
2 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
1 |
|
public function has($key) |
99
|
|
|
{ |
100
|
1 |
|
return isset($this->resources[$key]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
1 |
|
public function clear() |
107
|
|
|
{ |
108
|
1 |
|
$this->resources = array(); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function keys() |
115
|
|
|
{ |
116
|
|
|
return array_keys($this->resources); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
682 |
|
public function replace($resources) |
123
|
|
|
{ |
124
|
682 |
|
Assert::allIsInstanceOf($resources, 'Puli\Repository\Api\Resource\PuliResource'); |
125
|
|
|
|
126
|
679 |
|
$this->resources = is_array($resources) ? $resources : iterator_to_array($resources); |
127
|
679 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
3 |
|
public function merge($resources) |
133
|
|
|
{ |
134
|
3 |
|
Assert::allIsInstanceOf($resources, 'Puli\Repository\Api\Resource\PuliResource'); |
135
|
|
|
|
136
|
|
|
// only start merging after validating all resources |
137
|
1 |
|
foreach ($resources as $resource) { |
138
|
1 |
|
$this->resources[] = $resource; |
139
|
|
|
} |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
1 |
|
public function isEmpty() |
146
|
|
|
{ |
147
|
1 |
|
return 0 === count($this->resources); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function offsetExists($key) |
154
|
|
|
{ |
155
|
|
|
return $this->has($key); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
73 |
|
public function offsetGet($key) |
162
|
|
|
{ |
163
|
73 |
|
return $this->get($key); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
91 |
|
public function offsetSet($key, $value) |
170
|
|
|
{ |
171
|
91 |
|
if (null !== $key) { |
172
|
90 |
|
$this->set($key, $value); |
173
|
|
|
} else { |
174
|
1 |
|
$this->add($value); |
175
|
|
|
} |
176
|
91 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
|
|
public function offsetUnset($key) |
182
|
|
|
{ |
183
|
|
|
$this->remove($key); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
*/ |
189
|
|
|
public function getPaths() |
190
|
|
|
{ |
191
|
|
|
return array_map( |
192
|
|
|
function (PuliResource $resource) { return $resource->getPath(); }, |
193
|
|
|
$this->resources |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
|
|
public function getNames() |
201
|
|
|
{ |
202
|
|
|
return array_map( |
203
|
|
|
function (PuliResource $resource) { return $resource->getName(); }, |
204
|
|
|
$this->resources |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
145 |
|
public function count() |
209
|
|
|
{ |
210
|
145 |
|
return count($this->resources); |
211
|
|
|
} |
212
|
|
|
|
213
|
630 |
|
public function getIterator($mode = ResourceCollectionIterator::KEY_AS_CURSOR) |
214
|
|
|
{ |
215
|
630 |
|
return new ResourceCollectionIterator($this, $mode); |
216
|
|
|
} |
217
|
|
|
|
218
|
659 |
|
public function toArray() |
219
|
|
|
{ |
220
|
659 |
|
return $this->resources; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|