Passed
Pull Request — master (#1676)
by Arnaud
09:49 queued 04:14
created

Collection::filter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Collection;
15
16
/**
17
 * Class Collection.
18
 */
19
class Collection implements CollectionInterface
20
{
21
    /** @var string Collection's identifier. */
22
    protected $id;
23
24
    /** @var array Collection's items. */
25
    protected $items = [];
26
27 1
    public function __construct(string $id, array $items = [])
28
    {
29 1
        $this->setId($id);
30 1
        $this->items = $items;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public function setId(string $id): BaseInterface
37
    {
38 1
        $this->id = $id;
39
40 1
        return $this;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    public function getId(): string
47
    {
48 1
        return $this->id;
49
    }
50
51
    /**
52
     * Search an item by ID.
53
     */
54 1
    protected function searchItem(string $id): ?array
55
    {
56 1
        return array_filter($this->items, function (ItemInterface $item) use ($id) {
57 1
            return $item->getId() == $id;
58 1
        });
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @throws \DomainException
65
     */
66 1
    public function getPosition(string $id): int
67
    {
68 1
        $result = $this->searchItem($id);
69 1
        $position = key($result);
70 1
        if (!\is_int($position)) {
71
            throw new \DomainException(sprintf('"%s" does not exist in "%s" collection.', $id, $this->getId()));
72
        }
73
74 1
        return $position;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 1
    public function has(string $id): bool
81
    {
82 1
        $result = $this->searchItem($id);
83 1
        if (\is_array($result) && !empty($result)) {
84 1
            return true;
85
        }
86
87 1
        return false;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     *
93
     * @throws \DomainException
94
     */
95 1
    public function add(ItemInterface $item): CollectionInterface
96
    {
97 1
        if ($this->has($item->getId())) {
98 1
            throw new \DomainException(sprintf('Failed adding "%s" in "%s" collection: item already exists.', $item->getId(), $this->getId()));
99
        }
100 1
        $this->items[] = $item;
101
102 1
        return $this;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     *
108
     * @throws \DomainException
109
     */
110 1
    public function replace(string $id, ItemInterface $item): CollectionInterface
111
    {
112
        try {
113 1
            $this->items[$this->getPosition($id)] = $item;
114
        } catch (\DomainException $e) {
115
            throw new \DomainException(sprintf('Failed replacing "%s" in "%s" collection: item does not exist.', $id, $this->getId()));
116
        }
117
118 1
        return $this;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     *
124
     * @throws \DomainException
125
     */
126 1
    public function remove(string $id): CollectionInterface
127
    {
128
        try {
129 1
            unset($this->items[$this->getPosition($id)]);
130
        } catch (\DomainException $e) {
131
            throw new \DomainException(sprintf('Failed removing "%s" in "%s" collection: item does not exist.', $id, $this->getId()));
132
        }
133
134 1
        return $this;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     *
140
     * @throws \DomainException
141
     */
142 1
    public function get(string $id): ItemInterface
143
    {
144
        try {
145 1
            return $this->items[$this->getPosition($id)];
146
        } catch (\DomainException $e) {
147
            throw new \DomainException(sprintf('Failed getting "%s" in "%s" collection: item does not exist.', $id, $this->getId()));
148
        }
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function keys(): array
155
    {
156
        return array_keys($this->items);
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 1
    public function first(): ?ItemInterface
163
    {
164 1
        if (\count($this->items) < 1) {
165
            return null;
166
        }
167 1
        $items = $this->items;
168
169 1
        return array_shift($items);
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function last(): ?ItemInterface
176
    {
177
        if (\count($this->items) < 1) {
178
            return null;
179
        }
180
        $items = $this->items;
181
182
        return array_pop($items);
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188 1
    public function count(): int
189
    {
190 1
        return \count($this->items);
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 1
    public function toArray(): array
197
    {
198 1
        return $this->items;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function toJson(): string
205
    {
206
        return sprintf("%s\n", json_encode($this->items));
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212 1
    public function getIterator(): \ArrayIterator
213
    {
214 1
        return new \ArrayIterator($this->items);
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220 1
    public function usort(\Closure $callback = null): CollectionInterface
221
    {
222 1
        $callback ? usort($this->items, $callback) : usort($this->items, function ($a, $b) {
223
            if ($a == $b) {
224
                return 0;
225
            }
226
227
            return ($a < $b) ? -1 : 1;
228 1
        });
229
230 1
        return new static($this->getId(), $this->items); /** @phpstan-ignore-line */
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236
    public function reverse(): CollectionInterface
237
    {
238
        return new static($this->getId(), array_reverse($this->items)); /** @phpstan-ignore-line */
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244 1
    public function filter(\Closure $callback): CollectionInterface
245
    {
246 1
        return new static($this->getId(), array_filter($this->items, $callback)); /** @phpstan-ignore-line */
247
    }
248
249
    /**
250
     * {@inheritdoc}
251
     */
252 1
    public function map(\Closure $callback): CollectionInterface
253
    {
254 1
        return new static($this->getId(), array_map($callback, $this->items)); /** @phpstan-ignore-line */
255
    }
256
257
    /**
258
     * Implements \ArrayAccess.
259
     *
260
     * @param string $offset
261
     *
262
     * @return bool
263
     */
264 1
    #[\ReturnTypeWillChange]
265
    public function offsetExists($offset): bool
266
    {
267 1
        return $this->has((string) $offset);
268
    }
269
270
    /**
271
     * Implements \ArrayAccess.
272
     *
273
     * @param string $offset
274
     *
275
     * @return CollectionInterface|ItemInterface|null
276
     */
277 1
    #[\ReturnTypeWillChange]
278
    public function offsetGet($offset)
279
    {
280 1
        return $this->get((string) $offset);
281
    }
282
283
    /**
284
     * Implements \ArrayAccess.
285
     *
286
     * @param mixed         $offset
287
     * @param ItemInterface $value
288
     *
289
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
290
     */
291
    #[\ReturnTypeWillChange]
292
    public function offsetSet($offset, $value): void
293
    {
294
        $this->add($value);
295
    }
296
297
    /**
298
     * Implements \ArrayAccess.
299
     *
300
     * @param string $offset
301
     *
302
     * @return void
303
     */
304
    #[\ReturnTypeWillChange]
305
    public function offsetUnset($offset): void
306
    {
307
        $this->remove($offset);
308
    }
309
310
    /**
311
     * Returns the collection ID.
312
     *
313
     * @return string
314
     */
315 1
    public function __toString()
316
    {
317 1
        return $this->getId();
318
    }
319
}
320