1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alchemy\Resource\Metadata; |
4
|
|
|
|
5
|
|
|
class LazyMetadataIterator implements MetadataIterator |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var MetadataIterator |
9
|
|
|
*/ |
10
|
|
|
private $iterator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var callable |
14
|
|
|
*/ |
15
|
|
|
private $factory; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param callable $factory A callback that returns a MetadataIterator instance |
19
|
|
|
*/ |
20
|
|
|
public function __construct(callable $factory) |
21
|
|
|
{ |
22
|
|
|
$this->factory = $factory; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
private function getIterator() |
26
|
|
|
{ |
27
|
|
|
if ($this->iterator === null) { |
28
|
|
|
$factory = $this->factory; |
29
|
|
|
$this->iterator = $factory(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $this->iterator; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
37
|
|
|
* Move forward to next element |
38
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
39
|
|
|
* @return void Any returned value is ignored. |
40
|
|
|
*/ |
41
|
|
|
public function next() |
42
|
|
|
{ |
43
|
|
|
$this->getIterator()->next(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
48
|
|
|
* Return the key of the current element |
49
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
50
|
|
|
* @return mixed scalar on success, or null on failure. |
51
|
|
|
*/ |
52
|
|
|
public function key() |
53
|
|
|
{ |
54
|
|
|
return $this->getIterator()->key(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
59
|
|
|
* Checks if current position is valid |
60
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
61
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
62
|
|
|
* Returns true on success or false on failure. |
63
|
|
|
*/ |
64
|
|
|
public function valid() |
65
|
|
|
{ |
66
|
|
|
return $this->getIterator()->valid(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
71
|
|
|
* Rewind the Iterator to the first element |
72
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
73
|
|
|
* @return void Any returned value is ignored. |
74
|
|
|
*/ |
75
|
|
|
public function rewind() |
76
|
|
|
{ |
77
|
|
|
$this->getIterator()->rewind(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return Metadata |
82
|
|
|
*/ |
83
|
|
|
public function current() |
84
|
|
|
{ |
85
|
|
|
return $this->getIterator()->current(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|