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