-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathHelper.php
More file actions
258 lines (224 loc) · 7.27 KB
/
Helper.php
File metadata and controls
258 lines (224 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
namespace Rakit\Validation;
class Helper
{
/**
* Determine if a given string matches a given pattern.
* Adapted from: https://github.com/illuminate/support/blob/v5.3.23/Str.php#L119
*
* @param string $pattern
* @param string $value
* @return bool
*/
public static function strIs(string $pattern, string $value): bool
{
if ($pattern == $value) {
return true;
}
$pattern = preg_quote($pattern, '#');
// Asterisks are translated into zero-or-more regular expression wildcards
// to make it convenient to check if the strings starts with the given
// pattern such as "library/*", making any string check convenient.
$pattern = str_replace('\*', '.*', $pattern);
return (bool) preg_match('#^'.$pattern.'\z#u', $value);
}
/**
* Check if an item or items exist in an array using "dot" notation.
* Adapted from: https://github.com/illuminate/support/blob/v5.3.23/Arr.php#L81
*
* @param array $array
* @param string $key
* @return bool
*/
public static function arrayHas(array $array, string $key): bool
{
if (array_key_exists($key, $array)) {
return true;
}
foreach (explode('.', $key) as $segment) {
if (is_array($array) && array_key_exists($segment, $array)) {
$array = $array[$segment];
} else {
return false;
}
}
return true;
}
/**
* Get an item from an array using "dot" notation.
* Adapted from: https://github.com/illuminate/support/blob/v5.3.23/Arr.php#L246
*
* @param array $array
* @param string|null $key
* @param mixed $default
* @return mixed
*/
public static function arrayGet(array $array, $key, $default = null)
{
if (is_null($key)) {
return $array;
}
if (array_key_exists($key, $array)) {
return $array[$key];
}
foreach (explode('.', $key) as $segment) {
if (is_array($array) && array_key_exists($segment, $array)) {
$array = $array[$segment];
} else {
return $default;
}
}
return $array;
}
/**
* Flatten a multi-dimensional associative array with dots.
* Adapted from: https://github.com/illuminate/support/blob/v5.3.23/Arr.php#L81
*
* @param array $array
* @param string $prepend
* @return array
*/
public static function arrayDot(array $array, string $prepend = ''): array
{
$results = [];
foreach ($array as $key => $value) {
if (is_array($value) && ! empty($value)) {
$results = array_merge($results, static::arrayDot($value, $prepend.$key.'.'));
} else {
$results[$prepend.$key] = $value;
}
}
return $results;
}
/**
* Set an item on an array or object using dot notation.
* Adapted from: https://github.com/illuminate/support/blob/v5.3.23/helpers.php#L437
*
* @param mixed $target
* @param string|array|null $key
* @param mixed $value
* @param bool $overwrite
* @return mixed
*/
public static function arraySet(&$target, $key, $value, $overwrite = true): array
{
if (is_null($key)) {
if ($overwrite) {
return $target = array_merge($target, $value);
}
return $target = array_merge($value, $target);
}
$segments = is_array($key) ? $key : explode('.', $key);
if (($segment = array_shift($segments)) === '*') {
if (! is_array($target)) {
$target = [];
}
if ($segments) {
foreach ($target as &$inner) {
static::arraySet($inner, $segments, $value, $overwrite);
}
} elseif ($overwrite) {
foreach ($target as &$inner) {
$inner = $value;
}
}
} elseif (is_array($target)) {
if ($segments) {
if (! array_key_exists($segment, $target)) {
$target[$segment] = [];
}
static::arraySet($target[$segment], $segments, $value, $overwrite);
} elseif ($overwrite || ! array_key_exists($segment, $target)) {
$target[$segment] = $value;
}
} else {
$target = [];
if ($segments) {
static::arraySet($target[$segment], $segments, $value, $overwrite);
} elseif ($overwrite) {
$target[$segment] = $value;
}
}
return $target;
}
/**
* Unset an item on an array or object using dot notation.
*
* @param mixed $target
* @param string|array $key
* @return mixed
*/
public static function arrayUnset(&$target, $key)
{
if (!is_array($target)) {
return $target;
}
$segments = is_array($key) ? $key : explode('.', $key);
$segment = array_shift($segments);
if ($segment == '*') {
$target = [];
} elseif ($segments) {
if (array_key_exists($segment, $target)) {
static::arrayUnset($target[$segment], $segments);
}
} elseif (array_key_exists($segment, $target)) {
unset($target[$segment]);
}
return $target;
}
/**
* Get snake_case format from given string
*
* @param string $value
* @param string $delimiter
* @return string
*/
public static function snakeCase(string $value, string $delimiter = '_'): string
{
if (! ctype_lower($value)) {
$value = preg_replace('/\s+/u', '', ucwords($value));
$value = strtolower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
}
return $value;
}
/**
* Join string[] to string with given $separator and $lastSeparator.
*
* @param array $pieces
* @param string $separator
* @param string|null $lastSeparator
* @return string
*/
public static function join(array $pieces, string $separator, string $lastSeparator = null): string
{
if (is_null($lastSeparator)) {
$lastSeparator = $separator;
}
$last = array_pop($pieces);
switch (count($pieces)) {
case 0:
return $last ?: '';
case 1:
return $pieces[0] . $lastSeparator . $last;
default:
return implode($separator, $pieces) . $lastSeparator . $last;
}
}
/**
* Wrap string[] by given $prefix and $suffix
*
* @param array $strings
* @param string $prefix
* @param string|null $suffix
* @return array
*/
public static function wraps(array $strings, string $prefix, string $suffix = null): array
{
if (is_null($suffix)) {
$suffix = $prefix;
}
return array_map(function ($str) use ($prefix, $suffix) {
return $prefix . $str . $suffix;
}, $strings);
}
}