|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Asymptix\helpers; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* All naming functionality. |
|
7
|
|
|
* |
|
8
|
|
|
* @category Asymptix PHP Framework |
|
9
|
|
|
* @author Dmytro Zarezenko <[email protected]> |
|
10
|
|
|
* @copyright (c) 2016, Dmytro Zarezenko |
|
11
|
|
|
* |
|
12
|
|
|
* @git https://github.com/Asymptix/Framework |
|
13
|
|
|
* @license http://opensource.org/licenses/MIT |
|
14
|
|
|
*/ |
|
15
|
|
|
class Naming { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Parse usual HTML notation complex field name into array. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $fieldName Field name. |
|
21
|
|
|
* |
|
22
|
|
|
* @return mixed String or array. |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function parseComplexName($fieldName) { |
|
25
|
|
|
$normName = str_replace( |
|
26
|
|
|
['][', '[', ']'], |
|
27
|
|
|
['|', '|', ''], |
|
28
|
|
|
$fieldName |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
$complexName = explode("|", $normName); |
|
32
|
|
|
if (empty($complexName)) { |
|
33
|
|
|
return ""; |
|
34
|
|
|
} elseif (count($complexName) == 1) { |
|
35
|
|
|
return $complexName[0]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $complexName; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns value of the complex array element by it's complex key. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $array Complex associated array (dictionary); |
|
45
|
|
|
* @param mixed $complexName List with hierarchy complex key or a string |
|
46
|
|
|
* value of complex or simple (one level) key. |
|
47
|
|
|
* |
|
48
|
|
|
* @return mixed Value of the array element if found. |
|
49
|
|
|
* @throws \Exception If can't find element by complex key or no name provided. |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function getValueByComplexName($array, $complexName) { |
|
52
|
|
|
if (!is_array($complexName)) { |
|
53
|
|
|
$complexName = self::parseComplexName($complexName); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (!empty($complexName)) { |
|
57
|
|
|
if (is_array($complexName)) { // Complex key is provided |
|
58
|
|
|
$temp = $array; |
|
59
|
|
|
|
|
60
|
|
|
foreach ($complexName as $key) { |
|
61
|
|
|
if (isset($temp[$key])) { |
|
62
|
|
|
$temp = $temp[$key]; |
|
63
|
|
|
} else { |
|
64
|
|
|
throw new \Exception("Invalid complex key"); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $temp; |
|
69
|
|
|
} else { // Simple key is provided |
|
70
|
|
|
if (isset($array[$complexName])) { |
|
71
|
|
|
return $array[$complexName]; |
|
72
|
|
|
} else { |
|
73
|
|
|
throw new \Exception("Invalid simple key"); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
throw new \Exception("No name provided"); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Sets value of the complex array element by it's complex key. |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $array Complex associated array (dictionary); |
|
84
|
|
|
* @param mixed $complexName List with hierarchy complex key or a string |
|
85
|
|
|
* value of complex or simple (one level) key. |
|
86
|
|
|
* @param mixed $value Value. |
|
87
|
|
|
* @param bool $rewrite Rewrite existed values with the same name tree or not. |
|
88
|
|
|
* |
|
89
|
|
|
* @throws \Exception If can't find element by complex key or no name provided. |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function setValueWithComplexName(&$array, $complexName, $value, $rewrite = false) { |
|
92
|
|
|
if (!is_array($complexName)) { |
|
93
|
|
|
$complexName = self::parseComplexName($complexName); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (!empty($complexName)) { |
|
97
|
|
|
if (is_array($complexName)) { // Complex key is provided |
|
98
|
|
|
for ($i = 0; $i < count($complexName); $i++) { |
|
|
|
|
|
|
99
|
|
|
$key = $complexName[$i]; |
|
100
|
|
|
|
|
101
|
|
|
if ($i < (count($complexName) - 1)) { |
|
102
|
|
|
if (!isset($array[$key])) { // declare value as empty array as not last element |
|
103
|
|
|
$array[$key] = []; |
|
104
|
|
|
} else { |
|
105
|
|
|
if (!is_array($array[$key])) { // detect if current value is array because not last element |
|
106
|
|
|
if ($rewrite) { |
|
107
|
|
|
$array[$key] = []; |
|
108
|
|
|
} else { |
|
109
|
|
|
throw new \Exception( |
|
110
|
|
|
"Try to assign value as array element to the not an array" |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
$array = &$array[$key]; |
|
116
|
|
|
} else { // last element |
|
117
|
|
|
$array[$key] = $value; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} else { // Simple key is provided |
|
121
|
|
|
$array[$complexName] = $value; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return; |
|
125
|
|
|
} |
|
126
|
|
|
throw new \Exception("No name provided"); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Removes value of the complex array element by it's complex key. |
|
131
|
|
|
* |
|
132
|
|
|
* @param array $array Complex associated array (dictionary); |
|
133
|
|
|
* @param mixed $complexName List with hierarchy complex key or a string |
|
134
|
|
|
* value of complex or simple (one level) key. |
|
135
|
|
|
* |
|
136
|
|
|
* @throws \Exception If no name provided. |
|
137
|
|
|
*/ |
|
138
|
|
|
public static function unsetValueWithComplexName(&$array, $complexName) { |
|
139
|
|
|
if (!is_array($complexName)) { |
|
140
|
|
|
$complexName = self::parseComplexName($complexName); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if (!empty($complexName)) { |
|
144
|
|
|
if (is_array($complexName)) { // Complex key is provided |
|
145
|
|
|
for ($i = 0; $i < count($complexName); $i++) { |
|
|
|
|
|
|
146
|
|
|
$key = $complexName[$i]; |
|
147
|
|
|
|
|
148
|
|
|
if ($i < (count($complexName) - 1)) { |
|
149
|
|
|
if (!isset($array[$key]) || !is_array($array[$key])) { |
|
150
|
|
|
break; |
|
151
|
|
|
} |
|
152
|
|
|
$array = &$array[$key]; |
|
153
|
|
|
} elseif(isset($array[$key])) { // last element |
|
154
|
|
|
unset($array[$key]); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
} elseif (isset($array[$complexName])) { // Simple key is provided |
|
158
|
|
|
unset($array[$complexName]); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return; |
|
162
|
|
|
} |
|
163
|
|
|
throw new \Exception("No name provided"); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Removes value of the complex array element by it's complex key. |
|
168
|
|
|
* |
|
169
|
|
|
* @param array $array Complex associated array (dictionary); |
|
170
|
|
|
* @param mixed $complexName List with hierarchy complex key or a string |
|
171
|
|
|
* value of complex or simple (one level) key. |
|
172
|
|
|
* |
|
173
|
|
|
* @throws \Exception If no name provided. |
|
174
|
|
|
*/ |
|
175
|
|
|
public static function deleteValueWithComplexName(&$array, $complexName) { |
|
176
|
|
|
self::unsetValueWithComplexName($array, $complexName); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Removes value of the complex array element by it's complex key. |
|
181
|
|
|
* |
|
182
|
|
|
* @param array $array Complex associated array (dictionary); |
|
183
|
|
|
* @param mixed $complexName List with hierarchy complex key or a string |
|
184
|
|
|
* value of complex or simple (one level) key. |
|
185
|
|
|
* |
|
186
|
|
|
* @throws \Exception If no name provided. |
|
187
|
|
|
*/ |
|
188
|
|
|
public static function removeValueWithComplexName(&$array, $complexName) { |
|
189
|
|
|
self::unsetValueWithComplexName($array, $complexName); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: