Passed
Pull Request — master (#22)
by Sergei
13:33
created

PathPattern::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Files\PathMatch;
6
7
use Yiisoft\Strings\WildcardPattern;
8
9
/**
10
 * A shell path pattern to match against.
11
 * Based on {@see WildcardPattern}.
12
 */
13
final class PathPattern implements PathMatcherInterface
14
{
15
    private WildcardPattern $pattern;
16
17
    /**
18
     * @param string $pattern The path pattern to match against.
19
     */
20
    public function __construct(string $pattern)
21
    {
22
        $this->pattern = (new WildcardPattern($pattern))
23
            ->withExactSlashes()
24
            ->ignoreCase()
25
            ->withEnding();
26
    }
27
28
    /**
29
     * Make pattern case sensitive.
30
     * @return self
31
     */
32
    public function caseSensitive(): self
33
    {
34
        $new = clone $this;
35
        $new->pattern = $this->pattern->ignoreCase(false);
36
        return $new;
37
    }
38
39
    /**
40
     * Match full path, not just ending of path.
41
     * @return self
42
     */
43
    public function withFullPath(): self
44
    {
45
        $new = clone $this;
46
        $new->pattern = $this->pattern->withEnding(false);
47
        return $new;
48
    }
49
50
    /**
51
     * Match `/` character with wildcards.
52
     * @return self
53
     */
54
    public function withNotExactSlashes(): self
55
    {
56
        $new = clone $this;
57
        $new->pattern = $this->pattern->withExactSlashes(false);
58
        return $new;
59
    }
60
61
    /**
62
     * Checks if the passed path would match the given shell path pattern.
63
     *
64
     * @param string $path The tested path.
65
     * @return bool Whether the path matches pattern or not.
66
     */
67
    public function match(string $path): bool
68
    {
69
        return $this->pattern->match($path);
70
    }
71
}
72