1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Funivan\Cs\Filters; |
4
|
|
|
|
5
|
|
|
use Funivan\Cs\FileFinder\File; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Ivan Shcherbak <[email protected]> 2016 |
9
|
|
|
*/ |
10
|
|
|
class FileFilter { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var callable[] |
14
|
|
|
*/ |
15
|
|
|
private $callback = []; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $type |
20
|
|
|
* @return $this |
21
|
|
|
*/ |
22
|
|
|
public function mimeType($type) { |
23
|
|
|
$this->callback[] = function (File $file) use ($type) { |
24
|
|
|
return (strpos($file->getMimeType(), $type) === 0); |
25
|
|
|
}; |
26
|
|
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string|array $regex |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
|
|
public function path($regex) { |
35
|
|
|
$this->callback[] = function (File $file) use ($regex) { |
36
|
|
|
$regex = (array) $regex; |
|
|
|
|
37
|
|
|
$path = $file->getPath(); |
38
|
|
|
|
39
|
|
|
foreach ($regex as $reg) { |
40
|
|
|
if (preg_match($reg, $path)) { |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
return false; |
45
|
|
|
}; |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string|array $regex |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function name($regex) { |
56
|
|
|
$this->callback[] = function (File $file) use ($regex) { |
57
|
|
|
$regex = (array) $regex; |
|
|
|
|
58
|
|
|
$path = $file->getName(); |
59
|
|
|
|
60
|
|
|
foreach ($regex as $reg) { |
61
|
|
|
if (preg_match($reg, $path)) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
return false; |
66
|
|
|
}; |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array|string $ext |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function extension($ext) { |
76
|
|
|
$this->callback[] = function (File $file) use ($ext) { |
77
|
|
|
return (in_array($file->getExtension(), (array) $ext)); |
78
|
|
|
}; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array|string $status |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function status($status) { |
88
|
|
|
$this->callback[] = function (File $file) use ($status) { |
89
|
|
|
$status = (array) $status; |
|
|
|
|
90
|
|
|
return in_array($file->getStatus(), $status); |
91
|
|
|
}; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function notDeleted() { |
99
|
|
|
$this->status([ |
100
|
|
|
File::STATUS_ADDED, |
101
|
|
|
File::STATUS_COPIED, |
102
|
|
|
File::STATUS_RENAMED, |
103
|
|
|
File::STATUS_MODIFIED, |
104
|
|
|
File::STATUS_UNKNOWN, |
105
|
|
|
]); |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param File $fileInfo |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function isValid(File $fileInfo) { |
115
|
|
|
|
116
|
|
|
foreach ($this->callback as $callback) { |
117
|
|
|
if ($callback($fileInfo) === false) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope