Lines 100.00% 8 / 8
Functions and Methods 100.00% 5 / 5
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
RegistryIdCollection 100.00% 8 / 8 100.00% 5 / 5 7 100.00% 1 / 1
 contains 100.00% 1 / 1 100.00% 1 / 1 1
 containsAnyOf 100.00% 1 / 1 100.00% 1 / 1 1
 withAddedMembers 100.00% 4 / 4 100.00% 1 / 1 3
 withoutMembers 100.00% 1 / 1 100.00% 1 / 1 1
 toStrings 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Contracts\Registry\Collection;
6
7use Stewart\Contracts\Collection\ListCollection;
8use Stewart\Contracts\Registry\RegistryId;
9
10/**
11 * @template T of RegistryId
12 * @extends ListCollection<T>
13 */
14abstract readonly class RegistryIdCollection extends ListCollection
15{
16    /** @param T $id */
17    public function contains(RegistryId $id): bool
18    {
19        return $this->containsWhere(static fn(RegistryId $member): bool => $member->equals($id));
20    }
21
22    /** @param static $other */
23    public function containsAnyOf(self $other): bool
24    {
25        return $other->containsWhere($this->contains(...));
26    }
27
28    /** @param static $ids */
29    public function withAddedMembers(self $ids): static
30    {
31        $merged = $this;
32
33        foreach ($ids as $id) {
34            $merged = $merged->contains($id) ? $merged : $merged->withAppendedElement($id);
35        }
36
37        return $merged;
38    }
39
40    /** @param static $ids */
41    public function withoutMembers(self $ids): static
42    {
43        return $this->filter(static fn(RegistryId $id): bool => !$ids->contains($id));
44    }
45
46    /** @return list<string> */
47    public function toStrings(): array
48    {
49        return $this->mapToList(static fn(RegistryId $id): string => $id->value);
50    }
51}