Completed
Push — master ( 68f30c...a95a96 )
by Christian
02:25
created

FileList::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 9
nop 1
1
<?php
2
3
namespace uuf6429\ElderBrother\Change;
4
5
class FileList
6
{
7
    /** @var string */
8
    protected $cacheKey;
9
10
    /** @var callable */
11
    protected $source;
12
13
    /** @var array */
14
    protected static $cache;
15
16
    /**
17
     * @param string   $cacheKey
18
     * @param callable $source
19
     */
20
    public function __construct($cacheKey, callable $source)
21
    {
22
        $this->cacheKey = $cacheKey;
23
        $this->source = $source;
24
    }
25
26
    /**
27
     * Filter by file path matching a regular expression.
28
     *
29
     * @param string $regex
30
     *
31
     * @return static
32
     */
33
    public function name($regex)
34
    {
35
        $source = $this->source;
36
37
        return new self(
38
            $this->cacheKey . '->' . __FUNCTION__ . '(' . $regex . ')',
39
            function () use ($source, $regex) {
40
                return array_filter(
41
                    $source(),
42
                    function ($file) use ($regex) {
43
                        return preg_match($regex, $file);
44
                    }
45
                );
46
            }
47
        );
48
    }
49
50
    /**
51
     * Filter by file path starting with a string.
52
     *
53
     * @param string $string
54
     *
55
     * @return static
56
     */
57
    public function startingWith($string)
58
    {
59
        $source = $this->source;
60
61
        return new self(
62
            $this->cacheKey . '->' . __FUNCTION__ . '(' . $string . ')',
63
            function () use ($source, $string) {
64
                return array_filter(
65
                    $source(),
66
                    function ($file) use ($string) {
67
                        $fileLen = strlen($file);
68
                        $strnLen = strlen($string);
69
70
                        return $strnLen < $fileLen
71
                            && substr_compare($file, $string, 0, $strnLen) === 0;
72
                    }
73
                );
74
            }
75
        );
76
    }
77
78
    /**
79
     * Filter by file path ending with a string.
80
     *
81
     * @param string $string
82
     *
83
     * @return static
84
     */
85
    public function endingWith($string)
86
    {
87
        $source = $this->source;
88
89
        return new self(
90
            $this->cacheKey . '->' . __FUNCTION__ . '(' . $string . ')',
91
            function () use ($source, $string) {
92
                return array_filter(
93
                    $source(),
94
                    function ($file) use ($string) {
95
                        $fileLen = strlen($file);
96
                        $strnLen = strlen($string);
97
98
                        return $fileLen > $strnLen
99
                            && substr_compare($file, $string, -$strnLen) === 0;
100
                    }
101
                );
102
            }
103
        );
104
    }
105
106
    /**
107
     * Returns array of file paths.
108
     *
109
     * @return string[]
110
     */
111
    public function toArray()
112
    {
113
        if (!isset(self::$cache[$this->cacheKey])) {
114
            $source = $this->source;
115
            self::$cache[$this->cacheKey] = $source();
116
        }
117
118
        return self::$cache[$this->cacheKey];
119
    }
120
}
121