-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallbackTransformIterator.php
68 lines (59 loc) · 2.08 KB
/
CallbackTransformIterator.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
<?php
/**
* Class CallbackTransformIterator implements an Iterator that transforms
* the values of another Iterator through a callback provided from the
* outside.
*
* @author Beat Vontobel
* @since 2011-11-22
* @package MeteoNews\phplib
* @subpackage Iterators
*/
class CallbackTransformIterator extends IteratorIterator
implements Iterator, Traversable, OuterIterator {
private $valueCallback;
private $keyCallback;
/**
* In addition to a plain IteratorIterator we take a second argument with
* a callback function to transform the values returned by the inner Iterator.
* The callback will be called with three arguments: The current value, the
* current key and the inner Iterator. Most of the time it will probably
* only use the first of the three arguments (the value). It must return
* the transformed value.
*
* The optional third argument is a second transformation callback working
* on the key.
*/
public function __construct(Traversable $iterator,
callable $callback = NULL,
callable $keyCallback = NULL) {
$this->valueCallback = $callback;
$this->keyCallback = $keyCallback;
parent::__construct($iterator);
}
/**
* current() will return the current value of the inner Iterator transformed
* by the callback provided to the constructor.
*/
public function current() {
if(isset($this->valueCallback))
return call_user_func($this->valueCallback,
parent::current(),
parent::key(),
$this->getInnerIterator());
return parent::current();
}
/**
* key() will return the current key of the inner Iterator transformed
* by the callback provided to the constructor.
*/
public function key() {
if(isset($this->keyCallback))
return call_user_func($this->keyCallback,
parent::current(),
parent::key(),
$this->getInnerIterator());
return parent::key();
}
}
?>