MediaWiki
REL1_39
MappedIterator.php
Go to the documentation of this file.
1
<?php
28
class
MappedIterator
extends
FilterIterator {
30
protected
$vCallback
;
32
protected
$aCallback
;
34
protected
$cache
= [];
35
36
protected
$rewound
=
false
;
// boolean; whether rewind() has been called
37
54
public
function
__construct
( $iter,
$vCallback
, array $options = [] ) {
55
if
( is_array( $iter ) ) {
56
$baseIterator =
new
ArrayIterator( $iter );
57
} elseif ( $iter instanceof Iterator ) {
58
$baseIterator = $iter;
59
}
else
{
60
throw
new
UnexpectedValueException(
"Invalid base iterator provided."
);
61
}
62
parent::__construct( $baseIterator );
63
$this->vCallback =
$vCallback
;
64
$this->aCallback = $options[
'accept'
] ??
null
;
65
}
66
67
public
function
next
(): void {
68
$this->cache = [];
69
parent::next();
70
}
71
72
public
function
rewind
(): void {
73
$this->rewound =
true
;
74
$this->cache = [];
75
parent::rewind();
76
}
77
78
public
function
accept
(): bool {
79
$inner = $this->getInnerIterator();
80
'@phan-var Iterator $inner'
;
81
$value = call_user_func( $this->vCallback, $inner->current() );
82
$ok = ( $this->aCallback ) ? call_user_func( $this->aCallback, $value ) :
true
;
83
if
( $ok ) {
84
$this->cache[
'current'
] = $value;
85
}
86
87
return
$ok;
88
}
89
90
#[\ReturnTypeWillChange]
91
public
function
key
() {
92
$this->init();
93
94
return
parent::key();
95
}
96
97
public
function
valid
(): bool {
98
$this->init();
99
100
return
parent::valid();
101
}
102
103
#[\ReturnTypeWillChange]
104
public
function
current
() {
105
$this->init();
106
if
( parent::valid() ) {
107
return
$this->cache[
'current'
];
108
}
else
{
109
return
null
;
// out of range
110
}
111
}
112
116
protected
function
init
() {
117
if
( !$this->rewound ) {
118
$this->rewind();
119
}
120
}
121
}
MappedIterator
Convenience class for generating iterators from iterators.
Definition
MappedIterator.php:28
MappedIterator\key
key()
Definition
MappedIterator.php:91
MappedIterator\accept
accept()
Definition
MappedIterator.php:78
MappedIterator\__construct
__construct( $iter, $vCallback, array $options=[])
Build an new iterator from a base iterator by having the former wrap the later, returning the result ...
Definition
MappedIterator.php:54
MappedIterator\current
current()
Definition
MappedIterator.php:104
MappedIterator\valid
valid()
Definition
MappedIterator.php:97
MappedIterator\init
init()
Obviate the usual need for rewind() before using a FilterIterator in a manual loop.
Definition
MappedIterator.php:116
MappedIterator\$rewound
$rewound
Definition
MappedIterator.php:36
MappedIterator\$cache
array $cache
Definition
MappedIterator.php:34
MappedIterator\next
next()
Definition
MappedIterator.php:67
MappedIterator\rewind
rewind()
Definition
MappedIterator.php:72
MappedIterator\$aCallback
callable null $aCallback
Definition
MappedIterator.php:32
MappedIterator\$vCallback
callable $vCallback
Definition
MappedIterator.php:30
true
return true
Definition
router.php:92
includes
libs
MappedIterator.php
Generated on Mon Nov 25 2024 06:55:57 for MediaWiki by
1.10.0