1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Graze DataStructure |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017 Nature Delivered Ltd. <http://graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @see http://github.com/graze/data-structure/blob/master/LICENSE |
11
|
|
|
* @link http://github.com/graze/data-structure |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataStructure\Container; |
15
|
|
|
|
16
|
|
|
use ArrayAccess; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* FlatContainer accepts key access using a delimiter to represent child arrays |
20
|
|
|
* |
21
|
|
|
* ## Examples |
22
|
|
|
* |
23
|
|
|
* ```php |
24
|
|
|
* $container = new FlatContainer(['first' => 'value', 'second' => ['child' => 1, 'other' => 2]]); |
25
|
|
|
* |
26
|
|
|
* $container->get('second.child') |
27
|
|
|
* // 1 |
28
|
|
|
* |
29
|
|
|
* ($container->has('first.child')) |
30
|
|
|
* // false |
31
|
|
|
* |
32
|
|
|
* $container->set('second.third', 3); |
33
|
|
|
* $container->getAll(); |
34
|
|
|
* // ['first' => 'value', 'second' => ['child' => 1, 'other' => 2, 'third'=> 3]] |
35
|
|
|
* |
36
|
|
|
* $container->remove('second.other'); |
37
|
|
|
* $container->getAll(); |
38
|
|
|
* // ['first' => 'value', 'second' => ['child' => 1, 'third'=> 3]] |
39
|
|
|
* ``` |
40
|
|
|
*/ |
41
|
|
|
class FlatContainer extends Container |
42
|
|
|
{ |
43
|
|
|
const DELIMITER = '.'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $key |
47
|
|
|
* |
48
|
|
|
* @return mixed|null |
49
|
|
|
*/ |
50
|
|
|
private function getChild($key) |
51
|
|
|
{ |
52
|
|
|
$top = $this->params; |
53
|
|
|
|
54
|
|
|
foreach (explode(static::DELIMITER, $key) as $node) { |
55
|
|
|
if (!isset($top[$node])) { |
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
$top = $top[$node]; |
59
|
|
|
} |
60
|
|
|
return $top; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $key |
65
|
|
|
* |
66
|
|
|
* @return string[] split the key into everything up to the last delimiter, and the last key |
67
|
|
|
*/ |
68
|
|
|
private function splitToLast($key) |
69
|
|
|
{ |
70
|
|
|
$split = explode(static::DELIMITER, $key); |
71
|
|
|
$key = implode(static::DELIMITER, array_slice($split, 0, -1)); |
|
|
|
|
72
|
|
|
$last = end($split); |
|
|
|
|
73
|
|
|
|
74
|
|
|
return [$key, $last]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $key The key to access, supports delimiter based child access (e.g. `parent.child.node`) |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function has($key) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
if (mb_strpos($key, static::DELIMITER) !== false) { |
85
|
|
|
return (!is_null($this->getChild($key))); |
86
|
|
|
} |
87
|
|
|
return parent::has($key); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $key The key to access, supports delimiter based child access (e.g. `parent.child.node`) |
92
|
|
|
* |
93
|
|
|
* @return mixed|null |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function get($key) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
if (mb_strpos($key, static::DELIMITER) !== false) { |
98
|
|
|
return $this->getChild($key); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return parent::get($key); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $key The key to access, supports delimiter based child access (e.g. `parent.child.node`) |
106
|
|
|
* @param mixed $value |
107
|
|
|
* |
108
|
|
|
* @return ContainerInterface |
109
|
|
|
*/ |
110
|
|
|
public function set($key, $value) |
111
|
|
|
{ |
112
|
|
|
return $this->doSet($key, $value); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $key |
117
|
|
|
* @param mixed $value |
118
|
|
|
* |
119
|
|
|
* @return ContainerInterface |
120
|
|
|
*/ |
121
|
|
|
protected function doSet($key, $value) |
122
|
|
|
{ |
123
|
|
|
if (mb_strpos($key, static::DELIMITER) !== false) { |
124
|
|
|
list($key, $last) = $this->splitToLast($key); |
125
|
|
|
|
126
|
|
|
$top = $this->get($key); |
127
|
|
View Code Duplication |
if (is_null($top) || (!is_array($top) && !($top instanceof ArrayAccess))) { |
|
|
|
|
128
|
|
|
$top = []; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($top instanceof ContainerInterface) { |
132
|
|
|
$top = $top->set($last, $value); |
133
|
|
|
} else { |
134
|
|
|
$top[$last] = $value; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->doSet($key, $top); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return parent::set($key, $value); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $key The key to access, supports delimiter based child access (e.g. `parent.child.node`) |
145
|
|
|
* |
146
|
|
|
* @return ContainerInterface |
147
|
|
|
*/ |
148
|
|
|
public function remove($key) |
149
|
|
|
{ |
150
|
|
|
return $this->doRemove($key); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $key |
155
|
|
|
* |
156
|
|
|
* @return ContainerInterface |
157
|
|
|
*/ |
158
|
|
|
protected function doRemove($key) |
159
|
|
|
{ |
160
|
|
|
if (mb_strpos($key, static::DELIMITER) !== false) { |
161
|
|
|
list($key, $last) = $this->splitToLast($key); |
162
|
|
|
|
163
|
|
|
$top = $this->get($key); |
164
|
|
View Code Duplication |
if (is_null($top) || (!is_array($top) && !($top instanceof ArrayAccess))) { |
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($top instanceof ContainerInterface) { |
169
|
|
|
$top = $top->remove($last); |
170
|
|
|
} else { |
171
|
|
|
unset($top[$last]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $this->doSet($key, $top); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return parent::remove($key); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.