Passed
Pull Request — master (#22)
by Sergei
12:39
created

PathMatcher::except()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Files;
6
7
use Yiisoft\Strings\WildcardPattern;
8
9
final class PathMatcher
10
{
11
    private ?array $only = null;
12
    private ?array $except = null;
13
    private ?array $callbacks = null;
14
15
    private bool $caseSensitive = false;
16
17
    public function caseSensitive(): self
18
    {
19
        $new = clone $this;
20
        $new->caseSensitive = true;
21
        return $new;
22
    }
23
24
    /**
25
     * @param string|WildcardPattern ...$patterns
26
     * @return self
27
     */
28
    public function only(...$patterns): self
29
    {
30
        $new = clone $this;
31
        $new->only = $patterns;
32
        return $new;
33
    }
34
35
    /**
36
     * @param string|WildcardPattern ...$patterns
37
     * @return self
38
     */
39
    public function except(string ...$patterns): self
40
    {
41
        $new = clone $this;
42
        $new->except = $patterns;
43
        return $new;
44
    }
45
46
    public function callback(callable ...$callbacks): self
47
    {
48
        $new = clone $this;
49
        $new->callbacks = $callbacks;
50
        return $new;
51
    }
52
53
    public function match(string $path): bool
54
    {
55
        $path = str_replace('\\', '/', $path);
56
57
        if ($this->only !== null) {
58
            if (!$this->matchWildcardPatterns($path, $this->only)) {
59
                return false;
60
            }
61
        }
62
63
        if ($this->except !== null) {
64
            if ($this->matchWildcardPatterns($path, $this->except)) {
65
                return false;
66
            }
67
        }
68
69
        if ($this->callbacks !== null) {
70
            foreach ($this->callbacks as $callback) {
71
                if (!$callback($path)) {
72
                    return false;
73
                }
74
            }
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * @param string $path
82
     * @param string|WildcardPattern[] $patterns
83
     * @return bool
84
     */
85
    private function matchWildcardPatterns(string $path, array $patterns): bool
86
    {
87
        $patterns = $this->makeWildcardPatterns($patterns);
88
89
        foreach ($patterns as $pattern) {
90
            if ($pattern->match($path)) {
91
                return true;
92
            }
93
        }
94
95
        return false;
96
    }
97
98
    /**
99
     * @param string[]|WildcardPattern[] $patterns
100
     * @return WildcardPattern[]
101
     */
102
    private function makeWildcardPatterns(array $patterns): array
103
    {
104
        $wildcardPatterns = [];
105
        foreach ($patterns as $pattern) {
106
            if ($pattern instanceof WildcardPattern) {
107
                $wildcardPatterns[] = $pattern;
108
                continue;
109
            }
110
111
            $wildcardPattern = new WildcardPattern('*' . $pattern);
112
113
            if (!$this->caseSensitive) {
114
                $wildcardPattern = $wildcardPattern->ignoreCase();
115
            }
116
117
            $wildcardPatterns[] = $wildcardPattern;
118
        }
119
        return $wildcardPatterns;
120
    }
121
}
122