|
1
|
|
|
<?php |
|
2
|
|
|
namespace EZAMA{ |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
class AVCIterator implements \ArrayAccess, \Iterator, \Countable, \JsonSerializable |
|
6
|
|
|
{ |
|
7
|
|
|
protected $container=array(); |
|
8
|
|
|
protected $keys=array(); |
|
9
|
|
|
protected $simple; |
|
10
|
|
|
public function __construct($keys, $container) |
|
11
|
|
|
{ |
|
12
|
|
|
$this->container=$container; |
|
13
|
|
|
$this->keys=$keys; |
|
14
|
|
|
$this->simple=!is_array($container); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function offsetSet($key, $value) |
|
18
|
|
|
{ |
|
19
|
|
|
if ($this->isSimple()) { |
|
20
|
|
|
$this->keys[$key]=is_int($value) ? $value : 0; |
|
21
|
|
|
} else { |
|
22
|
|
|
$this->container[array_search($key, $this->keys, true)]=is_int($value) ? $value : 0; |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function offsetGet($key) |
|
27
|
|
|
{ |
|
28
|
|
|
if ($this->isSimple()) { |
|
29
|
|
|
return $this->keys[$key]; |
|
30
|
|
|
} else { |
|
31
|
|
|
return $this->container[array_search($key, $this->keys, true)]; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function offsetExists($key) |
|
36
|
|
|
{ |
|
37
|
|
|
if ($this->isSimple()) { |
|
38
|
|
|
return isset($this->keys[$key]); |
|
39
|
|
|
} else { |
|
40
|
|
|
return false!==array_search($key, $this->keys, true); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function offsetUnset($key) |
|
45
|
|
|
{ |
|
46
|
|
|
if ($this->isSimple()) { |
|
47
|
|
|
unset($this->keys[$key]); |
|
48
|
|
|
} else { |
|
49
|
|
|
unset($this->container[$k=array_search($key, $this->keys, true)], $this->keys[$k]); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
public function count() |
|
55
|
|
|
{ |
|
56
|
|
|
return count($this->keys); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function key() |
|
60
|
|
|
{ |
|
61
|
|
|
if ($this->isSimple()) { |
|
62
|
|
|
return key($this->keys); |
|
63
|
|
|
} else { |
|
64
|
|
|
return $this->keys[key($this->keys)]; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function current() |
|
69
|
|
|
{ |
|
70
|
|
|
if ($this->isSimple()) { |
|
71
|
|
|
return current($this->keys); |
|
72
|
|
|
} else { |
|
73
|
|
|
return current($this->container); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function rewind() |
|
78
|
|
|
{ |
|
79
|
|
|
if ($this->isSimple()) { |
|
80
|
|
|
reset($this->keys); |
|
81
|
|
|
} else { |
|
82
|
|
|
reset($this->keys); |
|
83
|
|
|
reset($this->container); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function next() |
|
88
|
|
|
{ |
|
89
|
|
|
if ($this->isSimple()) { |
|
90
|
|
|
next($this->keys); |
|
91
|
|
|
} else { |
|
92
|
|
|
next($this->keys)&&next($this->container); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function valid() |
|
97
|
|
|
{ |
|
98
|
|
|
$key=key($this->keys); |
|
99
|
|
|
return null!==$key&&false!==$key; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function jsonSerialize() |
|
103
|
|
|
{ |
|
104
|
|
|
if ($this->isSimple()) { |
|
105
|
|
|
return $this->keys; |
|
106
|
|
|
} else { |
|
107
|
|
|
$callback=function($value, $count) { |
|
108
|
|
|
return array(self::prepareJson($value), $count); |
|
109
|
|
|
}; |
|
110
|
|
|
return array_map($callback, $this->keys, $this->container); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function isSimple() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->simple; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
private static function prepareJson($value) |
|
120
|
|
|
{ |
|
121
|
|
|
switch (gettype($value)) { |
|
122
|
|
|
case "resource": |
|
123
|
|
|
case "resource(closed)": |
|
124
|
|
|
throw new \InvalidArgumentException("Resource type detected while trying to prepare JsonSerialize "); |
|
125
|
|
|
case "object": |
|
126
|
|
|
if (in_array('Serializable', class_implements(get_class($value)))) { |
|
127
|
|
|
try { |
|
128
|
|
|
$serialize=serialize($value); |
|
129
|
|
|
return $serialize; |
|
130
|
|
|
} catch (\Exception $e) { |
|
131
|
|
|
throw new \InvalidArgumentException($e->getMessage()); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
return serialize($value); |
|
135
|
|
|
default: |
|
136
|
|
|
return $value; |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|