FilterTrait::directoriesOnly()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/******************************************************************************
3
 * An iterator interface over the Leagues flysystem.
4
 * Copyright (c) 2021, 2015 Richard Klees <[email protected]>
5
 *
6
 * This software is licensed under GPLv3. You should have received
7
 * a copy of the along with the code.
8
 */
9
10
namespace Lechimp\Flightcontrol;
11
12
/**
13
 * Capture similarities for objects that could be filtered for name.
14
 */
15
trait FilterTrait
16
{
17
    /**
18
     * Get a recursor/iterator filters on the name of the objects it works on.
19
     *
20
     * The regexp will be embedded as such "%^$regexp$%" before it is passed
21
     * to preg_match.
22
     */
23 6
    public function named(string $regexp)
24
    {
25 6
        $regexp = "%^$regexp$%";
26 6
        return $this->filter(function (FSObject $obj) use ($regexp) {
27 6
            return preg_match($regexp, $obj->name());
28 6
        });
29
    }
30
31
    /**
32
     * Get an iterator for every directory in the current iterator.
33
     */
34 2
    public function directoriesOnly() : Iterator
35
    {
36 2
        return $this->filter(function (FSObject $obj) {
37 2
            return !$obj->isFile();
38 2
        });
39
    }
40
41
    /**
42
     * Get an iterator for every file in the current iterator.
43
     */
44 2
    public function filesOnly() : Iterator
45
    {
46 2
        return $this->filter(function (FSObject $obj) {
47 2
            return $obj->isFile();
48 2
        });
49
    }
50
51
    /**
52
     * This should actually return a filtered version of the object.
53
     *
54
     * @param   \Closure    $predicate
55
     * @return  mixed
56
     */
57
    abstract public function filter(\Closure $predicate);
58
}
59