|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Marwan Al-Soltany <[email protected]> |
|
5
|
|
|
* @copyright Marwan Al-Soltany 2021 |
|
6
|
|
|
* For the full copyright and license information, please view |
|
7
|
|
|
* the LICENSE file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace MAKS\Velox\Helper; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A class that serves as a holder for various miscellaneous utility function. |
|
16
|
|
|
*/ |
|
17
|
|
|
final class Misc |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Gets a value from an array via dot-notation. |
|
21
|
|
|
* |
|
22
|
|
|
* @param array &$array The array to get the value from. |
|
23
|
|
|
* @param string $key The dotted key representation. |
|
24
|
|
|
* @param mixed $default [optional] The default fallback value. |
|
25
|
|
|
* |
|
26
|
|
|
* @return mixed The requested value if found otherwise the default parameter. |
|
27
|
|
|
*/ |
|
28
|
44 |
|
public static function getArrayValueByKey(array &$array, string $key, $default = null) |
|
29
|
|
|
{ |
|
30
|
44 |
|
if (!count($array)) { |
|
31
|
1 |
|
return $default; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
44 |
|
$data = &$array; |
|
35
|
|
|
|
|
36
|
44 |
|
if (strpos($key, '.') !== false) { |
|
37
|
38 |
|
$parts = explode('.', $key); |
|
38
|
|
|
|
|
39
|
38 |
|
foreach ($parts as $part) { |
|
40
|
38 |
|
if (!isset($data[$part])) { |
|
41
|
6 |
|
return $default; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
38 |
|
$data = &$data[$part]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
37 |
|
return $data ?? $default; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
20 |
|
return $data[$key] ?? $default; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Sets a value of an array via dot-notation. |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $array The array to set the value in. |
|
57
|
|
|
* @param string $key The dotted key representation. |
|
58
|
|
|
* @param mixed $value The value to set. |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool True on success. |
|
61
|
|
|
*/ |
|
62
|
36 |
|
public static function setArrayValueByKey(array &$array, string $key, $value): bool |
|
63
|
|
|
{ |
|
64
|
36 |
|
if (!strlen($key)) { |
|
65
|
1 |
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
36 |
|
$parts = explode('.', $key); |
|
69
|
36 |
|
$lastPart = array_pop($parts); |
|
70
|
|
|
|
|
71
|
36 |
|
$data = &$array; |
|
72
|
|
|
|
|
73
|
36 |
|
if (!empty($parts)) { |
|
74
|
6 |
|
foreach ($parts as $part) { |
|
75
|
6 |
|
if (!isset($data[$part])) { |
|
76
|
4 |
|
$data[$part] = []; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
6 |
|
$data = &$data[$part]; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
36 |
|
$data[$lastPart] = $value; |
|
84
|
|
|
|
|
85
|
36 |
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Cuts a value of an array via dot-notation, the value will be returned and the key will be unset from the array. |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $array The array to cut the value from. |
|
92
|
|
|
* @param string $key The dotted key representation. |
|
93
|
|
|
* |
|
94
|
|
|
* @return mixed The requested value if found otherwise the default parameter. |
|
95
|
|
|
*/ |
|
96
|
8 |
|
public static function cutArrayValueByKey(array &$array, string $key, $default = null) |
|
97
|
|
|
{ |
|
98
|
8 |
|
$cut = function (&$array, $key) use ($default, &$cut) { |
|
99
|
8 |
|
if (!is_array($array)) { |
|
100
|
1 |
|
return; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
8 |
|
if (count($parts = explode('.', $key)) > 1) { |
|
104
|
3 |
|
return $cut($array[$parts[0]], implode('.', array_slice($parts, 1))); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
8 |
|
$value = $array[$key] ?? $default; |
|
108
|
|
|
|
|
109
|
8 |
|
unset($array[$key]); |
|
110
|
|
|
|
|
111
|
8 |
|
return $value; |
|
112
|
8 |
|
}; |
|
113
|
|
|
|
|
114
|
8 |
|
return $cut($array, $key, $default); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Gets a private, protected, or public property (default, static, or constant) of an object. |
|
120
|
|
|
* |
|
121
|
|
|
* @param object $object Class instance. |
|
122
|
|
|
* @param string $property Property name. |
|
123
|
|
|
* |
|
124
|
|
|
* @return mixed The property value. |
|
125
|
|
|
* |
|
126
|
|
|
* @throws \Exception On failure. |
|
127
|
|
|
*/ |
|
128
|
6 |
|
public static function getObjectProperty($object, string $property) |
|
129
|
|
|
{ |
|
130
|
6 |
|
return \Closure::bind(function ($object, $property) { |
|
131
|
6 |
|
$return = null; |
|
132
|
|
|
|
|
133
|
|
|
try { |
|
134
|
6 |
|
$class = get_class($object); |
|
135
|
6 |
|
if (defined($class . '::' . $property)) { |
|
136
|
1 |
|
$return = constant($class . '::' . $property); |
|
137
|
6 |
|
} elseif (isset($object::$$property)) { |
|
138
|
5 |
|
$return = $object::$$property; |
|
139
|
3 |
|
} elseif (isset($object->{$property})) { |
|
140
|
3 |
|
$return = $object->{$property}; |
|
141
|
|
|
} else { |
|
142
|
6 |
|
throw new \Exception("No default, static, or constant property with the name '{$property}' exists!"); |
|
143
|
|
|
} |
|
144
|
1 |
|
} catch (\Exception $error) { |
|
145
|
1 |
|
throw new \Exception(sprintf('%s() failed!', __METHOD__), $error->getCode(), $error); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
6 |
|
return $return; |
|
149
|
6 |
|
}, null, $object)($object, $property); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Sets a private, protected, or public property (default or static) of an object. |
|
154
|
|
|
* |
|
155
|
|
|
* @param object $object Class instance. |
|
156
|
|
|
* @param string $property Property name. |
|
157
|
|
|
* @param mixed $value Property value. |
|
158
|
|
|
* |
|
159
|
|
|
* @return mixed The new property value. |
|
160
|
|
|
* |
|
161
|
|
|
* @throws \Exception On failure. |
|
162
|
|
|
*/ |
|
163
|
14 |
|
public static function setObjectProperty($object, string $property, $value) |
|
164
|
|
|
{ |
|
165
|
14 |
|
return \Closure::bind(function ($object, $property, $value) { |
|
166
|
14 |
|
$return = null; |
|
167
|
|
|
|
|
168
|
|
|
try { |
|
169
|
14 |
|
if (isset($object::$$property)) { |
|
170
|
14 |
|
$return = $object::$$property = $value; |
|
171
|
1 |
|
} elseif (isset($object->{$property})) { |
|
172
|
1 |
|
$return = $object->{$property} = $value; |
|
173
|
|
|
} else { |
|
174
|
14 |
|
throw new \Exception("No default, static, or constant property with the name '{$property}' exists!"); |
|
175
|
|
|
} |
|
176
|
1 |
|
} catch (\Exception $error) { |
|
177
|
1 |
|
throw new \Exception(sprintf('%s() failed!', __METHOD__), $error->getCode(), $error); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
14 |
|
return $return; |
|
181
|
14 |
|
}, null, $object)($object, $property, $value); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Calls a private, protected, or public method on an object. |
|
186
|
|
|
* |
|
187
|
|
|
* @param object $object Class instance. |
|
188
|
|
|
* @param string $method Method name. |
|
189
|
|
|
* @param mixed ...$arguments |
|
190
|
|
|
* |
|
191
|
|
|
* @return mixed The function result, or false on error. |
|
192
|
|
|
* |
|
193
|
|
|
* @throws \Exception On failure or if the called function threw an exception. |
|
194
|
|
|
*/ |
|
195
|
1 |
|
public static function callObjectMethod($object, string $method, ...$arguments) |
|
196
|
|
|
{ |
|
197
|
1 |
|
return \Closure::bind(function ($object, $method, $arguments) { |
|
198
|
|
|
try { |
|
199
|
1 |
|
return call_user_func_array([$object, $method], $arguments); |
|
200
|
1 |
|
} catch (\Exception $error) { |
|
201
|
1 |
|
throw new \Exception(sprintf('%s() failed!', __METHOD__), $error->getCode(), $error); |
|
202
|
|
|
} |
|
203
|
1 |
|
}, null, $object)($object, $method, $arguments); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Interpolates context values into text placeholders. |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $text The text to interpolate. |
|
210
|
|
|
* @param array $context An associative array like `['varName' => 'varValue']`. |
|
211
|
|
|
* @param string $placeholderIndicator The wrapper that indicate a variable. Max 2 chars, anything else will be ignored and "{}" will be used instead. |
|
212
|
|
|
* |
|
213
|
|
|
* @return string The interpolated string. |
|
214
|
|
|
*/ |
|
215
|
18 |
|
public static function interpolate(string $text, array $context = [], string $placeholderIndicator = '{}'): string |
|
216
|
|
|
{ |
|
217
|
18 |
|
if (strlen($placeholderIndicator) !== 2) { |
|
218
|
1 |
|
$placeholderIndicator = '{}'; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
18 |
|
$replacements = []; |
|
222
|
18 |
|
foreach ($context as $key => $value) { |
|
223
|
|
|
// check that the value can be cast to string |
|
224
|
17 |
|
if (!is_array($value) && (!is_object($value) || method_exists($value, '__toString'))) { |
|
225
|
17 |
|
$replacements[$placeholderIndicator[0] . $key . $placeholderIndicator[1]] = $value; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
18 |
|
return strtr($text, $replacements); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Returns the passed key(s) from the backtrace. |
|
234
|
|
|
* |
|
235
|
|
|
* @param null|string|string[] $pluck [optional] $pluck The key to to get as a string or an array of strings (keys) from this list `[file, line, function, class, type, args]`, passing `null` will return the entire backtrace. |
|
236
|
|
|
* @param int $offset [optional] The offset of the backtrace, passing `-1` will reference the last item in the backtrace. |
|
237
|
|
|
* |
|
238
|
|
|
* @return string|int|array|null A string or int if a string is passed, an array if an array or null is passed, and null if no match was found. |
|
239
|
|
|
*/ |
|
240
|
5 |
|
public static function backtrace($pluck = null, ?int $offset = 0) |
|
241
|
|
|
{ |
|
242
|
5 |
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT); |
|
243
|
5 |
|
$plucked = null; |
|
244
|
|
|
|
|
245
|
5 |
|
if ($pluck === null) { |
|
246
|
1 |
|
return $backtrace; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
5 |
|
if ($offset === -1) { |
|
250
|
1 |
|
$offset = 0; |
|
251
|
1 |
|
$backtrace = array_reverse($backtrace); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
5 |
|
if (count($backtrace) < $offset + 1) { |
|
255
|
1 |
|
return null; |
|
256
|
5 |
|
} elseif (is_string($pluck)) { |
|
257
|
3 |
|
$plucked = isset($backtrace[$offset][$pluck]) ? $backtrace[$offset][$pluck] : null; |
|
258
|
3 |
|
} elseif (is_array($pluck)) { |
|
|
|
|
|
|
259
|
3 |
|
$plucked = []; |
|
260
|
3 |
|
foreach ($pluck as $key) { |
|
261
|
3 |
|
!isset($backtrace[$offset][$key]) ?: $plucked[$key] = $backtrace[$offset][$key]; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
5 |
|
return is_string($plucked) || is_array($plucked) && count($plucked, COUNT_RECURSIVE) ? $plucked : null; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|