Passed
Push — master ( 272916...3e3561 )
by Alexander
01:44
created

PathPattern::withFullPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Files\PathMatcher;
6
7
use Yiisoft\Strings\WildcardPattern;
8
9
/**
10
 * A shell path pattern to match against. Based on {@see WildcardPattern}.
11
 */
12
final class PathPattern implements PathMatcherInterface
13
{
14
    private const FILES = 1;
15
    private const DIRECTORIES = 2;
16
17
    private WildcardPattern $pattern;
18
    private ?int $matchOnly = null;
19
20
    /**
21
     * @param string $pattern The path pattern to match against.
22
     */
23 28
    public function __construct(string $pattern)
24
    {
25 28
        $this->pattern = (new WildcardPattern($pattern))
26 28
            ->withExactSlashes()
27 28
            ->ignoreCase()
28 28
            ->withEnding();
29 28
    }
30
31
    /**
32
     * Make pattern case sensitive.
33
     *
34
     * @return self
35
     */
36 4
    public function caseSensitive(): self
37
    {
38 4
        $new = clone $this;
39 4
        $new->pattern = $this->pattern->ignoreCase(false);
40 4
        return $new;
41
    }
42
43
    /**
44
     * Match full path, not just ending of path.
45
     *
46
     * @return self
47
     */
48 5
    public function withFullPath(): self
49
    {
50 5
        $new = clone $this;
51 5
        $new->pattern = $this->pattern->withEnding(false);
52 5
        return $new;
53
    }
54
55
    /**
56
     * Match `/` character with wildcards.
57
     *
58
     * @return self
59
     */
60 4
    public function withNotExactSlashes(): self
61
    {
62 4
        $new = clone $this;
63 4
        $new->pattern = $this->pattern->withExactSlashes(false);
64 4
        return $new;
65
    }
66
67
    /**
68
     * If path is not file or file not exists skip matching.
69
     *
70
     * @return self
71
     */
72 5
    public function onlyFiles(): self
73
    {
74 5
        $new = clone $this;
75 5
        $new->matchOnly = self::FILES;
76 5
        return $new;
77
    }
78
79
    /**
80
     * Skip matching if path is not directory or directory does no exist.
81
     *
82
     * @return self
83
     */
84 5
    public function onlyDirectories(): self
85
    {
86 5
        $new = clone $this;
87 5
        $new->matchOnly = self::DIRECTORIES;
88 5
        return $new;
89
    }
90
91
    /**
92
     * Checks if the passed path would match the given shell path pattern.
93
     * If need match only files and path is directory or conversely then matching skipped and returned `null`.
94
     *
95
     * @param string $path The tested path.
96
     *
97
     * @return bool|null Whether the path matches pattern or not, `null` if matching skipped.
98
     */
99 26
    public function match(string $path): ?bool
100
    {
101 26
        $path = str_replace('\\', '/', $path);
102
103
        if (
104 26
            ($this->matchOnly === self::FILES && is_dir($path)) ||
105 26
            ($this->matchOnly === self::DIRECTORIES && is_file($path))
106
        ) {
107 4
            return null;
108
        }
109
110 26
        return $this->pattern->match($path);
111
    }
112
}
113