1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alchemy\Zippy\Package\Iterator; |
4
|
|
|
|
5
|
|
|
use Alchemy\Zippy\Package\PackagedResourceIterator; |
6
|
|
|
|
7
|
|
|
abstract class AbstractIterator implements PackagedResourceIterator |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @return \Iterator |
12
|
|
|
*/ |
13
|
|
|
abstract protected function buildIterator(); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @return \Iterator |
17
|
|
|
*/ |
18
|
|
|
public function getIterator() |
19
|
|
|
{ |
20
|
|
|
if (! isset($this->iterator)) { |
21
|
|
|
$this->iterator = $this->buildIterator(); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return $this->iterator; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
29
|
|
|
* Move forward to next element |
30
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
31
|
|
|
* @return void Any returned value is ignored. |
32
|
|
|
*/ |
33
|
|
|
public function next() |
34
|
|
|
{ |
35
|
|
|
$this->getIterator()->next(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
40
|
|
|
* Return the key of the current element |
41
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
42
|
|
|
* @return mixed scalar on success, or null on failure. |
43
|
|
|
*/ |
44
|
|
|
public function key() |
45
|
|
|
{ |
46
|
|
|
return $this->getIterator()->key(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
51
|
|
|
* Checks if current position is valid |
52
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
53
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
54
|
|
|
* Returns true on success or false on failure. |
55
|
|
|
*/ |
56
|
|
|
public function valid() |
57
|
|
|
{ |
58
|
|
|
return $this->getIterator()->valid(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
63
|
|
|
* Rewind the Iterator to the first element |
64
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
65
|
|
|
* @return void Any returned value is ignored. |
66
|
|
|
*/ |
67
|
|
|
public function rewind() |
68
|
|
|
{ |
69
|
|
|
$this->getIterator()->rewind(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: