|
1
|
|
|
<?php |
|
2
|
|
|
namespace DICIT; |
|
3
|
|
|
|
|
4
|
|
|
class ArrayResolver implements \Iterator, \Countable, \ArrayAccess |
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
private $source; |
|
8
|
|
|
|
|
9
|
|
|
public function __construct(array $source = null) |
|
10
|
|
|
{ |
|
11
|
|
|
if ($source == null) { |
|
12
|
|
|
$source = array(); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
$this->source = $source; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function extract() |
|
19
|
|
|
{ |
|
20
|
|
|
return $this->source; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Resolves a value stored in an array, optionally by using dot notation to access nested elements. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $key |
|
27
|
|
|
* The key value to resolve. |
|
28
|
|
|
* @param mixed $default |
|
29
|
|
|
* @return mixed The resolved value or the provided default value. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function resolve($key, $default = null) |
|
32
|
|
|
{ |
|
33
|
|
|
$toReturn = $default; |
|
34
|
|
|
$dotted = explode(".", $key); |
|
35
|
|
|
|
|
36
|
|
|
if (count($dotted) > 1) { |
|
37
|
|
|
$currentDepthData = $this->source; |
|
38
|
|
|
|
|
39
|
|
|
foreach ($dotted as $paramKey) { |
|
40
|
|
|
if (array_key_exists($paramKey, $currentDepthData)) { |
|
41
|
|
|
$currentDepthData = $currentDepthData[$paramKey]; |
|
42
|
|
|
} else { |
|
43
|
|
|
return $this->wrapIfNecessary($default); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $this->wrapIfNecessary($currentDepthData); |
|
48
|
|
|
} |
|
49
|
|
|
elseif (array_key_exists($key, $this->source)) { |
|
50
|
|
|
$toReturn = $this->source[$key]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->wrapIfNecessary($toReturn); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function wrapIfNecessary($value) |
|
57
|
|
|
{ |
|
58
|
|
|
if (is_array($value)) { |
|
59
|
|
|
return new static($value); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $value; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function rewind() |
|
66
|
|
|
{ |
|
67
|
|
|
reset($this->source); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function current() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->wrapIfNecessary(current($this->source)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function key() |
|
76
|
|
|
{ |
|
77
|
|
|
return key($this->source); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function next() |
|
81
|
|
|
{ |
|
82
|
|
|
return next($this->source); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function valid() |
|
86
|
|
|
{ |
|
87
|
|
|
$key = key($this->source); |
|
88
|
|
|
|
|
89
|
|
|
return ($key !== null && $key !== false); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function count() |
|
93
|
|
|
{ |
|
94
|
|
|
return count($this->source); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function offsetSet($offset, $value) |
|
98
|
|
|
{ |
|
99
|
|
|
if (is_null($offset)) { |
|
100
|
|
|
$this->source[] = $value; |
|
101
|
|
|
} else { |
|
102
|
|
|
$this->source[$offset] = $value; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function offsetExists($offset) |
|
107
|
|
|
{ |
|
108
|
|
|
return isset($this->source[$offset]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function offsetUnset($offset) |
|
112
|
|
|
{ |
|
113
|
|
|
unset($this->source[$offset]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function offsetGet($offset) |
|
117
|
|
|
{ |
|
118
|
|
|
return isset($this->source[$offset]) ? $this->source[$offset] : null; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|