Lines 100.00% 22 / 22
Functions and Methods 100.00% 8 / 8
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Collection 100.00% 22 / 22 100.00% 8 / 8 11 100.00% 1 / 1
 __construct 100.00% 3 / 3 100.00% 1 / 1 2
 key n/a 0 / 0 n/a 0 / 0 0
 add 100.00% 5 / 5 100.00% 1 / 1 2
 remove 100.00% 4 / 4 100.00% 1 / 1 2
 has 100.00% 1 / 1 100.00% 1 / 1 1
 find 100.00% 1 / 1 100.00% 1 / 1 1
 filter 100.00% 6 / 6 100.00% 1 / 1 1
 getIterator 100.00% 1 / 1 100.00% 1 / 1 1
 count 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Core\Support;
6
7use Closure;
8use Countable;
9use InvalidArgumentException;
10use Iterator;
11use IteratorAggregate;
12use Psl\Collection\MutableMap;
13use Psl\Option;
14
15/**
16 * @template K of PositiveInteger|NonEmptyString
17 * @template T
18 *
19 * @implements IteratorAggregate<array-key, T>
20 */
21abstract class Collection implements IteratorAggregate, Countable
22{
23    /** @var MutableMap<array-key, T> */
24    private MutableMap $items;
25
26    /**
27     * @param list<T> $items
28     *
29     * @throws InvalidArgumentException
30     */
31    final public function __construct(array $items)
32    {
33        $this->items = new MutableMap([]);
34        foreach ($items as $item) {
35            $this->add($item);
36        }
37    }
38
39    /**
40     * @param T $item
41     *
42     * @return K
43     */
44    abstract protected static function key(mixed $item): PositiveInteger|NonEmptyString;
45
46    /**
47     * @param T $item
48     *
49     * @return $this
50     *
51     * @throws InvalidArgumentException
52     */
53    public function add(mixed $item): static
54    {
55        $key = static::key($item)->value;
56        if ($this->items->contains($key)) {
57            throw new InvalidArgumentException("{$key} is in the collection more than once.");
58        }
59
60        $this->items->add($key, $item);
61
62        return $this;
63    }
64
65    /**
66     * @param K $key
67     *
68     * @return $this
69     *
70     * @throws InvalidArgumentException
71     */
72    public function remove(PositiveInteger|NonEmptyString $key): static
73    {
74        if (!$this->items->contains($key->value)) {
75            throw new InvalidArgumentException("{$key} is not in the collection.");
76        }
77
78        $this->items->remove($key->value);
79
80        return $this;
81    }
82
83    /** @param T $item */
84    public function has(mixed $item): bool
85    {
86        return $this->items->contains(static::key($item)->value);
87    }
88
89    /**
90     * @param K $key
91     *
92     * @return Option\Option<T>
93     */
94    public function find(PositiveInteger|NonEmptyString $key): Option\Option
95    {
96        return Option\from_nullable($this->items->get($key->value));
97    }
98
99    /**
100     * @param Closure(T): bool $fn
101     *
102     * @throws InvalidArgumentException
103     */
104    public function filter(Closure $fn): static
105    {
106        /** @var static<K, T> */
107        return new static(
108            $this->items
109                ->filter($fn)
110                ->values()
111                ->toArray(),
112        );
113    }
114
115    /** @return Iterator<array-key, T> */
116    public function getIterator(): Iterator
117    {
118        return $this->items->getIterator();
119    }
120
121    public function count(): int
122    {
123        return $this->items->count();
124    }
125}