1 | <?php |
||
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 | 692 | public function __construct($resources = array()) |
|
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | 4 | public function add(PuliResource $resource) |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 97 | public function set($key, PuliResource $resource) |
|
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) |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 1 | public function has($key) |
|
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | 1 | public function clear() |
|
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function keys() |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 692 | public function replace($resources) |
|
123 | { |
||
124 | 692 | Assert::allIsInstanceOf($resources, 'Puli\Repository\Api\Resource\PuliResource'); |
|
125 | |||
126 | 689 | $this->resources = is_array($resources) ? $resources : iterator_to_array($resources); |
|
127 | 689 | } |
|
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() |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | public function offsetExists($key) |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 73 | public function offsetGet($key) |
|
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | 97 | public function offsetSet($key, $value) |
|
170 | { |
||
171 | 97 | if (null !== $key) { |
|
172 | 96 | $this->set($key, $value); |
|
173 | } else { |
||
174 | 1 | $this->add($value); |
|
175 | } |
||
176 | 97 | } |
|
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | */ |
||
181 | public function offsetUnset($key) |
||
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() |
|
212 | |||
213 | 640 | public function getIterator($mode = ResourceCollectionIterator::KEY_AS_CURSOR) |
|
217 | |||
218 | 669 | public function toArray() |
|
222 | } |
||
223 |