Lines 100.00% 7 / 7
Functions and Methods 100.00% 5 / 5
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
EntityInclusionRules 100.00% 7 / 7 100.00% 5 / 5 7 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 fromPatterns 100.00% 1 / 1 100.00% 1 / 1 1
 allowingEverything 100.00% 1 / 1 100.00% 1 / 1 1
 allows 100.00% 3 / 3 100.00% 1 / 1 3
 listUnmatchedIncludes 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Codegen\Entity;
6
7use Stewart\Contracts\Entity\EntityId;
8use Stewart\Contracts\Exception\SelectorException;
9use Stewart\Contracts\Selector\Collection\SelectorCollection;
10use Stewart\Contracts\Selector\Selector;
11use Stewart\Contracts\State\Collection\EntityStateCollection;
12
13final readonly class EntityInclusionRules
14{
15    private function __construct(
16        public SelectorCollection $include,
17        public SelectorCollection $exclude,
18    ) {}
19
20    /**
21     * @param list<string> $include
22     * @param list<string> $exclude
23     * @throws SelectorException
24     */
25    public static function fromPatterns(array $include = [], array $exclude = []): self
26    {
27        return new self(SelectorCollection::fromSpecs(...$include), SelectorCollection::fromSpecs(...$exclude));
28    }
29
30    public static function allowingEverything(): self
31    {
32        return new self(SelectorCollection::empty(), SelectorCollection::empty());
33    }
34
35    public function allows(EntityId $entityId): bool
36    {
37        if ($this->exclude->anyMatches($entityId->value)) {
38            return false;
39        }
40
41        return $this->include->isEmpty() || $this->include->anyMatches($entityId->value);
42    }
43
44    public function listUnmatchedIncludes(EntityStateCollection $states): SelectorCollection
45    {
46        return $this->include->filter(static fn(Selector $selector): bool => $states->filterBySelector($selector)->isEmpty());
47    }
48}