@@ -18,34 +18,34 @@ |
||
18 | 18 | */ |
19 | 19 | class DateComparator extends Comparator |
20 | 20 | { |
21 | - /** |
|
22 | - * @param string $test A comparison string |
|
23 | - * |
|
24 | - * @throws \InvalidArgumentException If the test is not understood |
|
25 | - */ |
|
26 | - public function __construct(string $test) |
|
27 | - { |
|
28 | - if (!preg_match('#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) { |
|
29 | - throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test)); |
|
30 | - } |
|
21 | + /** |
|
22 | + * @param string $test A comparison string |
|
23 | + * |
|
24 | + * @throws \InvalidArgumentException If the test is not understood |
|
25 | + */ |
|
26 | + public function __construct(string $test) |
|
27 | + { |
|
28 | + if (!preg_match('#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) { |
|
29 | + throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test)); |
|
30 | + } |
|
31 | 31 | |
32 | - try { |
|
33 | - $date = new \DateTime($matches[2]); |
|
34 | - $target = $date->format('U'); |
|
35 | - } catch (\Exception $e) { |
|
36 | - throw new \InvalidArgumentException(sprintf('"%s" is not a valid date.', $matches[2])); |
|
37 | - } |
|
32 | + try { |
|
33 | + $date = new \DateTime($matches[2]); |
|
34 | + $target = $date->format('U'); |
|
35 | + } catch (\Exception $e) { |
|
36 | + throw new \InvalidArgumentException(sprintf('"%s" is not a valid date.', $matches[2])); |
|
37 | + } |
|
38 | 38 | |
39 | - $operator = $matches[1] ?? '=='; |
|
40 | - if ('since' === $operator || 'after' === $operator) { |
|
41 | - $operator = '>'; |
|
42 | - } |
|
39 | + $operator = $matches[1] ?? '=='; |
|
40 | + if ('since' === $operator || 'after' === $operator) { |
|
41 | + $operator = '>'; |
|
42 | + } |
|
43 | 43 | |
44 | - if ('until' === $operator || 'before' === $operator) { |
|
45 | - $operator = '<'; |
|
46 | - } |
|
44 | + if ('until' === $operator || 'before' === $operator) { |
|
45 | + $operator = '<'; |
|
46 | + } |
|
47 | 47 | |
48 | - $this->setOperator($operator); |
|
49 | - $this->setTarget($target); |
|
50 | - } |
|
48 | + $this->setOperator($operator); |
|
49 | + $this->setTarget($target); |
|
50 | + } |
|
51 | 51 | } |
@@ -34,46 +34,46 @@ |
||
34 | 34 | */ |
35 | 35 | class NumberComparator extends Comparator |
36 | 36 | { |
37 | - /** |
|
38 | - * @param string|int $test A comparison string or an integer |
|
39 | - * |
|
40 | - * @throws \InvalidArgumentException If the test is not understood |
|
41 | - */ |
|
42 | - public function __construct(?string $test) |
|
43 | - { |
|
44 | - if (null === $test || !preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) { |
|
45 | - throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test ?? 'null')); |
|
46 | - } |
|
37 | + /** |
|
38 | + * @param string|int $test A comparison string or an integer |
|
39 | + * |
|
40 | + * @throws \InvalidArgumentException If the test is not understood |
|
41 | + */ |
|
42 | + public function __construct(?string $test) |
|
43 | + { |
|
44 | + if (null === $test || !preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) { |
|
45 | + throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test ?? 'null')); |
|
46 | + } |
|
47 | 47 | |
48 | - $target = $matches[2]; |
|
49 | - if (!is_numeric($target)) { |
|
50 | - throw new \InvalidArgumentException(sprintf('Invalid number "%s".', $target)); |
|
51 | - } |
|
52 | - if (isset($matches[3])) { |
|
53 | - // magnitude |
|
54 | - switch (strtolower($matches[3])) { |
|
55 | - case 'k': |
|
56 | - $target *= 1000; |
|
57 | - break; |
|
58 | - case 'ki': |
|
59 | - $target *= 1024; |
|
60 | - break; |
|
61 | - case 'm': |
|
62 | - $target *= 1000000; |
|
63 | - break; |
|
64 | - case 'mi': |
|
65 | - $target *= 1024 * 1024; |
|
66 | - break; |
|
67 | - case 'g': |
|
68 | - $target *= 1000000000; |
|
69 | - break; |
|
70 | - case 'gi': |
|
71 | - $target *= 1024 * 1024 * 1024; |
|
72 | - break; |
|
73 | - } |
|
74 | - } |
|
48 | + $target = $matches[2]; |
|
49 | + if (!is_numeric($target)) { |
|
50 | + throw new \InvalidArgumentException(sprintf('Invalid number "%s".', $target)); |
|
51 | + } |
|
52 | + if (isset($matches[3])) { |
|
53 | + // magnitude |
|
54 | + switch (strtolower($matches[3])) { |
|
55 | + case 'k': |
|
56 | + $target *= 1000; |
|
57 | + break; |
|
58 | + case 'ki': |
|
59 | + $target *= 1024; |
|
60 | + break; |
|
61 | + case 'm': |
|
62 | + $target *= 1000000; |
|
63 | + break; |
|
64 | + case 'mi': |
|
65 | + $target *= 1024 * 1024; |
|
66 | + break; |
|
67 | + case 'g': |
|
68 | + $target *= 1000000000; |
|
69 | + break; |
|
70 | + case 'gi': |
|
71 | + $target *= 1024 * 1024 * 1024; |
|
72 | + break; |
|
73 | + } |
|
74 | + } |
|
75 | 75 | |
76 | - $this->setTarget($target); |
|
77 | - $this->setOperator($matches[1] ?? '=='); |
|
78 | - } |
|
76 | + $this->setTarget($target); |
|
77 | + $this->setOperator($matches[1] ?? '=='); |
|
78 | + } |
|
79 | 79 | } |
@@ -18,74 +18,74 @@ |
||
18 | 18 | */ |
19 | 19 | class Comparator |
20 | 20 | { |
21 | - private $target; |
|
22 | - private $operator = '=='; |
|
21 | + private $target; |
|
22 | + private $operator = '=='; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Gets the target value. |
|
26 | - * |
|
27 | - * @return string The target value |
|
28 | - */ |
|
29 | - public function getTarget() |
|
30 | - { |
|
31 | - return $this->target; |
|
32 | - } |
|
24 | + /** |
|
25 | + * Gets the target value. |
|
26 | + * |
|
27 | + * @return string The target value |
|
28 | + */ |
|
29 | + public function getTarget() |
|
30 | + { |
|
31 | + return $this->target; |
|
32 | + } |
|
33 | 33 | |
34 | - public function setTarget(string $target) |
|
35 | - { |
|
36 | - $this->target = $target; |
|
37 | - } |
|
34 | + public function setTarget(string $target) |
|
35 | + { |
|
36 | + $this->target = $target; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Gets the comparison operator. |
|
41 | - * |
|
42 | - * @return string The operator |
|
43 | - */ |
|
44 | - public function getOperator() |
|
45 | - { |
|
46 | - return $this->operator; |
|
47 | - } |
|
39 | + /** |
|
40 | + * Gets the comparison operator. |
|
41 | + * |
|
42 | + * @return string The operator |
|
43 | + */ |
|
44 | + public function getOperator() |
|
45 | + { |
|
46 | + return $this->operator; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Sets the comparison operator. |
|
51 | - * |
|
52 | - * @throws \InvalidArgumentException |
|
53 | - */ |
|
54 | - public function setOperator(string $operator) |
|
55 | - { |
|
56 | - if ('' === $operator) { |
|
57 | - $operator = '=='; |
|
58 | - } |
|
49 | + /** |
|
50 | + * Sets the comparison operator. |
|
51 | + * |
|
52 | + * @throws \InvalidArgumentException |
|
53 | + */ |
|
54 | + public function setOperator(string $operator) |
|
55 | + { |
|
56 | + if ('' === $operator) { |
|
57 | + $operator = '=='; |
|
58 | + } |
|
59 | 59 | |
60 | - if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) { |
|
61 | - throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator)); |
|
62 | - } |
|
60 | + if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) { |
|
61 | + throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator)); |
|
62 | + } |
|
63 | 63 | |
64 | - $this->operator = $operator; |
|
65 | - } |
|
64 | + $this->operator = $operator; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Tests against the target. |
|
69 | - * |
|
70 | - * @param mixed $test A test value |
|
71 | - * |
|
72 | - * @return bool |
|
73 | - */ |
|
74 | - public function test($test) |
|
75 | - { |
|
76 | - switch ($this->operator) { |
|
77 | - case '>': |
|
78 | - return $test > $this->target; |
|
79 | - case '>=': |
|
80 | - return $test >= $this->target; |
|
81 | - case '<': |
|
82 | - return $test < $this->target; |
|
83 | - case '<=': |
|
84 | - return $test <= $this->target; |
|
85 | - case '!=': |
|
86 | - return $test != $this->target; |
|
87 | - } |
|
67 | + /** |
|
68 | + * Tests against the target. |
|
69 | + * |
|
70 | + * @param mixed $test A test value |
|
71 | + * |
|
72 | + * @return bool |
|
73 | + */ |
|
74 | + public function test($test) |
|
75 | + { |
|
76 | + switch ($this->operator) { |
|
77 | + case '>': |
|
78 | + return $test > $this->target; |
|
79 | + case '>=': |
|
80 | + return $test >= $this->target; |
|
81 | + case '<': |
|
82 | + return $test < $this->target; |
|
83 | + case '<=': |
|
84 | + return $test <= $this->target; |
|
85 | + case '!=': |
|
86 | + return $test != $this->target; |
|
87 | + } |
|
88 | 88 | |
89 | - return $test == $this->target; |
|
90 | - } |
|
89 | + return $test == $this->target; |
|
90 | + } |
|
91 | 91 | } |
@@ -18,68 +18,68 @@ |
||
18 | 18 | */ |
19 | 19 | class SplFileInfo extends \SplFileInfo |
20 | 20 | { |
21 | - private $relativePath; |
|
22 | - private $relativePathname; |
|
21 | + private $relativePath; |
|
22 | + private $relativePathname; |
|
23 | 23 | |
24 | - /** |
|
25 | - * @param string $file The file name |
|
26 | - * @param string $relativePath The relative path |
|
27 | - * @param string $relativePathname The relative path name |
|
28 | - */ |
|
29 | - public function __construct(string $file, string $relativePath, string $relativePathname) |
|
30 | - { |
|
31 | - parent::__construct($file); |
|
32 | - $this->relativePath = $relativePath; |
|
33 | - $this->relativePathname = $relativePathname; |
|
34 | - } |
|
24 | + /** |
|
25 | + * @param string $file The file name |
|
26 | + * @param string $relativePath The relative path |
|
27 | + * @param string $relativePathname The relative path name |
|
28 | + */ |
|
29 | + public function __construct(string $file, string $relativePath, string $relativePathname) |
|
30 | + { |
|
31 | + parent::__construct($file); |
|
32 | + $this->relativePath = $relativePath; |
|
33 | + $this->relativePathname = $relativePathname; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Returns the relative path. |
|
38 | - * |
|
39 | - * This path does not contain the file name. |
|
40 | - * |
|
41 | - * @return string the relative path |
|
42 | - */ |
|
43 | - public function getRelativePath() |
|
44 | - { |
|
45 | - return $this->relativePath; |
|
46 | - } |
|
36 | + /** |
|
37 | + * Returns the relative path. |
|
38 | + * |
|
39 | + * This path does not contain the file name. |
|
40 | + * |
|
41 | + * @return string the relative path |
|
42 | + */ |
|
43 | + public function getRelativePath() |
|
44 | + { |
|
45 | + return $this->relativePath; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Returns the relative path name. |
|
50 | - * |
|
51 | - * This path contains the file name. |
|
52 | - * |
|
53 | - * @return string the relative path name |
|
54 | - */ |
|
55 | - public function getRelativePathname() |
|
56 | - { |
|
57 | - return $this->relativePathname; |
|
58 | - } |
|
48 | + /** |
|
49 | + * Returns the relative path name. |
|
50 | + * |
|
51 | + * This path contains the file name. |
|
52 | + * |
|
53 | + * @return string the relative path name |
|
54 | + */ |
|
55 | + public function getRelativePathname() |
|
56 | + { |
|
57 | + return $this->relativePathname; |
|
58 | + } |
|
59 | 59 | |
60 | - public function getFilenameWithoutExtension(): string |
|
61 | - { |
|
62 | - $filename = $this->getFilename(); |
|
60 | + public function getFilenameWithoutExtension(): string |
|
61 | + { |
|
62 | + $filename = $this->getFilename(); |
|
63 | 63 | |
64 | - return pathinfo($filename, \PATHINFO_FILENAME); |
|
65 | - } |
|
64 | + return pathinfo($filename, \PATHINFO_FILENAME); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Returns the contents of the file. |
|
69 | - * |
|
70 | - * @return string the contents of the file |
|
71 | - * |
|
72 | - * @throws \RuntimeException |
|
73 | - */ |
|
74 | - public function getContents() |
|
75 | - { |
|
76 | - set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; }); |
|
77 | - $content = file_get_contents($this->getPathname()); |
|
78 | - restore_error_handler(); |
|
79 | - if (false === $content) { |
|
80 | - throw new \RuntimeException($error); |
|
81 | - } |
|
67 | + /** |
|
68 | + * Returns the contents of the file. |
|
69 | + * |
|
70 | + * @return string the contents of the file |
|
71 | + * |
|
72 | + * @throws \RuntimeException |
|
73 | + */ |
|
74 | + public function getContents() |
|
75 | + { |
|
76 | + set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; }); |
|
77 | + $content = file_get_contents($this->getPathname()); |
|
78 | + restore_error_handler(); |
|
79 | + if (false === $content) { |
|
80 | + throw new \RuntimeException($error); |
|
81 | + } |
|
82 | 82 | |
83 | - return $content; |
|
84 | - } |
|
83 | + return $content; |
|
84 | + } |
|
85 | 85 | } |
@@ -18,76 +18,76 @@ |
||
18 | 18 | */ |
19 | 19 | class ExcludeDirectoryFilterIterator extends \FilterIterator implements \RecursiveIterator |
20 | 20 | { |
21 | - private $iterator; |
|
22 | - private $isRecursive; |
|
23 | - private $excludedDirs = []; |
|
24 | - private $excludedPattern; |
|
21 | + private $iterator; |
|
22 | + private $isRecursive; |
|
23 | + private $excludedDirs = []; |
|
24 | + private $excludedPattern; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @param \Iterator $iterator The Iterator to filter |
|
28 | - * @param string[] $directories An array of directories to exclude |
|
29 | - */ |
|
30 | - public function __construct(\Iterator $iterator, array $directories) |
|
31 | - { |
|
32 | - $this->iterator = $iterator; |
|
33 | - $this->isRecursive = $iterator instanceof \RecursiveIterator; |
|
34 | - $patterns = []; |
|
35 | - foreach ($directories as $directory) { |
|
36 | - $directory = rtrim($directory, '/'); |
|
37 | - if (!$this->isRecursive || str_contains($directory, '/')) { |
|
38 | - $patterns[] = preg_quote($directory, '#'); |
|
39 | - } else { |
|
40 | - $this->excludedDirs[$directory] = true; |
|
41 | - } |
|
42 | - } |
|
43 | - if ($patterns) { |
|
44 | - $this->excludedPattern = '#(?:^|/)(?:'.implode('|', $patterns).')(?:/|$)#'; |
|
45 | - } |
|
26 | + /** |
|
27 | + * @param \Iterator $iterator The Iterator to filter |
|
28 | + * @param string[] $directories An array of directories to exclude |
|
29 | + */ |
|
30 | + public function __construct(\Iterator $iterator, array $directories) |
|
31 | + { |
|
32 | + $this->iterator = $iterator; |
|
33 | + $this->isRecursive = $iterator instanceof \RecursiveIterator; |
|
34 | + $patterns = []; |
|
35 | + foreach ($directories as $directory) { |
|
36 | + $directory = rtrim($directory, '/'); |
|
37 | + if (!$this->isRecursive || str_contains($directory, '/')) { |
|
38 | + $patterns[] = preg_quote($directory, '#'); |
|
39 | + } else { |
|
40 | + $this->excludedDirs[$directory] = true; |
|
41 | + } |
|
42 | + } |
|
43 | + if ($patterns) { |
|
44 | + $this->excludedPattern = '#(?:^|/)(?:'.implode('|', $patterns).')(?:/|$)#'; |
|
45 | + } |
|
46 | 46 | |
47 | - parent::__construct($iterator); |
|
48 | - } |
|
47 | + parent::__construct($iterator); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Filters the iterator values. |
|
52 | - * |
|
53 | - * @return bool True if the value should be kept, false otherwise |
|
54 | - */ |
|
55 | - #[\ReturnTypeWillChange] |
|
56 | - public function accept() |
|
57 | - { |
|
58 | - if ($this->isRecursive && isset($this->excludedDirs[$this->getFilename()]) && $this->isDir()) { |
|
59 | - return false; |
|
60 | - } |
|
50 | + /** |
|
51 | + * Filters the iterator values. |
|
52 | + * |
|
53 | + * @return bool True if the value should be kept, false otherwise |
|
54 | + */ |
|
55 | + #[\ReturnTypeWillChange] |
|
56 | + public function accept() |
|
57 | + { |
|
58 | + if ($this->isRecursive && isset($this->excludedDirs[$this->getFilename()]) && $this->isDir()) { |
|
59 | + return false; |
|
60 | + } |
|
61 | 61 | |
62 | - if ($this->excludedPattern) { |
|
63 | - $path = $this->isDir() ? $this->current()->getRelativePathname() : $this->current()->getRelativePath(); |
|
64 | - $path = str_replace('\\', '/', $path); |
|
62 | + if ($this->excludedPattern) { |
|
63 | + $path = $this->isDir() ? $this->current()->getRelativePathname() : $this->current()->getRelativePath(); |
|
64 | + $path = str_replace('\\', '/', $path); |
|
65 | 65 | |
66 | - return !preg_match($this->excludedPattern, $path); |
|
67 | - } |
|
66 | + return !preg_match($this->excludedPattern, $path); |
|
67 | + } |
|
68 | 68 | |
69 | - return true; |
|
70 | - } |
|
69 | + return true; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @return bool |
|
74 | - */ |
|
75 | - #[\ReturnTypeWillChange] |
|
76 | - public function hasChildren() |
|
77 | - { |
|
78 | - return $this->isRecursive && $this->iterator->hasChildren(); |
|
79 | - } |
|
72 | + /** |
|
73 | + * @return bool |
|
74 | + */ |
|
75 | + #[\ReturnTypeWillChange] |
|
76 | + public function hasChildren() |
|
77 | + { |
|
78 | + return $this->isRecursive && $this->iterator->hasChildren(); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @return self |
|
83 | - */ |
|
84 | - #[\ReturnTypeWillChange] |
|
85 | - public function getChildren() |
|
86 | - { |
|
87 | - $children = new self($this->iterator->getChildren(), []); |
|
88 | - $children->excludedDirs = $this->excludedDirs; |
|
89 | - $children->excludedPattern = $this->excludedPattern; |
|
81 | + /** |
|
82 | + * @return self |
|
83 | + */ |
|
84 | + #[\ReturnTypeWillChange] |
|
85 | + public function getChildren() |
|
86 | + { |
|
87 | + $children = new self($this->iterator->getChildren(), []); |
|
88 | + $children->excludedDirs = $this->excludedDirs; |
|
89 | + $children->excludedPattern = $this->excludedPattern; |
|
90 | 90 | |
91 | - return $children; |
|
92 | - } |
|
91 | + return $children; |
|
92 | + } |
|
93 | 93 | } |
@@ -19,41 +19,41 @@ |
||
19 | 19 | */ |
20 | 20 | class FilecontentFilterIterator extends MultiplePcreFilterIterator |
21 | 21 | { |
22 | - /** |
|
23 | - * Filters the iterator values. |
|
24 | - * |
|
25 | - * @return bool true if the value should be kept, false otherwise |
|
26 | - */ |
|
27 | - #[\ReturnTypeWillChange] |
|
28 | - public function accept() |
|
29 | - { |
|
30 | - if (!$this->matchRegexps && !$this->noMatchRegexps) { |
|
31 | - return true; |
|
32 | - } |
|
33 | - |
|
34 | - $fileinfo = $this->current(); |
|
35 | - |
|
36 | - if ($fileinfo->isDir() || !$fileinfo->isReadable()) { |
|
37 | - return false; |
|
38 | - } |
|
39 | - |
|
40 | - $content = $fileinfo->getContents(); |
|
41 | - if (!$content) { |
|
42 | - return false; |
|
43 | - } |
|
44 | - |
|
45 | - return $this->isAccepted($content); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Converts string to regexp if necessary. |
|
50 | - * |
|
51 | - * @param string $str Pattern: string or regexp |
|
52 | - * |
|
53 | - * @return string regexp corresponding to a given string or regexp |
|
54 | - */ |
|
55 | - protected function toRegex(string $str) |
|
56 | - { |
|
57 | - return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/'; |
|
58 | - } |
|
22 | + /** |
|
23 | + * Filters the iterator values. |
|
24 | + * |
|
25 | + * @return bool true if the value should be kept, false otherwise |
|
26 | + */ |
|
27 | + #[\ReturnTypeWillChange] |
|
28 | + public function accept() |
|
29 | + { |
|
30 | + if (!$this->matchRegexps && !$this->noMatchRegexps) { |
|
31 | + return true; |
|
32 | + } |
|
33 | + |
|
34 | + $fileinfo = $this->current(); |
|
35 | + |
|
36 | + if ($fileinfo->isDir() || !$fileinfo->isReadable()) { |
|
37 | + return false; |
|
38 | + } |
|
39 | + |
|
40 | + $content = $fileinfo->getContents(); |
|
41 | + if (!$content) { |
|
42 | + return false; |
|
43 | + } |
|
44 | + |
|
45 | + return $this->isAccepted($content); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Converts string to regexp if necessary. |
|
50 | + * |
|
51 | + * @param string $str Pattern: string or regexp |
|
52 | + * |
|
53 | + * @return string regexp corresponding to a given string or regexp |
|
54 | + */ |
|
55 | + protected function toRegex(string $str) |
|
56 | + { |
|
57 | + return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/'; |
|
58 | + } |
|
59 | 59 | } |
@@ -20,29 +20,29 @@ |
||
20 | 20 | */ |
21 | 21 | class FilenameFilterIterator extends MultiplePcreFilterIterator |
22 | 22 | { |
23 | - /** |
|
24 | - * Filters the iterator values. |
|
25 | - * |
|
26 | - * @return bool true if the value should be kept, false otherwise |
|
27 | - */ |
|
28 | - #[\ReturnTypeWillChange] |
|
29 | - public function accept() |
|
30 | - { |
|
31 | - return $this->isAccepted($this->current()->getFilename()); |
|
32 | - } |
|
23 | + /** |
|
24 | + * Filters the iterator values. |
|
25 | + * |
|
26 | + * @return bool true if the value should be kept, false otherwise |
|
27 | + */ |
|
28 | + #[\ReturnTypeWillChange] |
|
29 | + public function accept() |
|
30 | + { |
|
31 | + return $this->isAccepted($this->current()->getFilename()); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Converts glob to regexp. |
|
36 | - * |
|
37 | - * PCRE patterns are left unchanged. |
|
38 | - * Glob strings are transformed with Glob::toRegex(). |
|
39 | - * |
|
40 | - * @param string $str Pattern: glob or regexp |
|
41 | - * |
|
42 | - * @return string regexp corresponding to a given glob or regexp |
|
43 | - */ |
|
44 | - protected function toRegex(string $str) |
|
45 | - { |
|
46 | - return $this->isRegex($str) ? $str : Glob::toRegex($str); |
|
47 | - } |
|
34 | + /** |
|
35 | + * Converts glob to regexp. |
|
36 | + * |
|
37 | + * PCRE patterns are left unchanged. |
|
38 | + * Glob strings are transformed with Glob::toRegex(). |
|
39 | + * |
|
40 | + * @param string $str Pattern: glob or regexp |
|
41 | + * |
|
42 | + * @return string regexp corresponding to a given glob or regexp |
|
43 | + */ |
|
44 | + protected function toRegex(string $str) |
|
45 | + { |
|
46 | + return $this->isRegex($str) ? $str : Glob::toRegex($str); |
|
47 | + } |
|
48 | 48 | } |
@@ -18,84 +18,84 @@ |
||
18 | 18 | */ |
19 | 19 | class SortableIterator implements \IteratorAggregate |
20 | 20 | { |
21 | - public const SORT_BY_NONE = 0; |
|
22 | - public const SORT_BY_NAME = 1; |
|
23 | - public const SORT_BY_TYPE = 2; |
|
24 | - public const SORT_BY_ACCESSED_TIME = 3; |
|
25 | - public const SORT_BY_CHANGED_TIME = 4; |
|
26 | - public const SORT_BY_MODIFIED_TIME = 5; |
|
27 | - public const SORT_BY_NAME_NATURAL = 6; |
|
21 | + public const SORT_BY_NONE = 0; |
|
22 | + public const SORT_BY_NAME = 1; |
|
23 | + public const SORT_BY_TYPE = 2; |
|
24 | + public const SORT_BY_ACCESSED_TIME = 3; |
|
25 | + public const SORT_BY_CHANGED_TIME = 4; |
|
26 | + public const SORT_BY_MODIFIED_TIME = 5; |
|
27 | + public const SORT_BY_NAME_NATURAL = 6; |
|
28 | 28 | |
29 | - private $iterator; |
|
30 | - private $sort; |
|
29 | + private $iterator; |
|
30 | + private $sort; |
|
31 | 31 | |
32 | - /** |
|
33 | - * @param int|callable $sort The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a PHP callback) |
|
34 | - * |
|
35 | - * @throws \InvalidArgumentException |
|
36 | - */ |
|
37 | - public function __construct(\Traversable $iterator, $sort, bool $reverseOrder = false) |
|
38 | - { |
|
39 | - $this->iterator = $iterator; |
|
40 | - $order = $reverseOrder ? -1 : 1; |
|
32 | + /** |
|
33 | + * @param int|callable $sort The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a PHP callback) |
|
34 | + * |
|
35 | + * @throws \InvalidArgumentException |
|
36 | + */ |
|
37 | + public function __construct(\Traversable $iterator, $sort, bool $reverseOrder = false) |
|
38 | + { |
|
39 | + $this->iterator = $iterator; |
|
40 | + $order = $reverseOrder ? -1 : 1; |
|
41 | 41 | |
42 | - if (self::SORT_BY_NAME === $sort) { |
|
43 | - $this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) { |
|
44 | - return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); |
|
45 | - }; |
|
46 | - } elseif (self::SORT_BY_NAME_NATURAL === $sort) { |
|
47 | - $this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) { |
|
48 | - return $order * strnatcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); |
|
49 | - }; |
|
50 | - } elseif (self::SORT_BY_TYPE === $sort) { |
|
51 | - $this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) { |
|
52 | - if ($a->isDir() && $b->isFile()) { |
|
53 | - return -$order; |
|
54 | - } elseif ($a->isFile() && $b->isDir()) { |
|
55 | - return $order; |
|
56 | - } |
|
42 | + if (self::SORT_BY_NAME === $sort) { |
|
43 | + $this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) { |
|
44 | + return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); |
|
45 | + }; |
|
46 | + } elseif (self::SORT_BY_NAME_NATURAL === $sort) { |
|
47 | + $this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) { |
|
48 | + return $order * strnatcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); |
|
49 | + }; |
|
50 | + } elseif (self::SORT_BY_TYPE === $sort) { |
|
51 | + $this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) { |
|
52 | + if ($a->isDir() && $b->isFile()) { |
|
53 | + return -$order; |
|
54 | + } elseif ($a->isFile() && $b->isDir()) { |
|
55 | + return $order; |
|
56 | + } |
|
57 | 57 | |
58 | - return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); |
|
59 | - }; |
|
60 | - } elseif (self::SORT_BY_ACCESSED_TIME === $sort) { |
|
61 | - $this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) { |
|
62 | - return $order * ($a->getATime() - $b->getATime()); |
|
63 | - }; |
|
64 | - } elseif (self::SORT_BY_CHANGED_TIME === $sort) { |
|
65 | - $this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) { |
|
66 | - return $order * ($a->getCTime() - $b->getCTime()); |
|
67 | - }; |
|
68 | - } elseif (self::SORT_BY_MODIFIED_TIME === $sort) { |
|
69 | - $this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) { |
|
70 | - return $order * ($a->getMTime() - $b->getMTime()); |
|
71 | - }; |
|
72 | - } elseif (self::SORT_BY_NONE === $sort) { |
|
73 | - $this->sort = $order; |
|
74 | - } elseif (\is_callable($sort)) { |
|
75 | - $this->sort = $reverseOrder ? static function (\SplFileInfo $a, \SplFileInfo $b) use ($sort) { return -$sort($a, $b); } : $sort; |
|
76 | - } else { |
|
77 | - throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.'); |
|
78 | - } |
|
79 | - } |
|
58 | + return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); |
|
59 | + }; |
|
60 | + } elseif (self::SORT_BY_ACCESSED_TIME === $sort) { |
|
61 | + $this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) { |
|
62 | + return $order * ($a->getATime() - $b->getATime()); |
|
63 | + }; |
|
64 | + } elseif (self::SORT_BY_CHANGED_TIME === $sort) { |
|
65 | + $this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) { |
|
66 | + return $order * ($a->getCTime() - $b->getCTime()); |
|
67 | + }; |
|
68 | + } elseif (self::SORT_BY_MODIFIED_TIME === $sort) { |
|
69 | + $this->sort = static function (\SplFileInfo $a, \SplFileInfo $b) use ($order) { |
|
70 | + return $order * ($a->getMTime() - $b->getMTime()); |
|
71 | + }; |
|
72 | + } elseif (self::SORT_BY_NONE === $sort) { |
|
73 | + $this->sort = $order; |
|
74 | + } elseif (\is_callable($sort)) { |
|
75 | + $this->sort = $reverseOrder ? static function (\SplFileInfo $a, \SplFileInfo $b) use ($sort) { return -$sort($a, $b); } : $sort; |
|
76 | + } else { |
|
77 | + throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.'); |
|
78 | + } |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @return \Traversable |
|
83 | - */ |
|
84 | - #[\ReturnTypeWillChange] |
|
85 | - public function getIterator() |
|
86 | - { |
|
87 | - if (1 === $this->sort) { |
|
88 | - return $this->iterator; |
|
89 | - } |
|
81 | + /** |
|
82 | + * @return \Traversable |
|
83 | + */ |
|
84 | + #[\ReturnTypeWillChange] |
|
85 | + public function getIterator() |
|
86 | + { |
|
87 | + if (1 === $this->sort) { |
|
88 | + return $this->iterator; |
|
89 | + } |
|
90 | 90 | |
91 | - $array = iterator_to_array($this->iterator, true); |
|
91 | + $array = iterator_to_array($this->iterator, true); |
|
92 | 92 | |
93 | - if (-1 === $this->sort) { |
|
94 | - $array = array_reverse($array); |
|
95 | - } else { |
|
96 | - uasort($array, $this->sort); |
|
97 | - } |
|
93 | + if (-1 === $this->sort) { |
|
94 | + $array = array_reverse($array); |
|
95 | + } else { |
|
96 | + uasort($array, $this->sort); |
|
97 | + } |
|
98 | 98 | |
99 | - return new \ArrayIterator($array); |
|
100 | - } |
|
99 | + return new \ArrayIterator($array); |
|
100 | + } |
|
101 | 101 | } |
@@ -20,40 +20,40 @@ |
||
20 | 20 | */ |
21 | 21 | class DateRangeFilterIterator extends \FilterIterator |
22 | 22 | { |
23 | - private $comparators = []; |
|
24 | - |
|
25 | - /** |
|
26 | - * @param \Iterator $iterator The Iterator to filter |
|
27 | - * @param DateComparator[] $comparators An array of DateComparator instances |
|
28 | - */ |
|
29 | - public function __construct(\Iterator $iterator, array $comparators) |
|
30 | - { |
|
31 | - $this->comparators = $comparators; |
|
32 | - |
|
33 | - parent::__construct($iterator); |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Filters the iterator values. |
|
38 | - * |
|
39 | - * @return bool true if the value should be kept, false otherwise |
|
40 | - */ |
|
41 | - #[\ReturnTypeWillChange] |
|
42 | - public function accept() |
|
43 | - { |
|
44 | - $fileinfo = $this->current(); |
|
45 | - |
|
46 | - if (!file_exists($fileinfo->getPathname())) { |
|
47 | - return false; |
|
48 | - } |
|
49 | - |
|
50 | - $filedate = $fileinfo->getMTime(); |
|
51 | - foreach ($this->comparators as $compare) { |
|
52 | - if (!$compare->test($filedate)) { |
|
53 | - return false; |
|
54 | - } |
|
55 | - } |
|
56 | - |
|
57 | - return true; |
|
58 | - } |
|
23 | + private $comparators = []; |
|
24 | + |
|
25 | + /** |
|
26 | + * @param \Iterator $iterator The Iterator to filter |
|
27 | + * @param DateComparator[] $comparators An array of DateComparator instances |
|
28 | + */ |
|
29 | + public function __construct(\Iterator $iterator, array $comparators) |
|
30 | + { |
|
31 | + $this->comparators = $comparators; |
|
32 | + |
|
33 | + parent::__construct($iterator); |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Filters the iterator values. |
|
38 | + * |
|
39 | + * @return bool true if the value should be kept, false otherwise |
|
40 | + */ |
|
41 | + #[\ReturnTypeWillChange] |
|
42 | + public function accept() |
|
43 | + { |
|
44 | + $fileinfo = $this->current(); |
|
45 | + |
|
46 | + if (!file_exists($fileinfo->getPathname())) { |
|
47 | + return false; |
|
48 | + } |
|
49 | + |
|
50 | + $filedate = $fileinfo->getMTime(); |
|
51 | + foreach ($this->comparators as $compare) { |
|
52 | + if (!$compare->test($filedate)) { |
|
53 | + return false; |
|
54 | + } |
|
55 | + } |
|
56 | + |
|
57 | + return true; |
|
58 | + } |
|
59 | 59 | } |