1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: daniel.jurkovic |
5
|
|
|
* Date: 15.06.17 |
6
|
|
|
* Time: 14:14 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Macroparts\Vortex\ArrayTools; |
10
|
|
|
|
11
|
|
|
class RecursiveAccessor |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param array $array |
15
|
|
|
* @param string $path |
16
|
|
|
* @return mixed |
17
|
|
|
* @throws \InvalidArgumentException |
18
|
|
|
*/ |
19
|
36 |
View Code Duplication |
public static function readSingleFromPath($array, $path) |
|
|
|
|
20
|
|
|
{ |
21
|
36 |
|
if (self::isInvalidPath($path)) { |
22
|
6 |
|
return null; |
23
|
|
|
} |
24
|
|
|
|
25
|
30 |
|
$path = explode('.', $path); |
26
|
30 |
|
$lastKey = array_pop($path); |
27
|
30 |
|
$pointer = &$array; |
28
|
|
|
|
29
|
30 |
|
foreach ($path as $newPosition) { |
30
|
12 |
|
if (!isset($pointer[$newPosition]) || !is_array($pointer[$newPosition])) { |
31
|
3 |
|
unset($pointer); |
32
|
3 |
|
return null; |
33
|
|
|
} |
34
|
9 |
|
$pointer = &$pointer[$newPosition]; |
35
|
18 |
|
} |
36
|
|
|
|
37
|
27 |
|
$value = $pointer[$lastKey]; |
38
|
27 |
|
unset($pointer); |
39
|
27 |
|
return $value; |
40
|
|
|
} |
41
|
|
|
|
42
|
63 |
|
private static function isInvalidPath($path) |
43
|
|
|
{ |
44
|
63 |
|
return !self::isValidPath($path); |
45
|
|
|
} |
46
|
|
|
|
47
|
63 |
|
private static function isValidPath($path) |
48
|
|
|
{ |
49
|
63 |
|
return is_string($path) && $path !== ''; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array $array |
54
|
|
|
* @param string $path |
55
|
|
|
*/ |
56
|
9 |
|
public static function unsetInPath(&$array, $path) |
57
|
|
|
{ |
58
|
9 |
|
$path = explode('.', $path); |
59
|
9 |
|
$lastKey = array_pop($path); |
60
|
9 |
|
$pointer = &$array; |
61
|
|
|
|
62
|
9 |
|
foreach ($path as $newPosition) { |
63
|
3 |
|
if (!isset($pointer[$newPosition]) || !is_array($pointer[$newPosition])) { |
64
|
3 |
|
unset($pointer); |
65
|
3 |
|
return; |
66
|
|
|
} |
67
|
3 |
|
$pointer = &$pointer[$newPosition]; |
68
|
6 |
|
} |
69
|
|
|
|
70
|
9 |
|
unset($pointer[$lastKey], $pointer); |
71
|
9 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $array |
75
|
|
|
* @param string $path |
76
|
|
|
* @param string $type |
77
|
|
|
* @return null |
78
|
|
|
*/ |
79
|
12 |
View Code Duplication |
public static function castInPath(&$array, $path, $type) |
|
|
|
|
80
|
|
|
{ |
81
|
12 |
|
if (self::isInvalidPath($path)) { |
82
|
3 |
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
9 |
|
$path = explode('.', $path); |
86
|
9 |
|
$lastKey = array_pop($path); |
87
|
9 |
|
$pointer = &$array; |
88
|
|
|
|
89
|
9 |
|
foreach ($path as $newPosition) { |
90
|
9 |
|
if (!isset($pointer[$newPosition]) || !is_array($pointer[$newPosition])) { |
91
|
3 |
|
unset($pointer); |
92
|
3 |
|
return; |
93
|
|
|
} |
94
|
6 |
|
$pointer = &$pointer[$newPosition]; |
95
|
4 |
|
} |
96
|
|
|
|
97
|
6 |
|
settype($pointer[$lastKey], $type); |
98
|
6 |
|
unset($pointer); |
99
|
6 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* The writeNestedValue function is simply (over)writing data to arrays, this one tries to merge data when possible |
103
|
|
|
* |
104
|
|
|
* @param array $array |
105
|
|
|
* @param string $path |
106
|
|
|
* @param mixed $value |
107
|
|
|
*/ |
108
|
30 |
|
public static function integrateIntoPath(&$array, $path, $value) |
109
|
|
|
{ |
110
|
30 |
|
if (self::isInvalidPath($path)) { |
111
|
6 |
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
24 |
|
$path = explode('.', $path); |
115
|
24 |
|
$lastKey = array_pop($path); |
116
|
24 |
|
$pointer = &$array; |
117
|
|
|
|
118
|
24 |
|
foreach ($path as $newPosition) { |
119
|
6 |
|
if (!isset($pointer[$newPosition]) || !is_array($pointer[$newPosition])) { |
120
|
6 |
|
$pointer[$newPosition] = []; |
121
|
4 |
|
} |
122
|
6 |
|
$pointer = &$pointer[$newPosition]; |
123
|
16 |
|
} |
124
|
|
|
|
125
|
|
|
//If value and target are both array, concatenate, otherwise overwrite |
126
|
24 |
|
if (is_array($value) && is_array($pointer[$lastKey])) { |
127
|
9 |
|
$pointer[$lastKey] = $pointer[$lastKey] + $value; |
128
|
6 |
|
} else { |
129
|
15 |
|
$pointer[$lastKey] = $value; |
130
|
|
|
} |
131
|
|
|
|
132
|
24 |
|
unset($pointer); |
133
|
24 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param array $array |
137
|
|
|
* @param string $path |
138
|
|
|
* @param mixed $value |
139
|
|
|
*/ |
140
|
6 |
|
public static function writeToPath(&$array, $path, $value) |
141
|
|
|
{ |
142
|
6 |
|
$path = explode('.', $path); |
143
|
6 |
|
$lastKey = array_pop($path); |
144
|
6 |
|
$pointer = &$array; |
145
|
|
|
|
146
|
6 |
|
foreach ($path as $newPosition) { |
147
|
6 |
|
if (!isset($pointer[$newPosition]) || !is_array($pointer[$newPosition])) { |
148
|
6 |
|
$pointer[$newPosition] = []; |
149
|
4 |
|
} |
150
|
6 |
|
$pointer = &$pointer[$newPosition]; |
151
|
4 |
|
} |
152
|
|
|
|
153
|
6 |
|
$pointer[$lastKey] = $value; |
154
|
6 |
|
unset($pointer); |
155
|
6 |
|
} |
156
|
|
|
} |
157
|
|
|
|
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.