blob: be25d6b1aa9940ad037f146146da48a3d16c42e2 (
plain)
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
|
<?php
/**
* @brief Regular expression filter for string iterators
* @author Marcus Boerger
* @version 1.0
*
* Instances of this class act as a filter around iterators whose elements
* are strings. In other words you can put an iterator into the constructor
* and the instance will only return elements which match the given regular
* expression.
*/
class filter implements spl_forward
{
protected $it;
protected $regex;
protected $curr;
/**
* Constructs a filter around an iterator whose elemnts are strings.
* If the given iterator is of type spl_sequence then its rewind()
* method is called.
*
* @param it Object that implements at least spl_forward
* @patam regex Regular expression used as a filter.
*/
function __construct(spl_forward $it, $regex) {
if ($it instanceof spl_sequence) {
$it->rewind();
}
$this->it = $it;
$this->regex = $regex;
$this->fetch();
}
/**
* Destruct the iterator.
*/
function __destruct() {
unset($this->it);
}
protected function accept($curr) {
return ereg($this->regex, $curr);
}
/**
* Fetch next element and store it.
*
* @return void
*/
protected function fetch() {
$this->curr = false;
while ($this->it->has_more()) {
$curr = $this->it->current();
if ($this->accept($curr)) {
$this->curr = $curr;
return;
}
$this->it->next();
};
}
/**
* Move to next element
*
* @return void
*/
function next() {
$this->it->next();
$this->fetch();
}
/**
* @return Whether more elements are available
*/
function has_more() {
return $this->curr !== false;
}
/**
* @return The current value
*/
function current() {
return $this->curr;
}
/**
* hidden __clone
*/
protected function __clone() {
// disallow clone
}
}
?>
|