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