|
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 bool |
|
49
|
|
|
*/ |
|
50
|
|
View Code Duplication |
public function has($key) |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
if (mb_strpos($key, static::DELIMITER) !== false) { |
|
53
|
|
|
$top = $this->getAll(); |
|
54
|
|
|
|
|
55
|
|
|
foreach (explode(static::DELIMITER, $key) as $node) { |
|
56
|
|
|
if (!isset($top[$node])) { |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
$top = $top[$node]; |
|
60
|
|
|
} |
|
61
|
|
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
return parent::has($key); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $key |
|
68
|
|
|
* |
|
69
|
|
|
* @return mixed|null |
|
70
|
|
|
*/ |
|
71
|
|
View Code Duplication |
public function get($key) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
if (mb_strpos($key, static::DELIMITER) !== false) { |
|
74
|
|
|
$top = $this->getAll(); |
|
75
|
|
|
|
|
76
|
|
|
foreach (explode(static::DELIMITER, $key) as $node) { |
|
77
|
|
|
if (!isset($top[$node])) { |
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
$top = $top[$node]; |
|
81
|
|
|
} |
|
82
|
|
|
return $top; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return parent::get($key); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $key |
|
90
|
|
|
* @param mixed $value |
|
91
|
|
|
* |
|
92
|
|
|
* @return ContainerInterface |
|
93
|
|
|
*/ |
|
94
|
|
|
public function set($key, $value) |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->doSet($key, $value); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $key |
|
101
|
|
|
* @param mixed $value |
|
102
|
|
|
* |
|
103
|
|
|
* @return ContainerInterface |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function doSet($key, $value) |
|
106
|
|
|
{ |
|
107
|
|
|
if (mb_strpos($key, static::DELIMITER) !== false) { |
|
108
|
|
|
$split = explode(static::DELIMITER, $key); |
|
109
|
|
|
$key = implode(static::DELIMITER, array_slice($split, 0, -1)); |
|
|
|
|
|
|
110
|
|
|
$top = $this->get($key); |
|
|
|
|
|
|
111
|
|
View Code Duplication |
if (is_null($top) || (!is_array($top) && !($top instanceof ArrayAccess))) { |
|
|
|
|
|
|
112
|
|
|
$top = []; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$last = $split[count($split) - 1]; |
|
116
|
|
|
if ($top instanceof ContainerInterface) { |
|
117
|
|
|
$top = $top->set($last, $value); |
|
118
|
|
|
} else { |
|
119
|
|
|
$top[$last] = $value; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $this->doSet($key, $top); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return parent::set($key, $value); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $key |
|
130
|
|
|
* |
|
131
|
|
|
* @return ContainerInterface |
|
132
|
|
|
*/ |
|
133
|
|
|
public function remove($key) |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->doRemove($key); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param string $key |
|
140
|
|
|
* |
|
141
|
|
|
* @return ContainerInterface |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function doRemove($key) |
|
144
|
|
|
{ |
|
145
|
|
|
if (mb_strpos($key, static::DELIMITER) !== false) { |
|
146
|
|
|
$split = explode(static::DELIMITER, $key); |
|
147
|
|
|
$key = implode(static::DELIMITER, array_slice($split, 0, -1)); |
|
|
|
|
|
|
148
|
|
|
$top = $this->get($key); |
|
|
|
|
|
|
149
|
|
View Code Duplication |
if (is_null($top) || (!is_array($top) && !($top instanceof ArrayAccess))) { |
|
|
|
|
|
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$last = $split[count($split) - 1]; |
|
154
|
|
|
if ($top instanceof ContainerInterface) { |
|
155
|
|
|
$top = $top->remove($last); |
|
156
|
|
|
} else { |
|
157
|
|
|
unset($top[$last]); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $this->doSet($key, $top); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return parent::remove($key); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
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.