Passed
Pull Request — master (#1676)
by Arnaud
08:22 queued 03:21
created

Collection::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 3
cts 5
cp 0.6
crap 2.2559
rs 10
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 1
    public function has(string $id): bool
65
    {
66 1
        $result = $this->searchItem($id);
67 1
        if (is_array($result) && !empty($result)) {
68 1
            return true;
69
        }
70
71 1
        return false;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     *
77
     * @throws \DomainException
78
     */
79 1
    public function add(ItemInterface $item): CollectionInterface
80
    {
81 1
        if ($this->has($item->getId())) {
82 1
            throw new \DomainException(\sprintf('Failed adding "%s" in "%s" collection: item already exists.', $item->getId(), $this->getId()));
83
        }
84 1
        $this->items[] = $item;
85
86 1
        return $this;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     *
92
     * @throws \DomainException
93
     */
94 1
    public function replace(string $id, ItemInterface $item): CollectionInterface
95
    {
96
        try {
97 1
            $this->items[$this->getPosition($id)] = $item;
98
        } catch (\Throwable $th) {
99
            throw new \DomainException(\sprintf('Failed replacing "%s" in "%s" collection: item does not exist.', $item->getId(), $this->getId()));
100
        }
101
102 1
        return $this;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     *
108
     * @throws \DomainException
109
     */
110 1
    public function remove(string $id): CollectionInterface
111
    {
112
        try {
113 1
            unset($this->items[$this->getPosition($id)]);
114
        } catch (\Throwable $th) {
115
            throw new \DomainException(\sprintf('Failed removing "%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 get(string $id): ItemInterface
127
    {
128
        try {
129 1
            return $this->items[$this->getPosition($id)];
130
        } catch (\Throwable $th) {
131
            throw new \DomainException(\sprintf('Failed getting "%s" in "%s" collection: item does not exist.', $id, $this->getId()));
132
        }
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     *
138
     * @throws \DomainException
139
     */
140 1
    public function getPosition(string $id): int
141
    {
142 1
        $result = $this->searchItem($id);
143 1
        $position = key($result);
144 1
        if (!is_int($position)) {
145
            throw new \DomainException(\sprintf('Failed getting position of "%s" in "%s" collection: item does not exist.', $id, $this->getId()));
146
        }
147
148 1
        return $position;
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
    #[\ReturnTypeWillChange]
265 1
    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
    #[\ReturnTypeWillChange]
278 1
    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