/
RegexFilterIterator.php
156 lines (131 loc) · 3.2 KB
/
RegexFilterIterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/*
* This file is part of the webmozart/glob package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Webmozart\Glob\Iterator;
use FilterIterator;
use Iterator;
/**
* Filters an iterator by a regular expression.
*
* @since 1.0
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @see Glob
*/
class RegexFilterIterator extends FilterIterator
{
/**
* Mode: Filters the values of the inner iterator.
*/
const FILTER_VALUE = 1;
/**
* Mode: Filters the keys of the inner iterator.
*/
const FILTER_KEY = 2;
/**
* Mode: Return incrementing numbers as keys.
*/
const CURSOR_AS_KEY = 16;
/**
* Mode: Return the original keys as keys.
*/
const KEY_AS_KEY = 32;
/**
* @var string
*/
private $regExp;
/**
* @var string
*/
private $staticPrefix;
/**
* @var int
*/
private $cursor = 0;
/**
* @var int
*/
private $mode;
/**
* Creates a new iterator.
*
* @param string $regExp The regular expression to filter by.
* @param string $staticPrefix The static prefix of the regular
* expression.
* @param Iterator $innerIterator The filtered iterator.
* @param int $mode A bitwise combination of the mode constants.
*/
public function __construct($regExp, $staticPrefix, Iterator $innerIterator, $mode = null)
{
parent::__construct($innerIterator);
if (!($mode & (self::FILTER_KEY | self::FILTER_VALUE))) {
$mode |= self::FILTER_VALUE;
}
if (!($mode & (self::CURSOR_AS_KEY | self::KEY_AS_KEY))) {
$mode |= self::CURSOR_AS_KEY;
}
$this->regExp = $regExp;
$this->staticPrefix = $staticPrefix;
$this->mode = $mode;
}
/**
* Rewind the iterator to the first position.
*/
#[\ReturnTypeWillChange]
public function rewind()
{
parent::rewind();
$this->cursor = 0;
}
/**
* Returns the current position.
*
* @return int The current position.
*/
#[\ReturnTypeWillChange]
public function key()
{
if (!$this->valid()) {
return null;
}
if ($this->mode & self::KEY_AS_KEY) {
return parent::key();
}
return $this->cursor;
}
/**
* Advances to the next match.
*
* @see Iterator::next()
*/
#[\ReturnTypeWillChange]
public function next()
{
if ($this->valid()) {
parent::next();
++$this->cursor;
}
}
/**
* Accepts paths matching the glob.
*
* @return bool Whether the path is accepted.
*/
#[\ReturnTypeWillChange]
public function accept()
{
$path = ($this->mode & self::FILTER_VALUE) ? $this->current() : parent::key();
if (0 !== strpos($path, $this->staticPrefix)) {
return false;
}
$result = (bool) preg_match($this->regExp, $path);
return $result;
}
}