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
|
|
|
use RecursiveArrayIterator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* FlatContainer accepts key access using a delimiter to represent child arrays |
21
|
|
|
* |
22
|
|
|
* ## Examples |
23
|
|
|
* |
24
|
|
|
* ```php |
25
|
|
|
* $container = new FlatContainer(['first' => 'value', 'second' => ['child' => 1, 'other' => 2]]); |
26
|
|
|
* |
27
|
|
|
* $container->get('second.child') |
28
|
|
|
* // 1 |
29
|
|
|
* |
30
|
|
|
* $container->has('first.child') |
31
|
|
|
* // false |
32
|
|
|
* |
33
|
|
|
* $container->set('second.third', 3); |
34
|
|
|
* $container->getAll(); |
35
|
|
|
* // ['first' => 'value', 'second' => ['child' => 1, 'other' => 2, 'third' => 3]] |
36
|
|
|
* |
37
|
|
|
* $container->remove('second.other'); |
38
|
|
|
* $container->getAll(); |
39
|
|
|
* // ['first' => 'value', 'second' => ['child' => 1, 'third' => 3]] |
40
|
|
|
* ``` |
41
|
|
|
*/ |
42
|
|
|
class FlatContainer extends Container |
43
|
|
|
{ |
44
|
|
|
const DEFAULT_DELIMITER = '.'; |
45
|
|
|
|
46
|
|
|
/** @var string */ |
47
|
|
|
protected $delimiter = self::DEFAULT_DELIMITER; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $key |
51
|
|
|
* |
52
|
|
|
* @return mixed|null |
53
|
|
|
*/ |
54
|
33 |
|
private function getChild($key) |
55
|
|
|
{ |
56
|
33 |
|
$top = $this->params; |
57
|
|
|
|
58
|
33 |
|
foreach (explode($this->delimiter, $key) as $node) { |
59
|
33 |
|
if (!isset($top[$node])) { |
60
|
16 |
|
return null; |
61
|
|
|
} |
62
|
31 |
|
$top = $top[$node]; |
63
|
|
|
} |
64
|
17 |
|
return $top; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $key |
69
|
|
|
* |
70
|
|
|
* @return string[] split the key into everything up to the last delimiter, and the last key |
71
|
|
|
*/ |
72
|
36 |
|
private function splitToLast($key) |
73
|
|
|
{ |
74
|
36 |
|
$split = explode($this->delimiter, $key); |
75
|
36 |
|
$key = implode($this->delimiter, array_slice($split, 0, -1)); |
76
|
36 |
|
$last = end($split); |
77
|
|
|
|
78
|
36 |
|
return [$key, $last]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $key The key to access, supports delimiter based child access (e.g. `parent.child.node`) |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
85 |
View Code Duplication |
public function has($key) |
|
|
|
|
87
|
|
|
{ |
88
|
85 |
|
if (mb_strpos($key, $this->delimiter) !== false) { |
89
|
15 |
|
return (!is_null($this->getChild($key))); |
90
|
|
|
} |
91
|
71 |
|
return parent::has($key); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $key The key to access, supports delimiter based child access (e.g. `parent.child.node`) |
96
|
|
|
* |
97
|
|
|
* @return mixed|null |
98
|
|
|
*/ |
99
|
58 |
View Code Duplication |
public function get($key) |
|
|
|
|
100
|
|
|
{ |
101
|
58 |
|
if (mb_strpos($key, $this->delimiter) !== false) { |
102
|
18 |
|
return $this->getChild($key); |
103
|
|
|
} |
104
|
|
|
|
105
|
45 |
|
return parent::get($key); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $key The key to access, supports delimiter based child access (e.g. `parent.child.node`) |
110
|
|
|
* @param mixed $value |
111
|
|
|
* |
112
|
|
|
* @return ContainerInterface |
113
|
|
|
*/ |
114
|
78 |
|
public function set($key, $value) |
115
|
|
|
{ |
116
|
78 |
|
return $this->doSet($key, $value); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $key |
121
|
|
|
* @param mixed $value |
122
|
|
|
* |
123
|
|
|
* @return ContainerInterface |
124
|
|
|
*/ |
125
|
95 |
|
protected function doSet($key, $value) |
126
|
|
|
{ |
127
|
95 |
|
if (mb_strpos($key, $this->delimiter) !== false) { |
128
|
19 |
|
list($key, $last) = $this->splitToLast($key); |
129
|
|
|
|
130
|
19 |
|
$top = $this->get($key); |
131
|
19 |
View Code Duplication |
if (is_null($top) || (!is_array($top) && !($top instanceof ArrayAccess))) { |
|
|
|
|
132
|
8 |
|
$top = []; |
133
|
|
|
} |
134
|
|
|
|
135
|
19 |
|
if ($top instanceof ContainerInterface) { |
136
|
9 |
|
$top = $top->set($last, $value); |
137
|
|
|
} else { |
138
|
14 |
|
$top[$last] = $value; |
139
|
|
|
} |
140
|
|
|
|
141
|
19 |
|
return $this->doSet($key, $top); |
142
|
|
|
} |
143
|
|
|
|
144
|
95 |
|
return parent::set($key, $value); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $key The key to access, supports delimiter based child access (e.g. `parent.child.node`) |
149
|
|
|
* |
150
|
|
|
* @return ContainerInterface |
151
|
|
|
*/ |
152
|
26 |
|
public function remove($key) |
153
|
|
|
{ |
154
|
26 |
|
return $this->doRemove($key); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $key |
159
|
|
|
* |
160
|
|
|
* @return ContainerInterface |
161
|
|
|
*/ |
162
|
28 |
|
protected function doRemove($key) |
163
|
|
|
{ |
164
|
28 |
|
if (mb_strpos($key, $this->delimiter) !== false) { |
165
|
17 |
|
list($key, $last) = $this->splitToLast($key); |
166
|
|
|
|
167
|
17 |
|
$top = $this->get($key); |
168
|
17 |
View Code Duplication |
if (is_null($top) || (!is_array($top) && !($top instanceof ArrayAccess))) { |
|
|
|
|
169
|
2 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
15 |
|
if ($top instanceof ContainerInterface) { |
173
|
6 |
|
$top = $top->remove($last); |
174
|
|
|
} else { |
175
|
9 |
|
unset($top[$last]); |
176
|
|
|
} |
177
|
|
|
|
178
|
15 |
|
return $this->doSet($key, $top); |
179
|
|
|
} |
180
|
|
|
|
181
|
11 |
|
return parent::remove($key); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $delimiter |
186
|
|
|
* |
187
|
|
|
* @return FlatContainer |
188
|
|
|
*/ |
189
|
1 |
|
public function setDelimiter($delimiter) |
190
|
|
|
{ |
191
|
1 |
|
$this->delimiter = $delimiter; |
192
|
1 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
1 |
|
public function getDelimiter() |
199
|
|
|
{ |
200
|
1 |
|
return $this->delimiter; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.