Completed
Push — master ( 79c368...f72340 )
by Shcherbak
10:21 queued 06:32
created

FileFilter::processMatch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
crap 12
1
<?php
2
3
  namespace Funivan\Cs\Fs;
4
5
  /**
6
   * @author Ivan Shcherbak <[email protected]> 2016
7
   */
8
  class FileFilter {
9
10
    /**
11
     * @var callable[]
12
     */
13
    private $callback = [];
14
15
16
    /**
17
     * @param string[] $regex
18
     * @return $this
19
     */
20
    public function mimeType($regex) {
21
      $this->callback[] = function (File $file) use ($regex) {
22
        return $this->processMatch($regex, $file->getMimeType());
23
      };
24
      return $this;
25
    }
26
27
28
    /**
29
     * @param string[] $regex
30
     * @return $this
31
     */
32
    public function path(array $regex) {
33
      $this->callback[] = function (File $file) use ($regex) {
34
        return $this->processMatch($regex, $file->getPath());
35
      };
36
37
      return $this;
38
    }
39
40
41
    /**
42
     * @param string[] $regex
43
     * @return $this
44
     */
45
    public function name(array $regex) {
46
      $this->callback[] = function (File $file) use ($regex) {
47
        return $this->processMatch($regex, $file->getName());
48
      };
49
      return $this;
50
    }
51
52
53
    /**
54
     * @param string[] $ext
55
     * @return $this
56
     */
57
    public function extension(array $ext) {
58
      $this->callback[] = function (File $file) use ($ext) {
59
        return in_array($file->getExtension(), $ext);
60
      };
61
      return $this;
62
    }
63
64
65
    /**
66
     * @param int[] $status
67
     * @return $this
68
     */
69
    public function status(array $status) {
70
      $this->callback[] = function (File $file) use ($status) {
71
        return in_array($file->getStatus(), $status);
72
      };
73
    }
74
75
76
    /**
77
     * @return $this
78
     */
79
    public function notDeleted() {
80
      $this->status([
81
        File::STATUS_ADDED,
82
        File::STATUS_COPIED,
83
        File::STATUS_RENAMED,
84
        File::STATUS_MODIFIED,
85
        File::STATUS_UNKNOWN,
86
      ]);
87
      return $this;
88
    }
89
90
91
    /**
92
     * @param File $fileInfo
93
     * @return bool
94
     */
95
    public function isValid(File $fileInfo) {
96
97
      foreach ($this->callback as $callback) {
98
        if ($callback($fileInfo) === false) {
99
          return false;
100
        }
101
      }
102
103
      return true;
104
    }
105
106
107
    /**
108
     * @param string[] $regex
109
     * @param string $value
110
     * @return bool
111
     */
112
    private function processMatch(array $regex, $value) {
113
      foreach ($regex as $reg) {
114
        if (preg_match($reg, $value) === 1) {
115
          return true;
116
        }
117
      }
118
      return false;
119
    }
120
121
  }