/
Wrapper.php
56 lines (47 loc) · 1.38 KB
/
Wrapper.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
<?php
namespace SparkLib\Iterator;
class Wrapper extends \SparkLib\Iterator {
protected $_iterator;
protected $_wrapperClass;
public function __construct (\Iterator $iterator, $wrapper_class = '\stdClass')
{
$this->_iterator = $iterator;
$this->_wrapperClass = $wrapper_class;
}
public function current ()
{
if ($this->_iterator->valid()) {
return $this->makeInstance($this->_iterator->current());
} else {
return $this->_iterator->current();
}
}
public function getNext ()
{
$result = $this->_iterator->getNext();
if ($result)
return $this->makeInstance($result);
else
return $result;
}
protected function makeInstance ($result)
{
$wclass = $this->_wrapperClass;
return new $wclass($result);
}
/**
* Pass method calls through to the underlying iterator.
*/
public function __call ($name, $arguments)
{
return call_user_func_array(array($this->_iterator, $name), $arguments);
}
/**
* These could be handled by __call(), except we're implementing
* the SPL Iterator interface, and it expects them to be defined.
*/
public function key () { return $this->_iterator->key(); }
public function next () { return $this->_iterator->next(); }
public function rewind () { return $this->_iterator->rewind(); }
public function valid () { return $this->_iterator->valid(); }
}