| Total Complexity | 87 |
| Total Lines | 1176 |
| 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 |
||
| 53 | class Str |
||
| 54 | { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The cache of snake-cased words. |
||
| 58 | * |
||
| 59 | * @var array<string, string> |
||
| 60 | */ |
||
| 61 | protected static array $snakeCache = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The cache of camel-cased words. |
||
| 65 | * |
||
| 66 | * @var array<string, string> |
||
| 67 | */ |
||
| 68 | protected static array $camelCache = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The cache of studly-cased words. |
||
| 72 | * |
||
| 73 | * @var array<string, string> |
||
| 74 | */ |
||
| 75 | protected static array $studlyCache = []; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Convert an UTF-8 value to ASCII. |
||
| 79 | * @param string $value |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | public static function toAscii(string $value): string |
||
| 83 | { |
||
| 84 | foreach (self::getChars() as $key => $val) { |
||
| 85 | $value = str_replace($val, $key, $value); |
||
| 86 | } |
||
| 87 | |||
| 88 | return (string)preg_replace('/[^\x20-\x7E]/u', '', $value); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Convert to camel case |
||
| 93 | * @param string $value |
||
| 94 | * @param bool $lcfirst |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public static function camel(string $value, bool $lcfirst = true): string |
||
| 98 | { |
||
| 99 | if (isset(self::$camelCache[$value])) { |
||
| 100 | return self::$camelCache[$value]; |
||
| 101 | } |
||
| 102 | |||
| 103 | $studly = static::studly($value); |
||
| 104 | return self::$camelCache[$value] = ($lcfirst ? lcfirst($studly) : $studly); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Convert an string to array |
||
| 109 | * @param string $value |
||
| 110 | * @param string $delimiter |
||
| 111 | * @param int $limit |
||
| 112 | * @return array<string> |
||
| 113 | */ |
||
| 114 | public static function toArray(string $value, string $delimiter = ', ', int $limit = 0): array |
||
| 115 | { |
||
| 116 | $string = trim($value, $delimiter . ' '); |
||
| 117 | if ($string === '') { |
||
| 118 | return []; |
||
| 119 | } |
||
| 120 | |||
| 121 | $values = []; |
||
| 122 | /** @var array<string> $rawList */ |
||
| 123 | $rawList = $limit < 1 |
||
| 124 | ? (array) explode($delimiter, $string) |
||
| 125 | : (array) explode($delimiter, $string, $limit); |
||
| 126 | |||
| 127 | foreach ($rawList as $val) { |
||
| 128 | $val = trim($val); |
||
| 129 | if ($val !== '') { |
||
| 130 | $values[] = $val; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | return $values; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Determine if a given string contains a given sub string. |
||
| 139 | * @param string $value |
||
| 140 | * @param string|array<mixed> $needles |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | public static function contains(string $value, $needles): bool |
||
| 144 | { |
||
| 145 | if (!is_array($needles)) { |
||
| 146 | $needles = [$needles]; |
||
| 147 | } |
||
| 148 | |||
| 149 | foreach ($needles as $needle) { |
||
| 150 | if ($needle !== '' && strpos($needle, $value) !== false) { |
||
| 151 | return true; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | return false; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Determine if a given string ends with a given sub string. |
||
| 160 | * @param string $value |
||
| 161 | * @param string|array<mixed> $needles |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | public static function endsWith(string $value, $needles): bool |
||
| 165 | { |
||
| 166 | if (!is_array($needles)) { |
||
| 167 | $needles = [$needles]; |
||
| 168 | } |
||
| 169 | |||
| 170 | foreach ($needles as $needle) { |
||
| 171 | if ($value === (string) substr($needle, -strlen($value))) { |
||
| 172 | return true; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | return false; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Determine if a given string starts with a given sub string. |
||
| 181 | * @param string $value |
||
| 182 | * @param string|array<mixed> $needles |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public static function startsWith(string $value, $needles): bool |
||
| 186 | { |
||
| 187 | if (!is_array($needles)) { |
||
| 188 | $needles = [$needles]; |
||
| 189 | } |
||
| 190 | |||
| 191 | foreach ($needles as $needle) { |
||
| 192 | if ($needle !== '' && strpos($needle, $value) === 0) { |
||
| 193 | return true; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | return false; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Return the first line of multi line string |
||
| 202 | * @param string $value |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public static function firstLine(string $value): string |
||
| 206 | { |
||
| 207 | $str = trim($value); |
||
| 208 | |||
| 209 | if ($str === '') { |
||
| 210 | return ''; |
||
| 211 | } |
||
| 212 | |||
| 213 | if (strpos($str, "\n") > 0) { |
||
| 214 | $parts = explode("\n", $str); |
||
| 215 | |||
| 216 | return $parts[0] ?? ''; |
||
| 217 | } |
||
| 218 | |||
| 219 | return $str; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Cap a string with a single instance of a given value. |
||
| 224 | * @param string $value |
||
| 225 | * @param string $cap |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public static function finish(string $value, string $cap): string |
||
| 229 | { |
||
| 230 | $quoted = preg_quote($cap, '/'); |
||
| 231 | |||
| 232 | return (string) preg_replace('/(?:' . $quoted . ')+$/', '', $value) |
||
| 233 | . $cap; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Determine if a given string matches a given pattern. |
||
| 238 | * @param string $pattern |
||
| 239 | * @param string $value |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | public static function is(string $pattern, string $value): bool |
||
| 243 | { |
||
| 244 | if ($pattern === $value) { |
||
| 245 | return true; |
||
| 246 | } |
||
| 247 | |||
| 248 | $quoted = preg_quote($pattern, '#'); |
||
| 249 | |||
| 250 | // Asterisks are translated into zero-or-more regular expression wildcards |
||
| 251 | // to make it convenient to check if the strings starts with the given |
||
| 252 | // pattern such as "library/*", making any string check convenient. |
||
| 253 | $cleanQuoted = str_replace('\*', '.*', $quoted); |
||
| 254 | |||
| 255 | return (bool)preg_match('#^' . $cleanQuoted . '\z#', $value); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Return the length of the given string |
||
| 260 | * @param string|int $value |
||
| 261 | * @param string $encode |
||
| 262 | * @return int |
||
| 263 | */ |
||
| 264 | public static function length($value, string $encode = 'UTF-8'): int |
||
| 265 | { |
||
| 266 | if (!is_string($value)) { |
||
| 267 | $value = (string) $value; |
||
| 268 | } |
||
| 269 | |||
| 270 | $length = mb_strlen($value, $encode); |
||
| 271 | |||
| 272 | return $length !== false ? $length : -1; |
||
| 273 | } |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Add padding to string |
||
| 278 | * @param string|int $value |
||
| 279 | * @param int $length |
||
| 280 | * @param string $padStr |
||
| 281 | * @param int $type |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public static function pad( |
||
| 285 | $value, |
||
| 286 | int $length, |
||
| 287 | string $padStr, |
||
| 288 | int $type = STR_PAD_BOTH |
||
| 289 | ): string { |
||
| 290 | if (!is_string($value)) { |
||
| 291 | $value = (string) $value; |
||
| 292 | } |
||
| 293 | |||
| 294 | return $length > 0 |
||
| 295 | ? str_pad($value, $length, $padStr, $type) |
||
| 296 | : $value; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Add padding to string to left |
||
| 301 | * @param string|int $value |
||
| 302 | * @param int $length |
||
| 303 | * @param string $padStr |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public static function padLeft( |
||
| 307 | $value, |
||
| 308 | int $length, |
||
| 309 | string $padStr |
||
| 310 | ): string { |
||
| 311 | return self::pad($value, $length, $padStr, STR_PAD_LEFT); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Add padding to string to right |
||
| 316 | * @param string|int $value |
||
| 317 | * @param int $length |
||
| 318 | * @param string $padStr |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public static function padRight( |
||
| 322 | $value, |
||
| 323 | int $length, |
||
| 324 | string $padStr |
||
| 325 | ): string { |
||
| 326 | return self::pad($value, $length, $padStr, STR_PAD_RIGHT); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Repeat the given string $length times |
||
| 331 | * @param string|int $value |
||
| 332 | * @param int $length |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public static function repeat($value, int $length = 1): string |
||
| 336 | { |
||
| 337 | if (!is_string($value)) { |
||
| 338 | $value = (string) $value; |
||
| 339 | } |
||
| 340 | |||
| 341 | return str_repeat($value, $length); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Limit the length of given string |
||
| 346 | * @param string $value |
||
| 347 | * @param int $length |
||
| 348 | * @param string $end |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | public static function limit(string $value, int $length = 100, string $end = '...'): string |
||
| 352 | { |
||
| 353 | if (mb_strwidth($value, 'UTF-8') <= $length) { |
||
| 354 | return $value; |
||
| 355 | } |
||
| 356 | |||
| 357 | return rtrim(mb_strimwidth($value, 0, $length, '', 'UTF-8')) . $end; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Limit the number of words in a string. |
||
| 362 | * @param string $value |
||
| 363 | * @param int $length |
||
| 364 | * @param string $end |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public static function words(string $value, int $length = 100, string $end = '...'): string |
||
| 368 | { |
||
| 369 | $matches = []; |
||
| 370 | preg_match('/^\s*+(?:\S++\s*+){1,' . $length . '}/u', $value, $matches); |
||
| 371 | |||
| 372 | if (!isset($matches[0]) || strlen($value) === strlen($matches[0])) { |
||
| 373 | return $value; |
||
| 374 | } |
||
| 375 | |||
| 376 | return rtrim($matches[0]) . $end; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Replace the first match of the given string |
||
| 381 | * @param string $search |
||
| 382 | * @param string $replace |
||
| 383 | * @param string $value |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | public static function replaceFirst(string $search, string $replace, string $value): string |
||
| 387 | { |
||
| 388 | $pos = strpos($value, $search); |
||
| 389 | if ($pos !== false) { |
||
| 390 | return substr_replace($value, $replace, $pos, strlen($search)); |
||
|
|
|||
| 391 | } |
||
| 392 | |||
| 393 | return $value; |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Replace the last match of the given string |
||
| 398 | * @param string $search |
||
| 399 | * @param string $replace |
||
| 400 | * @param string $value |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public static function replaceLast(string $search, string $replace, string $value): string |
||
| 404 | { |
||
| 405 | $pos = strrpos($value, $search); |
||
| 406 | |||
| 407 | if ($pos !== false) { |
||
| 408 | return substr_replace($value, $replace, $pos, strlen($search)); |
||
| 409 | } |
||
| 410 | |||
| 411 | return $value; |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Put the string to title format |
||
| 416 | * @param string $value |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | public static function title(string $value): string |
||
| 420 | { |
||
| 421 | return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Generate a friendly "slug" from a given string. |
||
| 426 | * @param string $value |
||
| 427 | * @param string $separator |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | public static function slug(string $value, string $separator = '-'): string |
||
| 431 | { |
||
| 432 | $title = self::toAscii($value); |
||
| 433 | |||
| 434 | // Convert all dashes/underscores into separator |
||
| 435 | $flip = $separator === '-' ? '_' : '-'; |
||
| 436 | |||
| 437 | $utf8 = (string) preg_replace('![' . preg_quote($flip) . ']+!u', $separator, $title); |
||
| 438 | |||
| 439 | // Remove all characters that are not the separator, letters, numbers, |
||
| 440 | // or whitespace. |
||
| 441 | $alphaNum = (string) preg_replace( |
||
| 442 | '![^' . preg_quote($separator) . '\pL\pN\s]+!u', |
||
| 443 | '', |
||
| 444 | mb_strtolower($utf8) |
||
| 445 | ); |
||
| 446 | |||
| 447 | // Replace all separator characters and whitespace by a single separator |
||
| 448 | $removeWhitespace = (string) preg_replace( |
||
| 449 | '![' . preg_quote($separator) . '\s]+!u', |
||
| 450 | $separator, |
||
| 451 | $alphaNum |
||
| 452 | ); |
||
| 453 | |||
| 454 | return trim($removeWhitespace, $separator); |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Convert a string to snake case. |
||
| 459 | * @param string $value |
||
| 460 | * @param string $separator |
||
| 461 | * @return string |
||
| 462 | */ |
||
| 463 | public static function snake(string $value, string $separator = '_'): string |
||
| 464 | { |
||
| 465 | $key = $value . $separator; |
||
| 466 | if (isset(self::$snakeCache[$key])) { |
||
| 467 | return self::$snakeCache[$key]; |
||
| 468 | } |
||
| 469 | |||
| 470 | if (!ctype_lower($value)) { |
||
| 471 | $replace = (string) preg_replace('/\s+/', '', $value); |
||
| 472 | |||
| 473 | $value = strtolower((string) preg_replace( |
||
| 474 | '/(.)(?=[A-Z])/', |
||
| 475 | '$1' . $separator, |
||
| 476 | $replace |
||
| 477 | )); |
||
| 478 | } |
||
| 479 | |||
| 480 | return self::$snakeCache[$key] = $value; |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Convert a value to studly caps case. |
||
| 485 | * @param string $value |
||
| 486 | * @return string |
||
| 487 | */ |
||
| 488 | public static function studly(string $value): string |
||
| 489 | { |
||
| 490 | $key = $value; |
||
| 491 | if (isset(self::$studlyCache[$key])) { |
||
| 492 | return self::$studlyCache[$key]; |
||
| 493 | } |
||
| 494 | |||
| 495 | $val = ucwords(str_replace(['-', '_'], ' ', $value)); |
||
| 496 | |||
| 497 | return self::$studlyCache[$key] = str_replace(' ', '', $val); |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Returns the portion of string specified by the start and |
||
| 502 | * length parameters. |
||
| 503 | * |
||
| 504 | * @param string $value |
||
| 505 | * @param int $start |
||
| 506 | * @param int|null $length |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | public static function substr(string $value, int $start = 0, ?int $length = null): string |
||
| 510 | { |
||
| 511 | return mb_substr($value, $start, $length, 'UTF-8'); |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Make a string's first character to upper case. |
||
| 516 | * @param string $value |
||
| 517 | * @return string |
||
| 518 | */ |
||
| 519 | public static function ucfirst(string $value): string |
||
| 520 | { |
||
| 521 | return static::upper( |
||
| 522 | static::substr($value, 0, 1) |
||
| 523 | ) . static::substr($value, 1); |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Split the string by length part |
||
| 528 | * @param string $value |
||
| 529 | * @param int $length |
||
| 530 | * @return array<int, string> |
||
| 531 | */ |
||
| 532 | public static function split(string $value, int $length = 1): array |
||
| 533 | { |
||
| 534 | if ($length < 1) { |
||
| 535 | return []; |
||
| 536 | } |
||
| 537 | |||
| 538 | if (self::isAscii($value)) { |
||
| 539 | $res = str_split($value, $length); |
||
| 540 | if ($res === false) { |
||
| 541 | return []; |
||
| 542 | } |
||
| 543 | |||
| 544 | return $res; |
||
| 545 | } |
||
| 546 | |||
| 547 | if (mb_strlen($value) <= $length) { |
||
| 548 | return [$value]; |
||
| 549 | } |
||
| 550 | $matches = []; |
||
| 551 | preg_match_all( |
||
| 552 | '/.{' . $length . '}|[^\x00]{1,' . $length . '}$/us', |
||
| 553 | $value, |
||
| 554 | $matches |
||
| 555 | ); |
||
| 556 | |||
| 557 | return $matches[0]; |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Check whether the given string contains only ASCII chars |
||
| 562 | * @param string $value |
||
| 563 | * @return bool |
||
| 564 | */ |
||
| 565 | public static function isAscii(string $value): bool |
||
| 566 | { |
||
| 567 | return (bool)!preg_match('/[^\x00-\x7F]/S', $value); |
||
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Put string to lower case |
||
| 572 | * @param string $value |
||
| 573 | * @return string |
||
| 574 | */ |
||
| 575 | public static function lower(string $value): string |
||
| 578 | } |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Put string to upper case |
||
| 582 | * @param string $value |
||
| 583 | * @return string |
||
| 584 | */ |
||
| 585 | public static function upper(string $value): string |
||
| 586 | { |
||
| 587 | return mb_strtoupper($value, 'UTF-8'); |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Return the unique ID |
||
| 592 | * @param int $length |
||
| 593 | * |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | public static function uniqId(int $length = 13): string |
||
| 597 | { |
||
| 598 | $bytes = random_bytes((int) ceil($length / 2)); |
||
| 599 | |||
| 600 | return (string)substr(bin2hex($bytes), 0, $length); |
||
| 601 | } |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Generate random string value |
||
| 605 | * @param int $length |
||
| 606 | * @return string |
||
| 607 | */ |
||
| 608 | public static function random(int $length = 16): string |
||
| 609 | { |
||
| 610 | $string = ''; |
||
| 611 | while (($len = strlen($string)) < $length) { |
||
| 612 | $size = $length - $len; |
||
| 613 | $bytes = random_bytes($size); |
||
| 614 | |||
| 615 | $string .= substr( |
||
| 616 | str_replace(['/', '+', '='], '', base64_encode($bytes)), |
||
| 617 | 0, |
||
| 618 | $size |
||
| 619 | ); |
||
| 620 | } |
||
| 621 | |||
| 622 | return $string; |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Generates a random string of a given type and length. Possible |
||
| 627 | * values for the first argument ($type) are: |
||
| 628 | * - alnum - alpha-numeric characters (including capitals) |
||
| 629 | * - alpha - alphabetical characters (including capitals) |
||
| 630 | * - hexdec - hexadecimal characters, 0-9 plus a-f |
||
| 631 | * - numeric - digit characters, 0-9 |
||
| 632 | * - nozero - digit characters, 1-9 |
||
| 633 | * - distinct - clearly distinct alpha-numeric characters. |
||
| 634 | * @param string $type |
||
| 635 | * @param int $length |
||
| 636 | * @return string |
||
| 637 | */ |
||
| 638 | public static function randomString(string $type = 'alnum', int $length = 8): string |
||
| 639 | { |
||
| 640 | $utf8 = false; |
||
| 641 | |||
| 642 | switch ($type) { |
||
| 643 | case 'alnum': |
||
| 644 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||
| 645 | break; |
||
| 646 | case 'alpha': |
||
| 647 | $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||
| 648 | break; |
||
| 649 | case 'lowalnum': |
||
| 650 | $pool = '0123456789abcdefghijklmnopqrstuvwxyz'; |
||
| 651 | break; |
||
| 652 | case 'hexdec': |
||
| 653 | $pool = '0123456789abcdef'; |
||
| 654 | break; |
||
| 655 | case 'numeric': |
||
| 656 | $pool = '0123456789'; |
||
| 657 | break; |
||
| 658 | case 'nozero': |
||
| 659 | $pool = '123456789'; |
||
| 660 | break; |
||
| 661 | case 'distinct': |
||
| 662 | $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ'; |
||
| 663 | break; |
||
| 664 | default: |
||
| 665 | $pool = (string)$type; |
||
| 666 | $utf8 = !self::isAscii($pool); |
||
| 667 | break; |
||
| 668 | } |
||
| 669 | |||
| 670 | // Split the pool into an array of characters |
||
| 671 | $pool = $utf8 ? self::split($pool, 1) : str_split($pool, 1); |
||
| 672 | // Largest pool key |
||
| 673 | $max = count($pool) - 1; |
||
| 674 | |||
| 675 | $str = ''; |
||
| 676 | for ($i = 0; $i < $length; $i++) { |
||
| 677 | // Select a random character from the pool and add it to the string |
||
| 678 | $str .= $pool[random_int(0, $max)]; |
||
| 679 | } |
||
| 680 | |||
| 681 | // Make sure alnum strings contain at least one letter and one digit |
||
| 682 | if ($type === 'alnum' && $length > 1) { |
||
| 683 | if (ctype_alpha($str)) { |
||
| 684 | // Add a random digit |
||
| 685 | $str[random_int(0, $length - 1)] = chr(random_int(48, 57)); |
||
| 686 | } elseif (ctype_digit($str)) { |
||
| 687 | // Add a random letter |
||
| 688 | $str[random_int(0, $length - 1)] = chr(random_int(65, 90)); |
||
| 689 | } |
||
| 690 | } |
||
| 691 | |||
| 692 | return $str; |
||
| 693 | } |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Create a simple random token-string |
||
| 697 | * @param int $length |
||
| 698 | * @param string $salt |
||
| 699 | * @return string |
||
| 700 | */ |
||
| 701 | public static function randomToken(int $length = 24, string $salt = ''): string |
||
| 702 | { |
||
| 703 | $string = ''; |
||
| 704 | $chars = '0456789abc1def2ghi3jkl'; |
||
| 705 | $maxVal = strlen($chars) - 1; |
||
| 706 | |||
| 707 | for ($i = 0; $i < $length; ++$i) { |
||
| 708 | $string .= $chars[random_int(0, $maxVal)]; |
||
| 709 | } |
||
| 710 | |||
| 711 | return md5($string . $salt); |
||
| 712 | } |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Return the ASCII replacement |
||
| 716 | * @return array<string, array<string>> |
||
| 717 | */ |
||
| 718 | private static function getChars(): array |
||
| 1229 | ], |
||
| 1230 | ]; |
||
| 1231 | } |
||
| 1232 | } |
||
| 1233 |