Lines 100.00% 5 / 5
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
KeyPatterns 100.00% 5 / 5 100.00% 3 / 3 3 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 matches 100.00% 1 / 1 100.00% 1 / 1 1
 exactEntries 100.00% 3 / 3 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Codegen\Attribute;
6
7use Stewart\Contracts\Exception\SelectorException;
8use Stewart\Contracts\Selector\Collection\SelectorCollection;
9use Stewart\Contracts\Selector\Selector;
10use Stewart\Contracts\Selector\SelectorKind;
11
12final readonly class KeyPatterns
13{
14    private SelectorCollection $selectors;
15
16    /**
17     * @param list<string> $patterns
18     * @throws SelectorException
19     */
20    public function __construct(array $patterns = [])
21    {
22        $this->selectors = SelectorCollection::fromSpecs(...$patterns);
23    }
24
25    public function matches(string $key): bool
26    {
27        return $this->selectors->anyMatches($key);
28    }
29
30    /** @return list<string> */
31    public function exactEntries(): array
32    {
33        return $this->selectors
34            ->filter(static fn(Selector $selector): bool => $selector->getKind() === SelectorKind::Exact)
35            ->mapToList(static fn(Selector $selector): string => $selector->getPattern());
36    }
37}