| Total Complexity | 106 |
| Total Lines | 1300 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Str often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Str, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 59 | class Str |
||
| 60 | { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The cache of snake-cased words. |
||
| 64 | * |
||
| 65 | * @var array<string, string> |
||
| 66 | */ |
||
| 67 | protected static array $snakeCache = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The cache of camel-cased words. |
||
| 71 | * |
||
| 72 | * @var array<string, string> |
||
| 73 | */ |
||
| 74 | protected static array $camelCache = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The cache of studly-cased words. |
||
| 78 | * |
||
| 79 | * @var array<string, string> |
||
| 80 | */ |
||
| 81 | protected static array $studlyCache = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Convert an UTF-8 value to ASCII. |
||
| 85 | * @param string $value |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public static function toAscii(string $value): string |
||
| 89 | { |
||
| 90 | foreach (self::getChars() as $key => $val) { |
||
| 91 | $value = str_replace($val, $key, $value); |
||
| 92 | } |
||
| 93 | |||
| 94 | return (string)preg_replace('/[^\x20-\x7E]/u', '', $value); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Convert to camel case |
||
| 99 | * @param string $value |
||
| 100 | * @param bool $lcfirst |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public static function camel(string $value, bool $lcfirst = true): string |
||
| 104 | { |
||
| 105 | if (isset(self::$camelCache[$value])) { |
||
| 106 | return self::$camelCache[$value]; |
||
| 107 | } |
||
| 108 | |||
| 109 | $studly = static::studly($value); |
||
| 110 | return self::$camelCache[$value] = ($lcfirst ? lcfirst($studly) : $studly); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Convert an string to array |
||
| 115 | * @param string $value |
||
| 116 | * @param string $delimiter |
||
| 117 | * @param int $limit |
||
| 118 | * @return array<string> |
||
| 119 | */ |
||
| 120 | public static function toArray(string $value, string $delimiter = ', ', int $limit = 0): array |
||
| 121 | { |
||
| 122 | $string = trim($value, $delimiter . ' '); |
||
| 123 | if ($string === '') { |
||
| 124 | return []; |
||
| 125 | } |
||
| 126 | |||
| 127 | $values = []; |
||
| 128 | /** @var array<string> $rawList */ |
||
| 129 | $rawList = $limit < 1 |
||
| 130 | ? (array) explode($delimiter, $string) |
||
| 131 | : (array) explode($delimiter, $string, $limit); |
||
| 132 | |||
| 133 | foreach ($rawList as $val) { |
||
| 134 | $val = trim($val); |
||
| 135 | if ($val !== '') { |
||
| 136 | $values[] = $val; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | return $values; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Determine if a given string contains a given sub string. |
||
| 145 | * @param string $value |
||
| 146 | * @param string|array<mixed> $needles |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | public static function contains(string $value, $needles): bool |
||
| 150 | { |
||
| 151 | if (!is_array($needles)) { |
||
| 152 | $needles = [$needles]; |
||
| 153 | } |
||
| 154 | |||
| 155 | foreach ($needles as $needle) { |
||
| 156 | if ($needle !== '' && strpos($needle, $value) !== false) { |
||
| 157 | return true; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | return false; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Determine if a given string ends with a given sub string. |
||
| 166 | * @param string $value |
||
| 167 | * @param string|array<mixed> $needles |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | public static function endsWith(string $value, $needles): bool |
||
| 171 | { |
||
| 172 | if (!is_array($needles)) { |
||
| 173 | $needles = [$needles]; |
||
| 174 | } |
||
| 175 | |||
| 176 | foreach ($needles as $needle) { |
||
| 177 | if ($value === (string) substr($needle, -strlen($value))) { |
||
| 178 | return true; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | return false; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Determine if a given string starts with a given sub string. |
||
| 187 | * @param string $value |
||
| 188 | * @param string|array<mixed> $needles |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | public static function startsWith(string $value, $needles): bool |
||
| 192 | { |
||
| 193 | if (!is_array($needles)) { |
||
| 194 | $needles = [$needles]; |
||
| 195 | } |
||
| 196 | |||
| 197 | foreach ($needles as $needle) { |
||
| 198 | if ($needle !== '' && strpos($needle, $value) === 0) { |
||
| 199 | return true; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | return false; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Return the first line of multi line string |
||
| 208 | * @param string $value |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public static function firstLine(string $value): string |
||
| 212 | { |
||
| 213 | $str = trim($value); |
||
| 214 | |||
| 215 | if ($str === '') { |
||
| 216 | return ''; |
||
| 217 | } |
||
| 218 | |||
| 219 | if (strpos($str, "\n") > 0) { |
||
| 220 | $parts = explode("\n", $str); |
||
| 221 | |||
| 222 | return $parts[0] ?? ''; |
||
| 223 | } |
||
| 224 | |||
| 225 | return $str; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Cap a string with a single instance of a given value. |
||
| 230 | * @param string $value |
||
| 231 | * @param string $cap |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public static function finish(string $value, string $cap): string |
||
| 235 | { |
||
| 236 | $quoted = preg_quote($cap, '/'); |
||
| 237 | |||
| 238 | return (string) preg_replace('/(?:' . $quoted . ')+$/', '', $value) |
||
| 239 | . $cap; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Determine if a given string matches a given pattern. |
||
| 244 | * @param string $pattern |
||
| 245 | * @param string $value |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | public static function is(string $pattern, string $value): bool |
||
| 249 | { |
||
| 250 | if ($pattern === $value) { |
||
| 251 | return true; |
||
| 252 | } |
||
| 253 | |||
| 254 | $quoted = preg_quote($pattern, '#'); |
||
| 255 | |||
| 256 | // Asterisks are translated into zero-or-more regular expression wildcards |
||
| 257 | // to make it convenient to check if the strings starts with the given |
||
| 258 | // pattern such as "library/*", making any string check convenient. |
||
| 259 | $cleanQuoted = str_replace('\*', '.*', $quoted); |
||
| 260 | |||
| 261 | return (bool)preg_match('#^' . $cleanQuoted . '\z#', $value); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Return the length of the given string |
||
| 266 | * @param string|int $value |
||
| 267 | * @param string $encode |
||
| 268 | * @return int |
||
| 269 | */ |
||
| 270 | public static function length($value, string $encode = 'UTF-8'): int |
||
| 271 | { |
||
| 272 | if (!is_string($value)) { |
||
| 273 | $value = (string) $value; |
||
| 274 | } |
||
| 275 | |||
| 276 | $length = mb_strlen($value, $encode); |
||
| 277 | |||
| 278 | return $length !== false ? $length : -1; |
||
| 279 | } |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Add padding to string |
||
| 284 | * @param string|int $value |
||
| 285 | * @param int $length |
||
| 286 | * @param string $padStr |
||
| 287 | * @param int $type |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | public static function pad( |
||
| 291 | $value, |
||
| 292 | int $length, |
||
| 293 | string $padStr = ' ', |
||
| 294 | int $type = STR_PAD_BOTH |
||
| 295 | ): string { |
||
| 296 | if (!is_string($value)) { |
||
| 297 | $value = (string) $value; |
||
| 298 | } |
||
| 299 | |||
| 300 | return $length > 0 |
||
| 301 | ? str_pad($value, $length, $padStr, $type) |
||
| 302 | : $value; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Add padding to string to left |
||
| 307 | * @param string|int $value |
||
| 308 | * @param int $length |
||
| 309 | * @param string $padStr |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public static function padLeft( |
||
| 313 | $value, |
||
| 314 | int $length, |
||
| 315 | string $padStr = ' ' |
||
| 316 | ): string { |
||
| 317 | return self::pad($value, $length, $padStr, STR_PAD_LEFT); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Add padding to string to right |
||
| 322 | * @param string|int $value |
||
| 323 | * @param int $length |
||
| 324 | * @param string $padStr |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public static function padRight( |
||
| 328 | $value, |
||
| 329 | int $length, |
||
| 330 | string $padStr = ' ' |
||
| 331 | ): string { |
||
| 332 | return self::pad($value, $length, $padStr, STR_PAD_RIGHT); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Repeat the given string $length times |
||
| 337 | * @param string|int $value |
||
| 338 | * @param int $length |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public static function repeat($value, int $length = 1): string |
||
| 342 | { |
||
| 343 | if (!is_string($value)) { |
||
| 344 | $value = (string) $value; |
||
| 345 | } |
||
| 346 | |||
| 347 | return str_repeat($value, $length); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Limit the length of given string |
||
| 352 | * @param string $value |
||
| 353 | * @param int $length |
||
| 354 | * @param string $end |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public static function limit(string $value, int $length = 100, string $end = '...'): string |
||
| 358 | { |
||
| 359 | if (mb_strwidth($value, 'UTF-8') <= $length) { |
||
| 360 | return $value; |
||
| 361 | } |
||
| 362 | |||
| 363 | return rtrim(mb_strimwidth($value, 0, $length, '', 'UTF-8')) . $end; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Limit the number of words in a string. |
||
| 368 | * @param string $value |
||
| 369 | * @param int $length |
||
| 370 | * @param string $end |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | public static function words(string $value, int $length = 100, string $end = '...'): string |
||
| 374 | { |
||
| 375 | $matches = []; |
||
| 376 | preg_match('/^\s*+(?:\S++\s*+){1,' . $length . '}/u', $value, $matches); |
||
| 377 | |||
| 378 | if (!isset($matches[0]) || strlen($value) === strlen($matches[0])) { |
||
| 379 | return $value; |
||
| 380 | } |
||
| 381 | |||
| 382 | return rtrim($matches[0]) . $end; |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Replace the first match of the given string |
||
| 387 | * @param string $search |
||
| 388 | * @param string $replace |
||
| 389 | * @param string $value |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public static function replaceFirst(string $search, string $replace, string $value): string |
||
| 393 | { |
||
| 394 | $pos = strpos($value, $search); |
||
| 395 | if ($pos !== false) { |
||
| 396 | return substr_replace($value, $replace, $pos, strlen($search)); |
||
|
|
|||
| 397 | } |
||
| 398 | |||
| 399 | return $value; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Replace the last match of the given string |
||
| 404 | * @param string $search |
||
| 405 | * @param string $replace |
||
| 406 | * @param string $value |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | public static function replaceLast(string $search, string $replace, string $value): string |
||
| 410 | { |
||
| 411 | $pos = strrpos($value, $search); |
||
| 412 | |||
| 413 | if ($pos !== false) { |
||
| 414 | return substr_replace($value, $replace, $pos, strlen($search)); |
||
| 415 | } |
||
| 416 | |||
| 417 | return $value; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Put the string to title format |
||
| 422 | * @param string $value |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | public static function title(string $value): string |
||
| 426 | { |
||
| 427 | return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Generate a friendly "slug" from a given string. |
||
| 432 | * @param string $value |
||
| 433 | * @param string $separator |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | public static function slug(string $value, string $separator = '-'): string |
||
| 437 | { |
||
| 438 | $title = self::toAscii($value); |
||
| 439 | |||
| 440 | // Convert all dashes/underscores into separator |
||
| 441 | $flip = $separator === '-' ? '_' : '-'; |
||
| 442 | |||
| 443 | $utf8 = (string) preg_replace('![' . preg_quote($flip) . ']+!u', $separator, $title); |
||
| 444 | |||
| 445 | // Remove all characters that are not the separator, letters, numbers, |
||
| 446 | // or whitespace. |
||
| 447 | $alphaNum = (string) preg_replace( |
||
| 448 | '![^' . preg_quote($separator) . '\pL\pN\s]+!u', |
||
| 449 | '', |
||
| 450 | mb_strtolower($utf8) |
||
| 451 | ); |
||
| 452 | |||
| 453 | // Replace all separator characters and whitespace by a single separator |
||
| 454 | $removeWhitespace = (string) preg_replace( |
||
| 455 | '![' . preg_quote($separator) . '\s]+!u', |
||
| 456 | $separator, |
||
| 457 | $alphaNum |
||
| 458 | ); |
||
| 459 | |||
| 460 | return trim($removeWhitespace, $separator); |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Convert a string to snake case. |
||
| 465 | * @param string $value |
||
| 466 | * @param string $separator |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public static function snake(string $value, string $separator = '_'): string |
||
| 470 | { |
||
| 471 | $key = $value . $separator; |
||
| 472 | if (isset(self::$snakeCache[$key])) { |
||
| 473 | return self::$snakeCache[$key]; |
||
| 474 | } |
||
| 475 | |||
| 476 | if (!ctype_lower($value)) { |
||
| 477 | $replace = (string) preg_replace('/\s+/', '', $value); |
||
| 478 | |||
| 479 | $value = strtolower((string) preg_replace( |
||
| 480 | '/(.)(?=[A-Z])/', |
||
| 481 | '$1' . $separator, |
||
| 482 | $replace |
||
| 483 | )); |
||
| 484 | } |
||
| 485 | |||
| 486 | return self::$snakeCache[$key] = $value; |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Convert a value to studly caps case. |
||
| 491 | * @param string $value |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | public static function studly(string $value): string |
||
| 495 | { |
||
| 496 | $key = $value; |
||
| 497 | if (isset(self::$studlyCache[$key])) { |
||
| 498 | return self::$studlyCache[$key]; |
||
| 499 | } |
||
| 500 | |||
| 501 | $val = ucwords(str_replace(['-', '_'], ' ', $value)); |
||
| 502 | |||
| 503 | return self::$studlyCache[$key] = str_replace(' ', '', $val); |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Returns the portion of string specified by the start and |
||
| 508 | * length parameters. |
||
| 509 | * |
||
| 510 | * @param string $value |
||
| 511 | * @param int $start |
||
| 512 | * @param int|null $length |
||
| 513 | * @return string |
||
| 514 | */ |
||
| 515 | public static function substr(string $value, int $start = 0, ?int $length = null): string |
||
| 516 | { |
||
| 517 | return mb_substr($value, $start, $length, 'UTF-8'); |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Make a string's first character to upper case. |
||
| 522 | * @param string $value |
||
| 523 | * @return string |
||
| 524 | */ |
||
| 525 | public static function ucfirst(string $value): string |
||
| 526 | { |
||
| 527 | return static::upper( |
||
| 528 | static::substr($value, 0, 1) |
||
| 529 | ) . static::substr($value, 1); |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Split the string by length part |
||
| 534 | * @param string $value |
||
| 535 | * @param int $length |
||
| 536 | * @return array<int, string> |
||
| 537 | */ |
||
| 538 | public static function split(string $value, int $length = 1): array |
||
| 539 | { |
||
| 540 | if ($length < 1) { |
||
| 541 | return []; |
||
| 542 | } |
||
| 543 | |||
| 544 | if (self::isAscii($value)) { |
||
| 545 | $res = str_split($value, $length); |
||
| 546 | if ($res === false) { |
||
| 547 | return []; |
||
| 548 | } |
||
| 549 | |||
| 550 | return $res; |
||
| 551 | } |
||
| 552 | |||
| 553 | if (mb_strlen($value) <= $length) { |
||
| 554 | return [$value]; |
||
| 555 | } |
||
| 556 | $matches = []; |
||
| 557 | preg_match_all( |
||
| 558 | '/.{' . $length . '}|[^\x00]{1,' . $length . '}$/us', |
||
| 559 | $value, |
||
| 560 | $matches |
||
| 561 | ); |
||
| 562 | |||
| 563 | return $matches[0]; |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Check whether the given string contains only ASCII chars |
||
| 568 | * @param string $value |
||
| 569 | * @return bool |
||
| 570 | */ |
||
| 571 | public static function isAscii(string $value): bool |
||
| 572 | { |
||
| 573 | return (bool)!preg_match('/[^\x00-\x7F]/S', $value); |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Put string to lower case |
||
| 578 | * @param string $value |
||
| 579 | * @return string |
||
| 580 | */ |
||
| 581 | public static function lower(string $value): string |
||
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Put string to upper case |
||
| 588 | * @param string $value |
||
| 589 | * @return string |
||
| 590 | */ |
||
| 591 | public static function upper(string $value): string |
||
| 592 | { |
||
| 593 | return mb_strtoupper($value, 'UTF-8'); |
||
| 594 | } |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Return the unique ID |
||
| 598 | * @param int $length |
||
| 599 | * |
||
| 600 | * @return string |
||
| 601 | */ |
||
| 602 | public static function uniqId(int $length = 13): string |
||
| 603 | { |
||
| 604 | $bytes = random_bytes((int) ceil($length / 2)); |
||
| 605 | |||
| 606 | return (string)substr(bin2hex($bytes), 0, $length); |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Generate random string value |
||
| 611 | * @param int $length |
||
| 612 | * @return string |
||
| 613 | */ |
||
| 614 | public static function random(int $length = 16): string |
||
| 615 | { |
||
| 616 | $string = ''; |
||
| 617 | while (($len = strlen($string)) < $length) { |
||
| 618 | $size = $length - $len; |
||
| 619 | $bytes = random_bytes($size); |
||
| 620 | |||
| 621 | $string .= substr( |
||
| 622 | str_replace(['/', '+', '='], '', base64_encode($bytes)), |
||
| 623 | 0, |
||
| 624 | $size |
||
| 625 | ); |
||
| 626 | } |
||
| 627 | |||
| 628 | return $string; |
||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Generates a random string of a given type and length. Possible |
||
| 633 | * values for the first argument ($type) are: |
||
| 634 | * - alnum - alpha-numeric characters (including capitals) |
||
| 635 | * - alpha - alphabetical characters (including capitals) |
||
| 636 | * - hexdec - hexadecimal characters, 0-9 plus a-f |
||
| 637 | * - numeric - digit characters, 0-9 |
||
| 638 | * - nozero - digit characters, 1-9 |
||
| 639 | * - distinct - clearly distinct alpha-numeric characters. |
||
| 640 | * @param string $type |
||
| 641 | * @param int $length |
||
| 642 | * @return string |
||
| 643 | */ |
||
| 644 | public static function randomString(string $type = 'alnum', int $length = 8): string |
||
| 645 | { |
||
| 646 | $utf8 = false; |
||
| 647 | |||
| 648 | switch ($type) { |
||
| 649 | case 'alnum': |
||
| 650 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||
| 651 | break; |
||
| 652 | case 'alpha': |
||
| 653 | $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||
| 654 | break; |
||
| 655 | case 'lowalnum': |
||
| 656 | $pool = '0123456789abcdefghijklmnopqrstuvwxyz'; |
||
| 657 | break; |
||
| 658 | case 'hexdec': |
||
| 659 | $pool = '0123456789abcdef'; |
||
| 660 | break; |
||
| 661 | case 'numeric': |
||
| 662 | $pool = '0123456789'; |
||
| 663 | break; |
||
| 664 | case 'nozero': |
||
| 665 | $pool = '123456789'; |
||
| 666 | break; |
||
| 667 | case 'distinct': |
||
| 668 | $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ'; |
||
| 669 | break; |
||
| 670 | default: |
||
| 671 | $pool = (string)$type; |
||
| 672 | $utf8 = !self::isAscii($pool); |
||
| 673 | break; |
||
| 674 | } |
||
| 675 | |||
| 676 | // Split the pool into an array of characters |
||
| 677 | $pool = $utf8 ? self::split($pool, 1) : str_split($pool, 1); |
||
| 678 | // Largest pool key |
||
| 679 | $max = count($pool) - 1; |
||
| 680 | |||
| 681 | $str = ''; |
||
| 682 | for ($i = 0; $i < $length; $i++) { |
||
| 683 | // Select a random character from the pool and add it to the string |
||
| 684 | $str .= $pool[random_int(0, $max)]; |
||
| 685 | } |
||
| 686 | |||
| 687 | // Make sure alnum strings contain at least one letter and one digit |
||
| 688 | if ($type === 'alnum' && $length > 1) { |
||
| 689 | if (ctype_alpha($str)) { |
||
| 690 | // Add a random digit |
||
| 691 | $str[random_int(0, $length - 1)] = chr(random_int(48, 57)); |
||
| 692 | } elseif (ctype_digit($str)) { |
||
| 693 | // Add a random letter |
||
| 694 | $str[random_int(0, $length - 1)] = chr(random_int(65, 90)); |
||
| 695 | } |
||
| 696 | } |
||
| 697 | |||
| 698 | return $str; |
||
| 699 | } |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Create a simple random token-string |
||
| 703 | * @param int $length |
||
| 704 | * @param string $salt |
||
| 705 | * @return string |
||
| 706 | */ |
||
| 707 | public static function randomToken(int $length = 24, string $salt = ''): string |
||
| 708 | { |
||
| 709 | $string = ''; |
||
| 710 | $chars = '0456789abc1def2ghi3jkl'; |
||
| 711 | $maxVal = strlen($chars) - 1; |
||
| 712 | |||
| 713 | for ($i = 0; $i < $length; ++$i) { |
||
| 714 | $string .= $chars[random_int(0, $maxVal)]; |
||
| 715 | } |
||
| 716 | |||
| 717 | return md5($string . $salt); |
||
| 718 | } |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Convert the given value to string representation |
||
| 722 | * @param mixed $value |
||
| 723 | * @return string |
||
| 724 | */ |
||
| 725 | public static function stringify($value): string |
||
| 726 | { |
||
| 727 | if ($value === null) { |
||
| 728 | return 'null'; |
||
| 729 | } |
||
| 730 | |||
| 731 | if (is_bool($value)) { |
||
| 732 | return $value ? 'true' : 'false'; |
||
| 733 | } |
||
| 734 | |||
| 735 | if (is_string($value)) { |
||
| 736 | return sprintf('"%s"', $value); |
||
| 737 | } |
||
| 738 | |||
| 739 | if (is_scalar($value)) { |
||
| 740 | return (string) $value; |
||
| 741 | } |
||
| 742 | |||
| 743 | if (is_array($value)) { |
||
| 744 | return self::stringifyArray($value); |
||
| 745 | } |
||
| 746 | |||
| 747 | if (is_object($value)) { |
||
| 748 | return self::stringifyObject($value); |
||
| 749 | } |
||
| 750 | |||
| 751 | if (is_resource($value)) { |
||
| 752 | return sprintf('resource<%s>', get_resource_type($value)); |
||
| 753 | } |
||
| 754 | |||
| 755 | return gettype($value); |
||
| 756 | } |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Convert the given array to string representation |
||
| 760 | * @param array<mixed> $value |
||
| 761 | * @return string |
||
| 762 | */ |
||
| 763 | public static function stringifyArray(array $value): string |
||
| 764 | { |
||
| 765 | if (empty($value)) { |
||
| 766 | return '[]'; |
||
| 767 | } |
||
| 768 | |||
| 769 | $keys = array_keys($value); |
||
| 770 | $values = array_values($value); |
||
| 771 | [$firstKey] = $keys; |
||
| 772 | $ignoreKeys = $firstKey === 0; |
||
| 773 | |||
| 774 | return sprintf('[%s]', implode(', ', array_map( |
||
| 775 | function ($key, $value) use ($ignoreKeys) { |
||
| 776 | return $ignoreKeys |
||
| 777 | ? self::stringify($value) |
||
| 778 | : sprintf( |
||
| 779 | '%s => %s', |
||
| 780 | self::stringify($key), |
||
| 781 | self::stringify($value) |
||
| 782 | ); |
||
| 783 | }, |
||
| 784 | $keys, |
||
| 785 | $values |
||
| 786 | ))); |
||
| 787 | } |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Convert the given object to string representation |
||
| 791 | * @param object $value |
||
| 792 | * @return string |
||
| 793 | */ |
||
| 794 | public static function stringifyObject(object $value): string |
||
| 795 | { |
||
| 796 | $valueClass = get_class($value); |
||
| 797 | |||
| 798 | if ($value instanceof Throwable) { |
||
| 799 | return sprintf( |
||
| 800 | '%s { "%s", %s, %s #%s }', |
||
| 801 | $valueClass, |
||
| 802 | $value->getMessage(), |
||
| 803 | $value->getCode(), |
||
| 804 | $value->getFile(), |
||
| 805 | $value->getLine() |
||
| 806 | ); |
||
| 807 | } |
||
| 808 | |||
| 809 | if (method_exists($value, '__toString')) { |
||
| 810 | return sprintf('%s { %s }', $valueClass, $value->__toString()); |
||
| 811 | } |
||
| 812 | |||
| 813 | if (method_exists($value, 'toString')) { |
||
| 814 | return sprintf('%s { %s }', $valueClass, $value->toString()); |
||
| 815 | } |
||
| 816 | |||
| 817 | if ($value instanceof Traversable) { |
||
| 818 | return sprintf( |
||
| 819 | '%s %s', |
||
| 820 | $valueClass, |
||
| 821 | self::stringifyArray(iterator_to_array($value)) |
||
| 822 | ); |
||
| 823 | } |
||
| 824 | |||
| 825 | if ($value instanceof DateTimeInterface) { |
||
| 826 | return sprintf( |
||
| 827 | '%s { %s }', |
||
| 828 | $valueClass, |
||
| 829 | $value->format(DateTime::ATOM) |
||
| 830 | ); |
||
| 831 | } |
||
| 832 | |||
| 833 | if ($value instanceof JsonSerializable) { |
||
| 834 | return sprintf( |
||
| 835 | '%s {%s}', |
||
| 836 | $valueClass, |
||
| 837 | trim((string) json_encode($value->jsonSerialize()), '{}') |
||
| 838 | ); |
||
| 839 | } |
||
| 840 | |||
| 841 | return $valueClass; |
||
| 842 | } |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Return the ASCII replacement |
||
| 846 | * @return array<string, array<string>> |
||
| 847 | */ |
||
| 848 | private static function getChars(): array |
||
| 1359 | ], |
||
| 1360 | ]; |
||
| 1361 | } |
||
| 1362 | } |
||
| 1363 |