|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* \AppserverIo\Collections\Iter |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/appserver-io/collections |
|
18
|
|
|
* @link http://www.appserver.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppserverIo\Collections; |
|
22
|
|
|
|
|
23
|
|
|
use AppserverIo\Lang\Object; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This class is the default implementation of a Iterator |
|
27
|
|
|
* used for foreach constructs. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Tim Wagner <[email protected]> |
|
30
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
32
|
|
|
* @link https://github.com/appserver-io/collections |
|
33
|
|
|
* @link http://www.appserver.io |
|
34
|
|
|
*/ |
|
35
|
|
|
class Iter extends Object implements \Iterator |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Holds the internal array |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $arr = array(); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Constructor that initializes the internal member |
|
47
|
|
|
* with the array passed as parameter. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $array Holds the array |
|
50
|
|
|
*/ |
|
51
|
3 |
|
public function __construct($array) |
|
52
|
|
|
{ |
|
53
|
3 |
|
if (is_array($array)) { |
|
54
|
3 |
|
$this->arr = $array; |
|
55
|
3 |
|
} |
|
56
|
3 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Resets the internal array pointer to |
|
60
|
|
|
* the first entry. |
|
61
|
|
|
* |
|
62
|
|
|
* And returns the value therefore. |
|
63
|
|
|
* |
|
64
|
|
|
* @return mixed Holds the first value of the internal array |
|
65
|
|
|
*/ |
|
66
|
3 |
|
public function rewind() |
|
67
|
|
|
{ |
|
68
|
3 |
|
return reset($this->arr); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns the actual entry. |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed The actual entry of the internal array |
|
75
|
|
|
*/ |
|
76
|
3 |
|
public function current() |
|
77
|
|
|
{ |
|
78
|
3 |
|
return current($this->arr); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns the key of the actual entry. |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed The key of actual entry of the internal array |
|
85
|
|
|
*/ |
|
86
|
3 |
|
public function key() |
|
87
|
|
|
{ |
|
88
|
3 |
|
return key($this->arr); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns the next entry. |
|
93
|
|
|
* |
|
94
|
|
|
* @return mixed The next entry of the internal array |
|
95
|
|
|
*/ |
|
96
|
3 |
|
public function next() |
|
97
|
|
|
{ |
|
98
|
3 |
|
return next($this->arr); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Checks if the actual entry of the internal |
|
103
|
|
|
* array is not false. |
|
104
|
|
|
* |
|
105
|
|
|
* @return boolean TRUE if there is a actual entry in the internal array, else FALSE |
|
106
|
|
|
*/ |
|
107
|
3 |
|
public function valid() |
|
108
|
|
|
{ |
|
109
|
3 |
|
return $this->current() !== false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* This method sets the internal array pointer |
|
114
|
|
|
* to the end of the array and returns the |
|
115
|
|
|
* value therefore. |
|
116
|
|
|
* |
|
117
|
|
|
* @return mixed Holds the last value of the internal array |
|
118
|
|
|
*/ |
|
119
|
|
|
public function last() |
|
120
|
|
|
{ |
|
121
|
|
|
return end($this->arr); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|