Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
65 / 65 |
|
100.00% |
26 / 26 |
CRAP | n/a |
0 / 0 |
|
| app_date | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
10 | |||
| app_time | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| str_slug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| str_random | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| str_random_alpha | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| str_random_num | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| str_random_alphanum | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| str_from_camel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| str_to_camel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| str_title_case | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| str_snake_case | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| str_kebab_case | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| array_collapse | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| array_flatten | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| array_divide | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| array_join | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| array_prepend | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| array_pull | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| array_sort | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| array_sort_desc | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| array_ksort | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| array_ksort_desc | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| array_usort | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| array_uksort | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| array_make | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is_json | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | use Pop\Utils\AbstractArray; |
| 5 | use Pop\Utils\Str; |
| 6 | use Pop\Utils\Arr; |
| 7 | |
| 8 | if (!function_exists('app_date')) { |
| 9 | /** |
| 10 | * Produce datetime string based on app timezone |
| 11 | * |
| 12 | * @param string $format |
| 13 | * @param mixed $timestamp |
| 14 | * @param string $env |
| 15 | * @param mixed $envDefault |
| 16 | * @return string |
| 17 | */ |
| 18 | function app_date(string $format, mixed $timestamp = null, string $env = 'APP_TIMEZONE', mixed $envDefault = null): string |
| 19 | { |
| 20 | if (!empty($timestamp)) { |
| 21 | if (!is_numeric($timestamp)) { |
| 22 | $timestamp = strtotime($timestamp); |
| 23 | if ($timestamp === false) { |
| 24 | throw new \InvalidArgumentException('Error: That timestamp is invalid.'); |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | $timezone = $_ENV[$env] ?? $envDefault; |
| 30 | $gm = function_exists('gmdate'); |
| 31 | |
| 32 | if ((($timezone == 'UTC') || (is_numeric($timezone) && ($timezone == 0))) && ($gm)) { |
| 33 | return gmdate($format, $timestamp); |
| 34 | } else { |
| 35 | if (is_numeric($timezone) && ($gm)) { |
| 36 | $timestamp = strtotime(gmdate($format, $timestamp)) + ($timezone * 3600); |
| 37 | } |
| 38 | return date($format, $timestamp); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | if (!function_exists('app_time')) { |
| 44 | /** |
| 45 | * Produce timestamp based on app timezone |
| 46 | * |
| 47 | * @param ?string $timestamp |
| 48 | * @param string $env |
| 49 | * @param mixed $envDefault |
| 50 | * @return int|null |
| 51 | */ |
| 52 | function app_time(?string $timestamp = null, $env = 'APP_TIMEZONE', mixed $envDefault = null): int|null |
| 53 | { |
| 54 | $datetime = app_date('Y-m-d H:i:s', $timestamp, $env, $envDefault); |
| 55 | return (!empty($datetime) && strtotime($datetime) !== false) ? strtotime($datetime) : null; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (!function_exists('str_slug')) { |
| 60 | /** |
| 61 | * Convert the string into an SEO-friendly slug. |
| 62 | * |
| 63 | * @param string $string |
| 64 | * @param string $separator |
| 65 | * @return string |
| 66 | */ |
| 67 | function str_slug(string $string, string $separator = '-'): string |
| 68 | { |
| 69 | return Str::createSlug($string, $separator); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (!function_exists('str_random')) { |
| 74 | /** |
| 75 | * Generate a random string of a predefined length. |
| 76 | * |
| 77 | * @param int $length |
| 78 | * @param int $case |
| 79 | * @return string |
| 80 | */ |
| 81 | function str_random(int $length, int $case = Str::MIXEDCASE): string |
| 82 | { |
| 83 | return Str::createRandom($length, $case); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (!function_exists('str_random_alpha')) { |
| 88 | /** |
| 89 | * Generate a random alphabetical string of a predefined length. |
| 90 | * |
| 91 | * @param int $length |
| 92 | * @param int $case |
| 93 | * @return string |
| 94 | */ |
| 95 | function str_random_alpha(int $length, int $case = Str::MIXEDCASE): string |
| 96 | { |
| 97 | return Str::createRandomAlpha($length, $case); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (!function_exists('str_random_num')) { |
| 102 | /** |
| 103 | * Generate a random numeric string of a predefined length. |
| 104 | * |
| 105 | * @param int $length |
| 106 | * @return string |
| 107 | */ |
| 108 | function str_random_num(int $length): string |
| 109 | { |
| 110 | return Str::createRandomNumeric($length); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (!function_exists('str_random_alphanum')) { |
| 115 | /** |
| 116 | * Generate a random alphanumeric string of a predefined length. |
| 117 | * |
| 118 | * @param int $length |
| 119 | * @param int $case |
| 120 | * @return string |
| 121 | */ |
| 122 | function str_random_alphanum(int $length, int $case = Str::MIXEDCASE): string |
| 123 | { |
| 124 | return Str::createRandomAlphaNum($length, $case); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (!function_exists('str_from_camel')) { |
| 129 | /** |
| 130 | * Convert a camelCase string using the $separator value passed |
| 131 | * |
| 132 | * @param string $string |
| 133 | * @param ?string $separator |
| 134 | * @param bool $preserveCase |
| 135 | * @return string |
| 136 | */ |
| 137 | function str_from_camel(string $string, ?string $separator = '-', bool $preserveCase = false): string |
| 138 | { |
| 139 | return Str::convertFromCamelCase($string, $separator, $preserveCase); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (!function_exists('str_to_camel')) { |
| 144 | /** |
| 145 | * Convert a camelCase string using the $separator value passed |
| 146 | * |
| 147 | * @param string $string |
| 148 | * @return string |
| 149 | */ |
| 150 | function str_to_camel(string $string): string |
| 151 | { |
| 152 | return Str::convertToCamelCase($string); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (!function_exists('str_title_case')) { |
| 157 | /** |
| 158 | * Convert a string to title case |
| 159 | * |
| 160 | * @param string $string |
| 161 | * @return string |
| 162 | */ |
| 163 | function str_title_case(string $string): string |
| 164 | { |
| 165 | return ucfirst(Str::convertToCamelCase($string)); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (!function_exists('str_snake_case')) { |
| 170 | /** |
| 171 | * Convert a string to snake case |
| 172 | * |
| 173 | * @param string $string |
| 174 | * @param bool $preserveCase |
| 175 | * @return string |
| 176 | */ |
| 177 | function str_snake_case(string $string, bool $preserveCase = false): string |
| 178 | { |
| 179 | return Str::convertFromCamelCase($string, '_', $preserveCase); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (!function_exists('str_kebab_case')) { |
| 184 | /** |
| 185 | * Convert a string to snake case |
| 186 | * |
| 187 | * @param string $string |
| 188 | * @param bool $preserveCase |
| 189 | * @return string |
| 190 | */ |
| 191 | function str_kebab_case(string $string, bool $preserveCase = false): string |
| 192 | { |
| 193 | return Str::convertFromCamelCase($string, '-', $preserveCase); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (!function_exists('array_collapse')) { |
| 198 | /** |
| 199 | * Collapse an array of arrays |
| 200 | * |
| 201 | * @param array|AbstractArray $array |
| 202 | * @return array |
| 203 | */ |
| 204 | function array_collapse(array|AbstractArray $array): array |
| 205 | { |
| 206 | return Arr::collapse($array); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (!function_exists('array_flatten')) { |
| 211 | /** |
| 212 | * Flatten a multi-dimensional array |
| 213 | * |
| 214 | * @param array|AbstractArray $array |
| 215 | * @param int|float $depth |
| 216 | * @return array |
| 217 | */ |
| 218 | function array_flatten(array|AbstractArray $array, int|float $depth = INF): array |
| 219 | { |
| 220 | return Arr::flatten($array, $depth); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (!function_exists('array_divide')) { |
| 225 | /** |
| 226 | * Divide the array in an array of keys and values |
| 227 | * |
| 228 | * @param array|AbstractArray $array |
| 229 | * @return array |
| 230 | */ |
| 231 | function array_divide(array|AbstractArray $array): array |
| 232 | { |
| 233 | return Arr::divide($array); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (!function_exists('array_join')) { |
| 238 | /** |
| 239 | * Join the array values into a string |
| 240 | * |
| 241 | * @param array|AbstractArray $array |
| 242 | * @param string $glue |
| 243 | * @param string $finalGlue |
| 244 | * @return string |
| 245 | */ |
| 246 | function array_join(array|AbstractArray $array, string $glue, string $finalGlue = ''): string |
| 247 | { |
| 248 | return Arr::join($array, $glue, $finalGlue); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if (!function_exists('array_prepend')) { |
| 253 | /** |
| 254 | * Prepend value to the array |
| 255 | * |
| 256 | * @param array|AbstractArray $array |
| 257 | * @param mixed $value |
| 258 | * @param mixed $key |
| 259 | * @return array |
| 260 | */ |
| 261 | function array_prepend(array|AbstractArray $array, mixed $value, mixed $key = null): array |
| 262 | { |
| 263 | return Arr::prepend($array, $value, $key); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (!function_exists('array_pull')) { |
| 268 | /** |
| 269 | * Pull value from the array and remove it |
| 270 | * |
| 271 | * @param array $array |
| 272 | * @param mixed $key |
| 273 | * @return mixed |
| 274 | */ |
| 275 | function array_pull(array &$array, mixed $key): mixed |
| 276 | { |
| 277 | return Arr::pull($array, $key); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if (!function_exists('array_sort')) { |
| 282 | /** |
| 283 | * Sort array |
| 284 | * |
| 285 | * @param array|AbstractArray $array |
| 286 | * @param int $flags |
| 287 | * @param bool $assoc |
| 288 | * @param bool $descending |
| 289 | * @return array |
| 290 | */ |
| 291 | function array_sort(array|AbstractArray $array, int $flags = SORT_REGULAR, bool $assoc = true, bool $descending = false): array |
| 292 | { |
| 293 | return Arr::sort($array, $flags, $assoc, $descending); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (!function_exists('array_sort_desc')) { |
| 298 | /** |
| 299 | * Sort array descending |
| 300 | * |
| 301 | * @param array|AbstractArray $array |
| 302 | * @param int $flags |
| 303 | * @param bool $assoc |
| 304 | * @return array |
| 305 | */ |
| 306 | function array_sort_desc(array|AbstractArray $array, int $flags = SORT_REGULAR, bool $assoc = true): array |
| 307 | { |
| 308 | return Arr::sortDesc($array, $flags, $assoc); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | if (!function_exists('array_ksort')) { |
| 313 | /** |
| 314 | * Sort array by keys |
| 315 | * |
| 316 | * @param array|AbstractArray $array |
| 317 | * @param int $flags |
| 318 | * @param bool $descending |
| 319 | * @return array |
| 320 | */ |
| 321 | function array_ksort(array|AbstractArray $array, int $flags = SORT_REGULAR, bool $descending = false): array |
| 322 | { |
| 323 | return Arr::ksort($array, $flags, $descending); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (!function_exists('array_ksort_desc')) { |
| 328 | /** |
| 329 | * Sort array by keys, descending |
| 330 | * |
| 331 | * @param array|AbstractArray $array |
| 332 | * @param int $flags |
| 333 | * @return array |
| 334 | */ |
| 335 | function array_ksort_desc(array|AbstractArray $array, int $flags = SORT_REGULAR): array |
| 336 | { |
| 337 | return Arr::ksortDesc($array, $flags); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | if (!function_exists('array_usort')) { |
| 342 | /** |
| 343 | * Sort array by user-defined callback |
| 344 | * |
| 345 | * @param array|AbstractArray $array |
| 346 | * @param mixed $callback |
| 347 | * @param bool $assoc |
| 348 | * @return array |
| 349 | */ |
| 350 | function array_usort(array|AbstractArray $array, mixed $callback, bool $assoc = true): array |
| 351 | { |
| 352 | return Arr::usort($array, $callback, $assoc); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if (!function_exists('array_uksort')) { |
| 357 | /** |
| 358 | * Sort array by user-defined callback using keys |
| 359 | * |
| 360 | * @param array|AbstractArray $array |
| 361 | * @param mixed $callback |
| 362 | * @return array |
| 363 | */ |
| 364 | function array_uksort(array|AbstractArray $array, mixed $callback): array |
| 365 | { |
| 366 | return Arr::uksort($array, $callback); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if (!function_exists('array_make')) { |
| 371 | /** |
| 372 | * Force value to be any array (if it is not one already) |
| 373 | * |
| 374 | * @param mixed $value |
| 375 | * @return array |
| 376 | */ |
| 377 | function array_make(mixed $value): array |
| 378 | { |
| 379 | return Arr::make($value); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | if (!function_exists('is_json')) { |
| 384 | /** |
| 385 | * Check if string is valid JSON |
| 386 | * |
| 387 | * @param mixed $json |
| 388 | * @return bool |
| 389 | */ |
| 390 | function is_json(mixed $json): bool |
| 391 | { |
| 392 | return (((is_string($json) && (json_decode($json) !== false)) && |
| 393 | (json_last_error() == JSON_ERROR_NONE))); |
| 394 | } |
| 395 | } |