|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Netdudes\DataSourceryBundle\Util; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This is a helper abstract class that implements array access ([]) to an underlying |
|
7
|
|
|
* set of items. |
|
8
|
|
|
*/ |
|
9
|
|
|
abstract class AbstractArrayAccessibleCollection implements \Countable, \Iterator, \ArrayAccess, \JsonSerializable |
|
10
|
|
|
{ |
|
11
|
|
|
private $_elements = []; |
|
12
|
|
|
|
|
13
|
|
|
private $_internalIndex = 0; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param array $initial |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(array $initial = []) |
|
19
|
|
|
{ |
|
20
|
|
|
foreach ($initial as $key => $value) { |
|
21
|
|
|
$this[$key] = $value; |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function offsetGet($offset) |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->_elements[$offset]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function offsetSet($offset, $value) |
|
31
|
|
|
{ |
|
32
|
|
|
if (is_null($offset)) { |
|
33
|
|
|
$this->_elements[] = $value; |
|
34
|
|
|
} else { |
|
35
|
|
|
$this->_elements[$offset] = $value; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function offsetUnset($offset) |
|
40
|
|
|
{ |
|
41
|
|
|
unset($this->_elements[$offset]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function count() |
|
45
|
|
|
{ |
|
46
|
|
|
return count($this->_elements); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function toArray() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->_elements; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function current() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->_elements[$this->_internalIndex]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function next() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->_internalIndex++; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function key() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->_internalIndex; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function valid() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->offsetExists($this->_internalIndex); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function offsetExists($offset) |
|
75
|
|
|
{ |
|
76
|
|
|
return array_key_exists($offset, $this->_elements); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function rewind() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->_internalIndex = 0; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function addAtIndex($index, $element) |
|
85
|
|
|
{ |
|
86
|
|
|
array_splice($this->_elements, $index, 0, [$element]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* (PHP 5 >= 5.4.0)<br/> |
|
91
|
|
|
* Specify data which should be serialized to JSON |
|
92
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
93
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
|
94
|
|
|
* which is a value of any type other than a resource. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function jsonSerialize() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->_elements; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|