|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package domain |
|
4
|
|
|
* @subpackage models |
|
5
|
|
|
* @author Marius Orcsik <[email protected]> |
|
6
|
|
|
* @created 2015-04-15 |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace vsc\domain\models; |
|
9
|
|
|
|
|
10
|
|
|
trait IteratorTrait { |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $_current; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param string $sKey |
|
18
|
|
|
* @return bool |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract public function offsetExists($sKey); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $sOffset |
|
24
|
|
|
*/ |
|
25
|
1 |
|
protected function setCurrent($sOffset) { |
|
26
|
1 |
|
if ($this->offsetExists($sOffset)) |
|
27
|
1 |
|
$this->_current = $sOffset; |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
2 |
|
protected function getCurrent() { |
|
34
|
2 |
|
return $this->_current; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $sOffset |
|
40
|
|
|
* @param mixed $mValue |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
*/ |
|
43
|
|
|
public abstract function __set($sOffset, $mValue); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $sOffset |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
|
|
public abstract function __get($sOffset); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return int |
|
53
|
|
|
*/ |
|
54
|
|
|
public abstract function count(); |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
1 |
|
private function getPropertyNames() { |
|
60
|
1 |
|
$ret = []; |
|
61
|
1 |
|
$mirror = new \ReflectionClass($this); |
|
62
|
1 |
|
foreach ($mirror->getProperties() as $mirrorProperty) { |
|
63
|
1 |
|
$name = $mirrorProperty->getName(); |
|
64
|
1 |
|
if ($name == '_current') continue; |
|
65
|
1 |
|
$ret[] = $name; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
return $ret; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Iterator interface |
|
73
|
|
|
* @return mixed |
|
74
|
|
|
*/ |
|
75
|
3 |
|
public function current() |
|
76
|
|
|
{ |
|
77
|
3 |
|
if (is_null($this->_current)) { |
|
78
|
|
|
$this->rewind(); |
|
79
|
|
|
} |
|
80
|
3 |
|
return $this->__get($this->_current); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
3 |
|
public function key() |
|
87
|
|
|
{ |
|
88
|
3 |
|
if (is_null($this->_current)) { |
|
89
|
|
|
$this->rewind(); |
|
90
|
|
|
} |
|
91
|
3 |
|
return $this->_current; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function next() |
|
98
|
|
|
{ |
|
99
|
2 |
|
$aKeys = $this->getPropertyNames(); |
|
100
|
|
|
|
|
101
|
2 |
|
$iCurrent = array_search($this->_current, $aKeys); |
|
102
|
|
|
|
|
103
|
2 |
|
if ($iCurrent + 1 < $this->count()) { |
|
104
|
2 |
|
$this->_current = $aKeys[$iCurrent + 1]; |
|
105
|
|
|
} else { |
|
106
|
2 |
|
$this->_current = null; |
|
107
|
|
|
} |
|
108
|
2 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
11 |
|
public function rewind() |
|
114
|
|
|
{ |
|
115
|
11 |
|
$aKeys = $this->getPropertyNames(); |
|
116
|
|
|
|
|
117
|
11 |
|
if (is_array($aKeys) && isset($aKeys[0])) { |
|
118
|
10 |
|
$this->_current = $aKeys[0]; |
|
119
|
|
|
} |
|
120
|
11 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return bool |
|
124
|
|
|
*/ |
|
125
|
2 |
|
public function valid() |
|
126
|
|
|
{ |
|
127
|
2 |
|
$aKeys = $this->getPropertyNames(); |
|
128
|
|
|
|
|
129
|
2 |
|
if (in_array($this->_current, $aKeys)) { |
|
130
|
2 |
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|