Passed
Push — master ( 50ad36...d3d965 )
by Alexander
02:14
created

CompositeMatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Files\PathMatcher;
6
7
/**
8
 * Composite matcher allows combining several matchers.
9
 */
10
final class CompositeMatcher implements PathMatcherInterface
11
{
12
    private bool $matchAny;
13
14
    /**
15
     * @var PathMatcherInterface[]
16
     */
17
    private array $matchers;
18
19 14
    private function __construct(bool $matchAny, PathMatcherInterface ...$matchers)
20
    {
21 14
        $this->matchers = $matchers;
22 14
        $this->matchAny = $matchAny;
23 14
    }
24
25
    /**
26
     * Get an instance of composite matcher that gives a match if any of sub-matchers match.
27
     *
28
     * @param PathMatcherInterface ...$matchers Matchers to check.
29
     *
30
     * @return static
31
     */
32 7
    public static function any(PathMatcherInterface ...$matchers): self
33
    {
34 7
        return new self(true, ...$matchers);
35
    }
36
37
    /**
38
     * Get an instance of composite matcher that gives a match only if all of sub-matchers match.
39
     *
40
     * @param PathMatcherInterface ...$matchers Matchers to check.
41
     *
42
     * @return static
43
     */
44 7
    public static function all(PathMatcherInterface ...$matchers): self
45
    {
46 7
        return new self(false, ...$matchers);
47
    }
48
49 14
    public function match(string $path): ?bool
50
    {
51 14
        $allNulls = true;
52
53 14
        foreach ($this->matchers as $matcher) {
54 14
            $match = $matcher->match($path);
55
56 14
            if ($match === null) {
57 6
                continue;
58
            }
59
60 12
            $allNulls = false;
61
62 12
            if ($this->matchAny && $match) {
63 4
                return true;
64
            }
65
66 9
            if (!$this->matchAny && !$match) {
67 4
                return false;
68
            }
69
        }
70
71 6
        return $allNulls ? null : !$this->matchAny;
72
    }
73
}
74