| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use Illuminate\Support\Traits\Macroable; |
| 7 |
use League\CommonMark\Environment\Environment; |
| 8 |
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension; |
| 9 |
use League\CommonMark\Extension\InlinesOnly\InlinesOnlyExtension; |
| 10 |
use League\CommonMark\GithubFlavoredMarkdownConverter; |
| 11 |
use League\CommonMark\MarkdownConverter; |
| 12 |
use Ramsey\Uuid\Codec\TimestampFirstCombCodec; |
| 13 |
use Ramsey\Uuid\Exception\InvalidUuidStringException; |
| 14 |
use Ramsey\Uuid\Generator\CombGenerator; |
| 15 |
use Ramsey\Uuid\Rfc4122\FieldsInterface; |
| 16 |
use Ramsey\Uuid\Uuid; |
| 17 |
use Ramsey\Uuid\UuidFactory; |
| 18 |
use Symfony\Component\Uid\Ulid; |
| 19 |
use Throwable; |
| 20 |
use Traversable; |
| 21 |
use voku\helper\ASCII; |
| 22 |
|
| 23 |
class Str |
| 24 |
{ |
| 25 |
use Macroable; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
const INVISIBLE_CHARACTERS = '\x{0009}\x{0020}\x{00A0}\x{00AD}\x{034F}\x{061C}\x{115F}\x{1160}\x{17B4}\x{17B5}\x{180E}\x{2000}\x{2001}\x{2002}\x{2003}\x{2004}\x{2005}\x{2006}\x{2007}\x{2008}\x{2009}\x{200A}\x{200B}\x{200C}\x{200D}\x{200E}\x{200F}\x{202F}\x{205F}\x{2060}\x{2061}\x{2062}\x{2063}\x{2064}\x{2065}\x{206A}\x{206B}\x{206C}\x{206D}\x{206E}\x{206F}\x{3000}\x{2800}\x{3164}\x{FEFF}\x{FFA0}\x{1D159}\x{1D173}\x{1D174}\x{1D175}\x{1D176}\x{1D177}\x{1D178}\x{1D179}\x{1D17A}\x{E0020}'; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
protected static $snakeCache = []; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
protected static $camelCache = []; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
protected static $studlyCache = []; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
protected static $uuidFactory; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
protected static $ulidFactory; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
protected static $randomStringFactory; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static function of($string) |
| 83 |
{ |
| 84 |
return new Stringable($string); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public static function after($subject, $search) |
| 95 |
{ |
| 96 |
return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0]; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public static function afterLast($subject, $search) |
| 107 |
{ |
| 108 |
if ($search === '') { |
| 109 |
return $subject; |
| 110 |
} |
| 111 |
|
| 112 |
$position = mb_strrpos($subject, $search); |
| 113 |
|
| 114 |
if ($position === false) { |
| 115 |
return $subject; |
| 116 |
} |
| 117 |
|
| 118 |
return static::substr($subject, $position + static::length($search)); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public static function ascii($value, $language = 'en') |
| 129 |
{ |
| 130 |
return ASCII::to_ascii((string) $value, $language, replace_single_chars_only: false); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public static function transliterate($string, $unknown = '?', $strict = false) |
| 142 |
{ |
| 143 |
return ASCII::to_transliterate($string, $unknown, $strict); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public static function before($subject, $search) |
| 154 |
{ |
| 155 |
if ($search === '') { |
| 156 |
return $subject; |
| 157 |
} |
| 158 |
|
| 159 |
$result = strstr($subject, (string) $search, true); |
| 160 |
|
| 161 |
return $result === false ? $subject : $result; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public static function beforeLast($subject, $search) |
| 172 |
{ |
| 173 |
if ($search === '') { |
| 174 |
return $subject; |
| 175 |
} |
| 176 |
|
| 177 |
$pos = mb_strrpos($subject, $search); |
| 178 |
|
| 179 |
if ($pos === false) { |
| 180 |
return $subject; |
| 181 |
} |
| 182 |
|
| 183 |
return static::substr($subject, 0, $pos); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public static function between($subject, $from, $to) |
| 195 |
{ |
| 196 |
if ($from === '' || $to === '') { |
| 197 |
return $subject; |
| 198 |
} |
| 199 |
|
| 200 |
return static::beforeLast(static::after($subject, $from), $to); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public static function betweenFirst($subject, $from, $to) |
| 212 |
{ |
| 213 |
if ($from === '' || $to === '') { |
| 214 |
return $subject; |
| 215 |
} |
| 216 |
|
| 217 |
return static::before(static::after($subject, $from), $to); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public static function camel($value) |
| 227 |
{ |
| 228 |
if (isset(static::$camelCache[$value])) { |
| 229 |
return static::$camelCache[$value]; |
| 230 |
} |
| 231 |
|
| 232 |
return static::$camelCache[$value] = lcfirst(static::studly($value)); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public static function charAt($subject, $index) |
| 243 |
{ |
| 244 |
$length = mb_strlen($subject); |
| 245 |
|
| 246 |
if ($index < 0 ? $index < -$length : $index > $length - 1) { |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
return mb_substr($subject, $index, 1); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public static function chopStart($subject, $needle) |
| 261 |
{ |
| 262 |
foreach ((array) $needle as $n) { |
| 263 |
if ($n !== '' && str_starts_with($subject, $n)) { |
| 264 |
return mb_substr($subject, mb_strlen($n)); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
return $subject; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
public static function chopEnd($subject, $needle) |
| 279 |
{ |
| 280 |
foreach ((array) $needle as $n) { |
| 281 |
if ($n !== '' && str_ends_with($subject, $n)) { |
| 282 |
return mb_substr($subject, 0, -mb_strlen($n)); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
return $subject; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public static function contains($haystack, $needles, $ignoreCase = false) |
| 298 |
{ |
| 299 |
if (is_null($haystack)) { |
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
if ($ignoreCase) { |
| 304 |
$haystack = mb_strtolower($haystack); |
| 305 |
} |
| 306 |
|
| 307 |
if (! is_iterable($needles)) { |
| 308 |
$needles = (array) $needles; |
| 309 |
} |
| 310 |
|
| 311 |
foreach ($needles as $needle) { |
| 312 |
if ($ignoreCase) { |
| 313 |
$needle = mb_strtolower($needle); |
| 314 |
} |
| 315 |
|
| 316 |
if ($needle !== '' && str_contains($haystack, $needle)) { |
| 317 |
return true; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public static function containsAll($haystack, $needles, $ignoreCase = false) |
| 333 |
{ |
| 334 |
foreach ($needles as $needle) { |
| 335 |
if (! static::contains($haystack, $needle, $ignoreCase)) { |
| 336 |
return false; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
return true; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public static function doesntContain($haystack, $needles, $ignoreCase = false) |
| 352 |
{ |
| 353 |
return ! static::contains($haystack, $needles, $ignoreCase); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public static function convertCase(string $string, int $mode = MB_CASE_FOLD, ?string $encoding = 'UTF-8') |
| 365 |
{ |
| 366 |
return mb_convert_case($string, $mode, $encoding); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public static function deduplicate(string $string, array|string $characters = ' ') |
| 377 |
{ |
| 378 |
if (is_string($characters)) { |
| 379 |
return preg_replace('/'.preg_quote($characters, '/').'+/u', $characters, $string); |
| 380 |
} |
| 381 |
|
| 382 |
return array_reduce( |
| 383 |
$characters, |
| 384 |
fn ($carry, $character) => preg_replace('/'.preg_quote($character, '/').'+/u', $character, $carry), |
| 385 |
$string |
| 386 |
); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
public static function endsWith($haystack, $needles) |
| 397 |
{ |
| 398 |
if (is_null($haystack)) { |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
if (! is_iterable($needles)) { |
| 403 |
$needles = (array) $needles; |
| 404 |
} |
| 405 |
|
| 406 |
foreach ($needles as $needle) { |
| 407 |
if ((string) $needle !== '' && str_ends_with($haystack, $needle)) { |
| 408 |
return true; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
return false; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
public static function doesntEndWith($haystack, $needles) |
| 423 |
{ |
| 424 |
return ! static::endsWith($haystack, $needles); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
public static function excerpt($text, $phrase = '', $options = []) |
| 436 |
{ |
| 437 |
$radius = $options['radius'] ?? 100; |
| 438 |
$omission = $options['omission'] ?? '...'; |
| 439 |
|
| 440 |
preg_match('/^(.*?)('.preg_quote((string) $phrase, '/').')(.*)$/iu', (string) $text, $matches); |
| 441 |
|
| 442 |
if (empty($matches)) { |
| 443 |
return null; |
| 444 |
} |
| 445 |
|
| 446 |
$start = ltrim($matches[1]); |
| 447 |
|
| 448 |
$start = Str::of(mb_substr($start, max(mb_strlen($start, 'UTF-8') - $radius, 0), $radius, 'UTF-8'))->ltrim()->unless( |
| 449 |
fn ($startWithRadius) => $startWithRadius->exactly($start), |
| 450 |
fn ($startWithRadius) => $startWithRadius->prepend($omission), |
| 451 |
); |
| 452 |
|
| 453 |
$end = rtrim($matches[3]); |
| 454 |
|
| 455 |
$end = Str::of(mb_substr($end, 0, $radius, 'UTF-8'))->rtrim()->unless( |
| 456 |
fn ($endWithRadius) => $endWithRadius->exactly($end), |
| 457 |
fn ($endWithRadius) => $endWithRadius->append($omission), |
| 458 |
); |
| 459 |
|
| 460 |
return $start->append($matches[2], $end)->toString(); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public static function finish($value, $cap) |
| 471 |
{ |
| 472 |
$quoted = preg_quote($cap, '/'); |
| 473 |
|
| 474 |
return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public static function wrap($value, $before, $after = null) |
| 486 |
{ |
| 487 |
return $before.$value.($after ?? $before); |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
public static function unwrap($value, $before, $after = null) |
| 499 |
{ |
| 500 |
if (static::startsWith($value, $before)) { |
| 501 |
$value = static::substr($value, static::length($before)); |
| 502 |
} |
| 503 |
|
| 504 |
if (static::endsWith($value, $after ??= $before)) { |
| 505 |
$value = static::substr($value, 0, -static::length($after)); |
| 506 |
} |
| 507 |
|
| 508 |
return $value; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
public static function is($pattern, $value, $ignoreCase = false) |
| 520 |
{ |
| 521 |
$value = (string) $value; |
| 522 |
|
| 523 |
if (! is_iterable($pattern)) { |
| 524 |
$pattern = [$pattern]; |
| 525 |
} |
| 526 |
|
| 527 |
foreach ($pattern as $pattern) { |
| 528 |
$pattern = (string) $pattern; |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
if ($pattern === '*' || $pattern === $value) { |
| 534 |
return true; |
| 535 |
} |
| 536 |
|
| 537 |
if ($ignoreCase && mb_strtolower($pattern) === mb_strtolower($value)) { |
| 538 |
return true; |
| 539 |
} |
| 540 |
|
| 541 |
$pattern = preg_quote($pattern, '#'); |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
$pattern = str_replace('\*', '.*', $pattern); |
| 547 |
|
| 548 |
if (preg_match('#^'.$pattern.'\z#'.($ignoreCase ? 'isu' : 'su'), $value) === 1) { |
| 549 |
return true; |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
return false; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
public static function isAscii($value) |
| 563 |
{ |
| 564 |
return ASCII::is_ascii((string) $value); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
public static function isJson($value) |
| 576 |
{ |
| 577 |
if (! is_string($value)) { |
| 578 |
return false; |
| 579 |
} |
| 580 |
|
| 581 |
return json_validate($value, 512); |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
public static function isUrl($value, array $protocols = []) |
| 594 |
{ |
| 595 |
if (! is_string($value)) { |
| 596 |
return false; |
| 597 |
} |
| 598 |
|
| 599 |
$protocolList = empty($protocols) |
| 600 |
? 'aaa|aaas|about|acap|acct|acd|acr|adiumxtra|adt|afp|afs|aim|amss|android|appdata|apt|ark|attachment|aw|barion|beshare|bitcoin|bitcoincash|blob|bolo|browserext|calculator|callto|cap|cast|casts|chrome|chrome-extension|cid|coap|coap\+tcp|coap\+ws|coaps|coaps\+tcp|coaps\+ws|com-eventbrite-attendee|content|conti|crid|cvs|dab|data|dav|diaspora|dict|did|dis|dlna-playcontainer|dlna-playsingle|dns|dntp|dpp|drm|drop|dtn|dvb|ed2k|elsi|example|facetime|fax|feed|feedready|file|filesystem|finger|first-run-pen-experience|fish|fm|ftp|fuchsia-pkg|geo|gg|git|gizmoproject|go|gopher|graph|gtalk|h323|ham|hcap|hcp|http|https|hxxp|hxxps|hydrazone|iax|icap|icon|im|imap|info|iotdisco|ipn|ipp|ipps|irc|irc6|ircs|iris|iris\.beep|iris\.lwz|iris\.xpc|iris\.xpcs|isostore|itms|jabber|jar|jms|keyparc|lastfm|ldap|ldaps|leaptofrogans|lorawan|lvlt|magnet|mailserver|mailto|maps|market|message|mid|mms|modem|mongodb|moz|ms-access|ms-browser-extension|ms-calculator|ms-drive-to|ms-enrollment|ms-excel|ms-eyecontrolspeech|ms-gamebarservices|ms-gamingoverlay|ms-getoffice|ms-help|ms-infopath|ms-inputapp|ms-lockscreencomponent-config|ms-media-stream-id|ms-mixedrealitycapture|ms-mobileplans|ms-officeapp|ms-people|ms-project|ms-powerpoint|ms-publisher|ms-restoretabcompanion|ms-screenclip|ms-screensketch|ms-search|ms-search-repair|ms-secondary-screen-controller|ms-secondary-screen-setup|ms-settings|ms-settings-airplanemode|ms-settings-bluetooth|ms-settings-camera|ms-settings-cellular|ms-settings-cloudstorage|ms-settings-connectabledevices|ms-settings-displays-topology|ms-settings-emailandaccounts|ms-settings-language|ms-settings-location|ms-settings-lock|ms-settings-nfctransactions|ms-settings-notifications|ms-settings-power|ms-settings-privacy|ms-settings-proximity|ms-settings-screenrotation|ms-settings-wifi|ms-settings-workplace|ms-spd|ms-sttoverlay|ms-transit-to|ms-useractivityset|ms-virtualtouchpad|ms-visio|ms-walk-to|ms-whiteboard|ms-whiteboard-cmd|ms-word|msnim|msrp|msrps|mss|mtqp|mumble|mupdate|mvn|news|nfs|ni|nih|nntp|notes|ocf|oid|onenote|onenote-cmd|opaquelocktoken|openpgp4fpr|pack|palm|paparazzi|payto|pkcs11|platform|pop|pres|prospero|proxy|pwid|psyc|pttp|qb|query|redis|rediss|reload|res|resource|rmi|rsync|rtmfp|rtmp|rtsp|rtsps|rtspu|s3|secondlife|service|session|sftp|sgn|shttp|sieve|simpleledger|sip|sips|skype|smb|sms|smtp|snews|snmp|soap\.beep|soap\.beeps|soldat|spiffe|spotify|ssh|steam|stun|stuns|submit|svn|tag|teamspeak|tel|teliaeid|telnet|tftp|tg|things|thismessage|tip|tn3270|tool|ts3server|turn|turns|tv|udp|unreal|urn|ut2004|v-event|vemmi|ventrilo|videotex|vnc|view-source|wais|webcal|wpid|ws|wss|wtai|wyciwyg|xcon|xcon-userid|xfire|xmlrpc\.beep|xmlrpc\.beeps|xmpp|xri|ymsgr|z39\.50|z39\.50r|z39\.50s' |
| 601 |
: implode('|', $protocols); |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
$pattern = '~^ |
| 609 |
(LARAVEL_PROTOCOLS):// # protocol |
| 610 |
(((?:[\_\.\pL\pN-]|%[0-9A-Fa-f]{2})+:)?((?:[\_\.\pL\pN-]|%[0-9A-Fa-f]{2})+)@)? # basic auth |
| 611 |
( |
| 612 |
(?: |
| 613 |
(?: |
| 614 |
(?:[\pL\pN\pS\pM\-\_]++\.)+ |
| 615 |
(?: |
| 616 |
(?:xn--[a-z0-9-]++) # punycode in tld |
| 617 |
| |
| 618 |
(?:[\pL\pN\pM]++) # no punycode in tld |
| 619 |
) |
| 620 |
) # a multi-level domain name |
| 621 |
| |
| 622 |
[a-z0-9\-\_]++ # a single-level domain name |
| 623 |
)\.? |
| 624 |
| # or |
| 625 |
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # an IP address |
| 626 |
| # or |
| 627 |
\[ |
| 628 |
(?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,1}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){3})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,2}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){2})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,3}(?:(?:[0-9a-f]{1,4})))?::(?:(?:[0-9a-f]{1,4})):)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,4}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::)))) |
| 629 |
\] # an IPv6 address |
| 630 |
) |
| 631 |
(:[0-9]+)? # a port (optional) |
| 632 |
(?:/ (?:[\pL\pN\-._\~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})* )* # a path |
| 633 |
(?:\? (?:[\pL\pN\-._\~!$&\'\[\]()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )? # a query (optional) |
| 634 |
(?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )? # a fragment (optional) |
| 635 |
$~ixu'; |
| 636 |
|
| 637 |
return preg_match(str_replace('LARAVEL_PROTOCOLS', $protocolList, $pattern), $value) > 0; |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
public static function isUuid($value, $version = null) |
| 650 |
{ |
| 651 |
if (! is_string($value)) { |
| 652 |
return false; |
| 653 |
} |
| 654 |
|
| 655 |
if ($version === null) { |
| 656 |
return preg_match('/^[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}$/D', $value) > 0; |
| 657 |
} |
| 658 |
|
| 659 |
$factory = new UuidFactory; |
| 660 |
|
| 661 |
try { |
| 662 |
$factoryUuid = $factory->fromString($value); |
| 663 |
} catch (InvalidUuidStringException) { |
| 664 |
return false; |
| 665 |
} |
| 666 |
|
| 667 |
$fields = $factoryUuid->getFields(); |
| 668 |
|
| 669 |
if (! ($fields instanceof FieldsInterface)) { |
| 670 |
return false; |
| 671 |
} |
| 672 |
|
| 673 |
if ($version === 0 || $version === 'nil') { |
| 674 |
return $fields->isNil(); |
| 675 |
} |
| 676 |
|
| 677 |
if ($version === 'max') { |
| 678 |
return $fields->isMax(); |
| 679 |
} |
| 680 |
|
| 681 |
return $fields->getVersion() === $version; |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
public static function isUlid($value) |
| 693 |
{ |
| 694 |
if (! is_string($value)) { |
| 695 |
return false; |
| 696 |
} |
| 697 |
|
| 698 |
return Ulid::isValid($value); |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
public static function kebab($value) |
| 708 |
{ |
| 709 |
return static::snake($value, '-'); |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
public static function length($value, $encoding = null) |
| 720 |
{ |
| 721 |
return mb_strlen($value, $encoding); |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
public static function limit($value, $limit = 100, $end = '...', $preserveWords = false) |
| 734 |
{ |
| 735 |
if (mb_strwidth($value, 'UTF-8') <= $limit) { |
| 736 |
return $value; |
| 737 |
} |
| 738 |
|
| 739 |
if (! $preserveWords) { |
| 740 |
return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end; |
| 741 |
} |
| 742 |
|
| 743 |
$value = trim(preg_replace('/[\n\r]+/', ' ', strip_tags($value))); |
| 744 |
|
| 745 |
$trimmed = rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')); |
| 746 |
|
| 747 |
if (mb_substr($value, $limit, 1, 'UTF-8') === ' ') { |
| 748 |
return $trimmed.$end; |
| 749 |
} |
| 750 |
|
| 751 |
return preg_replace("/(.*)\s.*/", '$1', $trimmed).$end; |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
public static function lower($value) |
| 761 |
{ |
| 762 |
return mb_strtolower($value, 'UTF-8'); |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
public static function words($value, $words = 100, $end = '...') |
| 774 |
{ |
| 775 |
preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches); |
| 776 |
|
| 777 |
if (! isset($matches[0]) || static::length($value) === static::length($matches[0])) { |
| 778 |
return $value; |
| 779 |
} |
| 780 |
|
| 781 |
return rtrim($matches[0]).$end; |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
public static function markdown($string, array $options = [], array $extensions = []) |
| 793 |
{ |
| 794 |
$converter = new GithubFlavoredMarkdownConverter($options); |
| 795 |
|
| 796 |
$environment = $converter->getEnvironment(); |
| 797 |
|
| 798 |
foreach ($extensions as $extension) { |
| 799 |
$environment->addExtension($extension); |
| 800 |
} |
| 801 |
|
| 802 |
return (string) $converter->convert($string); |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
public static function inlineMarkdown($string, array $options = [], array $extensions = []) |
| 814 |
{ |
| 815 |
$environment = new Environment($options); |
| 816 |
|
| 817 |
$environment->addExtension(new GithubFlavoredMarkdownExtension()); |
| 818 |
$environment->addExtension(new InlinesOnlyExtension()); |
| 819 |
|
| 820 |
foreach ($extensions as $extension) { |
| 821 |
$environment->addExtension($extension); |
| 822 |
} |
| 823 |
|
| 824 |
$converter = new MarkdownConverter($environment); |
| 825 |
|
| 826 |
return (string) $converter->convert($string); |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
public static function mask($string, $character, $index, $length = null, $encoding = 'UTF-8') |
| 840 |
{ |
| 841 |
if ($character === '') { |
| 842 |
return $string; |
| 843 |
} |
| 844 |
|
| 845 |
$segment = mb_substr($string, $index, $length, $encoding); |
| 846 |
|
| 847 |
if ($segment === '') { |
| 848 |
return $string; |
| 849 |
} |
| 850 |
|
| 851 |
$strlen = mb_strlen($string, $encoding); |
| 852 |
$startIndex = $index; |
| 853 |
|
| 854 |
if ($index < 0) { |
| 855 |
$startIndex = $index < -$strlen ? 0 : $strlen + $index; |
| 856 |
} |
| 857 |
|
| 858 |
$start = mb_substr($string, 0, $startIndex, $encoding); |
| 859 |
$segmentLen = mb_strlen($segment, $encoding); |
| 860 |
$end = mb_substr($string, $startIndex + $segmentLen); |
| 861 |
|
| 862 |
return $start.str_repeat(mb_substr($character, 0, 1, $encoding), $segmentLen).$end; |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
public static function match($pattern, $subject) |
| 873 |
{ |
| 874 |
preg_match($pattern, $subject, $matches); |
| 875 |
|
| 876 |
if (! $matches) { |
| 877 |
return ''; |
| 878 |
} |
| 879 |
|
| 880 |
return $matches[1] ?? $matches[0]; |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
public static function isMatch($pattern, $value) |
| 891 |
{ |
| 892 |
$value = (string) $value; |
| 893 |
|
| 894 |
if (! is_iterable($pattern)) { |
| 895 |
$pattern = [$pattern]; |
| 896 |
} |
| 897 |
|
| 898 |
foreach ($pattern as $pattern) { |
| 899 |
$pattern = (string) $pattern; |
| 900 |
|
| 901 |
if (preg_match($pattern, $value) === 1) { |
| 902 |
return true; |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
return false; |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
public static function matchAll($pattern, $subject) |
| 917 |
{ |
| 918 |
preg_match_all($pattern, $subject, $matches); |
| 919 |
|
| 920 |
if (empty($matches[0])) { |
| 921 |
return new Collection; |
| 922 |
} |
| 923 |
|
| 924 |
return new Collection($matches[1] ?? $matches[0]); |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
public static function numbers($value) |
| 934 |
{ |
| 935 |
return preg_replace('/[^0-9]/', '', $value); |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
public static function padBoth($value, $length, $pad = ' ') |
| 947 |
{ |
| 948 |
return mb_str_pad($value, $length, $pad, STR_PAD_BOTH); |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
public static function padLeft($value, $length, $pad = ' ') |
| 960 |
{ |
| 961 |
return mb_str_pad($value, $length, $pad, STR_PAD_LEFT); |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
public static function padRight($value, $length, $pad = ' ') |
| 973 |
{ |
| 974 |
return mb_str_pad($value, $length, $pad, STR_PAD_RIGHT); |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
public static function parseCallback($callback, $default = null) |
| 985 |
{ |
| 986 |
if (static::contains($callback, "@anonymous\0")) { |
| 987 |
if (static::substrCount($callback, '@') > 1) { |
| 988 |
return [ |
| 989 |
static::beforeLast($callback, '@'), |
| 990 |
static::afterLast($callback, '@'), |
| 991 |
]; |
| 992 |
} |
| 993 |
|
| 994 |
return [$callback, $default]; |
| 995 |
} |
| 996 |
|
| 997 |
return static::contains($callback, '@') ? explode('@', $callback, 2) : [$callback, $default]; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
public static function plural($value, $count = 2, $prependCount = false) |
| 1009 |
{ |
| 1010 |
if (is_countable($count)) { |
| 1011 |
$count = count($count); |
| 1012 |
} |
| 1013 |
|
| 1014 |
return ($prependCount ? Number::format($count).' ' : '').Pluralizer::plural($value, $count); |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
public static function pluralStudly($value, $count = 2) |
| 1025 |
{ |
| 1026 |
$parts = preg_split('/(.)(?=[A-Z])/u', $value, -1, PREG_SPLIT_DELIM_CAPTURE); |
| 1027 |
|
| 1028 |
$lastWord = array_pop($parts); |
| 1029 |
|
| 1030 |
return implode('', $parts).self::plural($lastWord, $count); |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
public static function pluralPascal($value, $count = 2) |
| 1041 |
{ |
| 1042 |
return static::pluralStudly($value, $count); |
| 1043 |
} |
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
public static function password($length = 32, $letters = true, $numbers = true, $symbols = true, $spaces = false) |
| 1056 |
{ |
| 1057 |
$password = new Collection(); |
| 1058 |
|
| 1059 |
$options = (new Collection([ |
| 1060 |
'letters' => $letters === true ? [ |
| 1061 |
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', |
| 1062 |
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 1063 |
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
| 1064 |
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', |
| 1065 |
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
| 1066 |
] : null, |
| 1067 |
'numbers' => $numbers === true ? [ |
| 1068 |
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 1069 |
] : null, |
| 1070 |
'symbols' => $symbols === true ? [ |
| 1071 |
'~', '!', '#', '$', '%', '^', '&', '*', '(', ')', '-', |
| 1072 |
'_', '.', ',', '<', '>', '?', '/', '\\', '{', '}', '[', |
| 1073 |
']', '|', ':', ';', |
| 1074 |
] : null, |
| 1075 |
'spaces' => $spaces === true ? [' '] : null, |
| 1076 |
])) |
| 1077 |
->filter() |
| 1078 |
->each(fn ($c) => $password->push($c[random_int(0, count($c) - 1)])) |
| 1079 |
->flatten(); |
| 1080 |
|
| 1081 |
$length = $length - $password->count(); |
| 1082 |
|
| 1083 |
return $password->merge($options->pipe( |
| 1084 |
fn ($c) => Collection::times($length, fn () => $c[random_int(0, $c->count() - 1)]) |
| 1085 |
))->shuffle()->implode(''); |
| 1086 |
} |
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
public static function position($haystack, $needle, $offset = 0, $encoding = null) |
| 1098 |
{ |
| 1099 |
return mb_strpos($haystack, (string) $needle, $offset, $encoding); |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
public static function random($length = 16) |
| 1109 |
{ |
| 1110 |
return (static::$randomStringFactory ?? function ($length) { |
| 1111 |
$string = ''; |
| 1112 |
|
| 1113 |
while (($len = strlen($string)) < $length) { |
| 1114 |
$size = $length - $len; |
| 1115 |
|
| 1116 |
$bytesSize = (int) ceil($size / 3) * 3; |
| 1117 |
|
| 1118 |
$bytes = random_bytes($bytesSize); |
| 1119 |
|
| 1120 |
$string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size); |
| 1121 |
} |
| 1122 |
|
| 1123 |
return $string; |
| 1124 |
})($length); |
| 1125 |
} |
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
public static function createRandomStringsUsing(?callable $factory = null) |
| 1134 |
{ |
| 1135 |
static::$randomStringFactory = $factory; |
| 1136 |
} |
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
public static function createRandomStringsUsingSequence(array $sequence, $whenMissing = null) |
| 1146 |
{ |
| 1147 |
$next = 0; |
| 1148 |
|
| 1149 |
$whenMissing ??= function ($length) use (&$next) { |
| 1150 |
$factoryCache = static::$randomStringFactory; |
| 1151 |
|
| 1152 |
static::$randomStringFactory = null; |
| 1153 |
|
| 1154 |
$randomString = static::random($length); |
| 1155 |
|
| 1156 |
static::$randomStringFactory = $factoryCache; |
| 1157 |
|
| 1158 |
$next++; |
| 1159 |
|
| 1160 |
return $randomString; |
| 1161 |
}; |
| 1162 |
|
| 1163 |
static::createRandomStringsUsing(function ($length) use (&$next, $sequence, $whenMissing) { |
| 1164 |
if (array_key_exists($next, $sequence)) { |
| 1165 |
return $sequence[$next++]; |
| 1166 |
} |
| 1167 |
|
| 1168 |
return $whenMissing($length); |
| 1169 |
}); |
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
public static function createRandomStringsNormally() |
| 1178 |
{ |
| 1179 |
static::$randomStringFactory = null; |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
public static function repeat(string $string, int $times) |
| 1190 |
{ |
| 1191 |
return str_repeat($string, $times); |
| 1192 |
} |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
public static function replaceArray($search, $replace, $subject) |
| 1203 |
{ |
| 1204 |
if ($replace instanceof Traversable) { |
| 1205 |
$replace = Arr::from($replace); |
| 1206 |
} |
| 1207 |
|
| 1208 |
$segments = explode($search, $subject); |
| 1209 |
|
| 1210 |
$result = array_shift($segments); |
| 1211 |
|
| 1212 |
foreach ($segments as $segment) { |
| 1213 |
$result .= self::toStringOr(array_shift($replace) ?? $search, $search).$segment; |
| 1214 |
} |
| 1215 |
|
| 1216 |
return $result; |
| 1217 |
} |
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
private static function toStringOr($value, $fallback) |
| 1227 |
{ |
| 1228 |
try { |
| 1229 |
return (string) $value; |
| 1230 |
} catch (Throwable $e) { |
| 1231 |
return $fallback; |
| 1232 |
} |
| 1233 |
} |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
public static function replace($search, $replace, $subject, $caseSensitive = true) |
| 1245 |
{ |
| 1246 |
if ($search instanceof Traversable) { |
| 1247 |
$search = Arr::from($search); |
| 1248 |
} |
| 1249 |
|
| 1250 |
if ($replace instanceof Traversable) { |
| 1251 |
$replace = Arr::from($replace); |
| 1252 |
} |
| 1253 |
|
| 1254 |
if ($subject instanceof Traversable) { |
| 1255 |
$subject = Arr::from($subject); |
| 1256 |
} |
| 1257 |
|
| 1258 |
return $caseSensitive |
| 1259 |
? str_replace($search, $replace, $subject) |
| 1260 |
: str_ireplace($search, $replace, $subject); |
| 1261 |
} |
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
public static function replaceFirst($search, $replace, $subject) |
| 1272 |
{ |
| 1273 |
$search = (string) $search; |
| 1274 |
|
| 1275 |
if ($search === '') { |
| 1276 |
return $subject; |
| 1277 |
} |
| 1278 |
|
| 1279 |
$position = strpos($subject, $search); |
| 1280 |
|
| 1281 |
if ($position !== false) { |
| 1282 |
return substr_replace($subject, $replace, $position, strlen($search)); |
| 1283 |
} |
| 1284 |
|
| 1285 |
return $subject; |
| 1286 |
} |
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
public static function replaceStart($search, $replace, $subject) |
| 1297 |
{ |
| 1298 |
$search = (string) $search; |
| 1299 |
|
| 1300 |
if ($search === '') { |
| 1301 |
return $subject; |
| 1302 |
} |
| 1303 |
|
| 1304 |
if (static::startsWith($subject, $search)) { |
| 1305 |
return static::replaceFirst($search, $replace, $subject); |
| 1306 |
} |
| 1307 |
|
| 1308 |
return $subject; |
| 1309 |
} |
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
public static function replaceLast($search, $replace, $subject) |
| 1320 |
{ |
| 1321 |
$search = (string) $search; |
| 1322 |
|
| 1323 |
if ($search === '') { |
| 1324 |
return $subject; |
| 1325 |
} |
| 1326 |
|
| 1327 |
$position = strrpos($subject, $search); |
| 1328 |
|
| 1329 |
if ($position !== false) { |
| 1330 |
return substr_replace($subject, $replace, $position, strlen($search)); |
| 1331 |
} |
| 1332 |
|
| 1333 |
return $subject; |
| 1334 |
} |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
public static function replaceEnd($search, $replace, $subject) |
| 1345 |
{ |
| 1346 |
$search = (string) $search; |
| 1347 |
|
| 1348 |
if ($search === '') { |
| 1349 |
return $subject; |
| 1350 |
} |
| 1351 |
|
| 1352 |
if (static::endsWith($subject, $search)) { |
| 1353 |
return static::replaceLast($search, $replace, $subject); |
| 1354 |
} |
| 1355 |
|
| 1356 |
return $subject; |
| 1357 |
} |
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
public static function replaceMatches($pattern, $replace, $subject, $limit = -1) |
| 1369 |
{ |
| 1370 |
if ($replace instanceof Closure) { |
| 1371 |
return preg_replace_callback($pattern, $replace, $subject, $limit); |
| 1372 |
} |
| 1373 |
|
| 1374 |
return preg_replace($pattern, $replace, $subject, $limit); |
| 1375 |
} |
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
public static function remove($search, $subject, $caseSensitive = true) |
| 1386 |
{ |
| 1387 |
if ($search instanceof Traversable) { |
| 1388 |
$search = Arr::from($search); |
| 1389 |
} |
| 1390 |
|
| 1391 |
return $caseSensitive |
| 1392 |
? str_replace($search, '', $subject) |
| 1393 |
: str_ireplace($search, '', $subject); |
| 1394 |
} |
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
public static function reverse(string $value) |
| 1403 |
{ |
| 1404 |
return implode(array_reverse(mb_str_split($value))); |
| 1405 |
} |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
public static function start($value, $prefix) |
| 1415 |
{ |
| 1416 |
$quoted = preg_quote($prefix, '/'); |
| 1417 |
|
| 1418 |
return $prefix.preg_replace('/^(?:'.$quoted.')+/u', '', $value); |
| 1419 |
} |
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
public static function upper($value) |
| 1428 |
{ |
| 1429 |
return mb_strtoupper($value, 'UTF-8'); |
| 1430 |
} |
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
public static function title($value) |
| 1439 |
{ |
| 1440 |
return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); |
| 1441 |
} |
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
public static function headline($value) |
| 1450 |
{ |
| 1451 |
$parts = mb_split('\s+', $value); |
| 1452 |
|
| 1453 |
$parts = count($parts) > 1 |
| 1454 |
? array_map(static::title(...), $parts) |
| 1455 |
: array_map(static::title(...), static::ucsplit(implode('_', $parts))); |
| 1456 |
|
| 1457 |
$collapsed = static::replace(['-', '_', ' '], '_', implode('_', $parts)); |
| 1458 |
|
| 1459 |
return implode(' ', array_filter(explode('_', $collapsed))); |
| 1460 |
} |
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
public static function initials($value, $capitalize = false) |
| 1470 |
{ |
| 1471 |
$parts = mb_split("\s+", $value); |
| 1472 |
|
| 1473 |
$parts = array_map(fn ($part) => mb_substr($part, 0, 1), $parts); |
| 1474 |
|
| 1475 |
$initials = implode('', $parts); |
| 1476 |
|
| 1477 |
return $capitalize ? static::upper($initials) : $initials; |
| 1478 |
} |
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
public static function apa($value) |
| 1489 |
{ |
| 1490 |
if (trim($value) === '') { |
| 1491 |
return $value; |
| 1492 |
} |
| 1493 |
|
| 1494 |
$minorWords = [ |
| 1495 |
'and', 'as', 'but', 'for', 'if', 'nor', 'or', 'so', 'yet', 'a', 'an', |
| 1496 |
'the', 'at', 'by', 'in', 'of', 'off', 'on', 'per', 'to', 'up', 'via', |
| 1497 |
'et', 'ou', 'un', 'une', 'la', 'le', 'les', 'de', 'du', 'des', 'par', 'Ã ', |
| 1498 |
]; |
| 1499 |
|
| 1500 |
$endPunctuation = ['.', '!', '?', ':', '—', ',']; |
| 1501 |
|
| 1502 |
$words = mb_split('\s+', $value); |
| 1503 |
$wordCount = count($words); |
| 1504 |
|
| 1505 |
for ($i = 0; $i < $wordCount; $i++) { |
| 1506 |
$lowercaseWord = mb_strtolower($words[$i]); |
| 1507 |
|
| 1508 |
if (str_contains($lowercaseWord, '-')) { |
| 1509 |
$hyphenatedWords = explode('-', $lowercaseWord); |
| 1510 |
|
| 1511 |
$hyphenatedWords = array_map(function ($part) use ($minorWords) { |
| 1512 |
return (in_array($part, $minorWords) && mb_strlen($part) <= 3) |
| 1513 |
? $part |
| 1514 |
: mb_strtoupper(mb_substr($part, 0, 1)).mb_substr($part, 1); |
| 1515 |
}, $hyphenatedWords); |
| 1516 |
|
| 1517 |
$words[$i] = implode('-', $hyphenatedWords); |
| 1518 |
} else { |
| 1519 |
if (in_array($lowercaseWord, $minorWords) && |
| 1520 |
mb_strlen($lowercaseWord) <= 3 && |
| 1521 |
! ($i === 0 || in_array(mb_substr($words[$i - 1], -1), $endPunctuation))) { |
| 1522 |
$words[$i] = $lowercaseWord; |
| 1523 |
} else { |
| 1524 |
$words[$i] = mb_strtoupper(mb_substr($lowercaseWord, 0, 1)).mb_substr($lowercaseWord, 1); |
| 1525 |
} |
| 1526 |
} |
| 1527 |
} |
| 1528 |
|
| 1529 |
return implode(' ', $words); |
| 1530 |
} |
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
public static function singular($value) |
| 1539 |
{ |
| 1540 |
return Pluralizer::singular($value); |
| 1541 |
} |
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
public static function slug($title, $separator = '-', $language = 'en', $dictionary = ['@' => 'at']) |
| 1553 |
{ |
| 1554 |
$title = $language ? static::ascii($title, $language) : $title; |
| 1555 |
|
| 1556 |
|
| 1557 |
$flip = $separator === '-' ? '_' : '-'; |
| 1558 |
|
| 1559 |
$title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title); |
| 1560 |
|
| 1561 |
|
| 1562 |
foreach ($dictionary as $key => $value) { |
| 1563 |
$dictionary[$key] = $separator.$value.$separator; |
| 1564 |
} |
| 1565 |
|
| 1566 |
$title = str_replace(array_keys($dictionary), array_values($dictionary), $title); |
| 1567 |
|
| 1568 |
|
| 1569 |
$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', static::lower($title)); |
| 1570 |
|
| 1571 |
|
| 1572 |
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title); |
| 1573 |
|
| 1574 |
return trim($title, $separator); |
| 1575 |
} |
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
public static function snake($value, $delimiter = '_') |
| 1585 |
{ |
| 1586 |
$key = $value; |
| 1587 |
|
| 1588 |
if (isset(static::$snakeCache[$key][$delimiter])) { |
| 1589 |
return static::$snakeCache[$key][$delimiter]; |
| 1590 |
} |
| 1591 |
|
| 1592 |
if (! ctype_lower($value)) { |
| 1593 |
$value = preg_replace('/\s+/u', '', ucwords($value)); |
| 1594 |
|
| 1595 |
$value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value)); |
| 1596 |
} |
| 1597 |
|
| 1598 |
return static::$snakeCache[$key][$delimiter] = $value; |
| 1599 |
} |
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
public static function trim($value, $charlist = null) |
| 1609 |
{ |
| 1610 |
if ($charlist === null) { |
| 1611 |
$trimDefaultCharacters = " \n\r\t\v\0"; |
| 1612 |
|
| 1613 |
return preg_replace('~^[\s'.self::INVISIBLE_CHARACTERS.$trimDefaultCharacters.']+|[\s'.self::INVISIBLE_CHARACTERS.$trimDefaultCharacters.']+$~u', '', $value) ?? trim($value); |
| 1614 |
} |
| 1615 |
|
| 1616 |
return trim($value, $charlist); |
| 1617 |
} |
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
public static function ltrim($value, $charlist = null) |
| 1627 |
{ |
| 1628 |
if ($charlist === null) { |
| 1629 |
$ltrimDefaultCharacters = " \n\r\t\v\0"; |
| 1630 |
|
| 1631 |
return preg_replace('~^[\s'.self::INVISIBLE_CHARACTERS.$ltrimDefaultCharacters.']+~u', '', $value) ?? ltrim($value); |
| 1632 |
} |
| 1633 |
|
| 1634 |
return ltrim($value, $charlist); |
| 1635 |
} |
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
public static function rtrim($value, $charlist = null) |
| 1645 |
{ |
| 1646 |
if ($charlist === null) { |
| 1647 |
$rtrimDefaultCharacters = " \n\r\t\v\0"; |
| 1648 |
|
| 1649 |
return preg_replace('~[\s'.self::INVISIBLE_CHARACTERS.$rtrimDefaultCharacters.']+$~u', '', $value) ?? rtrim($value); |
| 1650 |
} |
| 1651 |
|
| 1652 |
return rtrim($value, $charlist); |
| 1653 |
} |
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
public static function squish($value) |
| 1662 |
{ |
| 1663 |
return preg_replace('~(\s|\x{3164}|\x{1160})+~u', ' ', static::trim($value)); |
| 1664 |
} |
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
public static function startsWith($haystack, $needles) |
| 1676 |
{ |
| 1677 |
if (is_null($haystack)) { |
| 1678 |
return false; |
| 1679 |
} |
| 1680 |
|
| 1681 |
if (! is_iterable($needles)) { |
| 1682 |
$needles = [$needles]; |
| 1683 |
} |
| 1684 |
|
| 1685 |
foreach ($needles as $needle) { |
| 1686 |
if ((string) $needle !== '' && str_starts_with($haystack, $needle)) { |
| 1687 |
return true; |
| 1688 |
} |
| 1689 |
} |
| 1690 |
|
| 1691 |
return false; |
| 1692 |
} |
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
public static function doesntStartWith($haystack, $needles) |
| 1704 |
{ |
| 1705 |
return ! static::startsWith($haystack, $needles); |
| 1706 |
} |
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
public static function studly($value) |
| 1715 |
{ |
| 1716 |
$key = $value; |
| 1717 |
|
| 1718 |
if (isset(static::$studlyCache[$key])) { |
| 1719 |
return static::$studlyCache[$key]; |
| 1720 |
} |
| 1721 |
|
| 1722 |
$words = mb_split('\s+', static::replace(['-', '_'], ' ', $value)); |
| 1723 |
|
| 1724 |
$studlyWords = array_map(fn ($word) => static::ucfirst($word), $words); |
| 1725 |
|
| 1726 |
return static::$studlyCache[$key] = implode($studlyWords); |
| 1727 |
} |
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
public static function pascal($value) |
| 1736 |
{ |
| 1737 |
return static::studly($value); |
| 1738 |
} |
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
public static function substr($string, $start, $length = null, $encoding = 'UTF-8') |
| 1750 |
{ |
| 1751 |
return mb_substr($string, $start, $length, $encoding); |
| 1752 |
} |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
public static function substrCount($haystack, $needle, $offset = 0, $length = null) |
| 1764 |
{ |
| 1765 |
if (! is_null($length)) { |
| 1766 |
return substr_count($haystack, $needle, $offset, $length); |
| 1767 |
} |
| 1768 |
|
| 1769 |
return substr_count($haystack, $needle, $offset); |
| 1770 |
} |
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
public static function substrReplace($string, $replace, $offset = 0, $length = null) |
| 1782 |
{ |
| 1783 |
if ($length === null) { |
| 1784 |
$length = static::length($string); |
| 1785 |
} |
| 1786 |
|
| 1787 |
return mb_substr($string, 0, $offset) |
| 1788 |
.$replace |
| 1789 |
.mb_substr(mb_substr($string, $offset), $length); |
| 1790 |
} |
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
public static function swap(array $map, $subject) |
| 1800 |
{ |
| 1801 |
return strtr($subject, $map); |
| 1802 |
} |
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
public static function take($string, int $limit): string |
| 1812 |
{ |
| 1813 |
if ($limit < 0) { |
| 1814 |
return static::substr($string, $limit); |
| 1815 |
} |
| 1816 |
|
| 1817 |
return static::substr($string, 0, $limit); |
| 1818 |
} |
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
public static function toBase64($string): string |
| 1827 |
{ |
| 1828 |
return base64_encode($string); |
| 1829 |
} |
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
public static function fromBase64($string, $strict = false) |
| 1839 |
{ |
| 1840 |
return base64_decode($string, $strict); |
| 1841 |
} |
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
public static function lcfirst($string) |
| 1850 |
{ |
| 1851 |
return static::lower(static::substr($string, 0, 1)).static::substr($string, 1); |
| 1852 |
} |
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
public static function ucfirst($string) |
| 1861 |
{ |
| 1862 |
return static::upper(static::substr($string, 0, 1)).static::substr($string, 1); |
| 1863 |
} |
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
public static function ucwords($string, $separators = " \t\r\n\f\v") |
| 1873 |
{ |
| 1874 |
$pattern = '/(^|['.preg_quote($separators, '/').'])(\p{Ll})/u'; |
| 1875 |
|
| 1876 |
return preg_replace_callback($pattern, function ($matches) { |
| 1877 |
return $matches[1].mb_strtoupper($matches[2]); |
| 1878 |
}, $string); |
| 1879 |
} |
| 1880 |
|
| 1881 |
|
| 1882 |
|
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
public static function ucsplit($string) |
| 1888 |
{ |
| 1889 |
return preg_split('/(?=\p{Lu})/u', $string, -1, PREG_SPLIT_NO_EMPTY); |
| 1890 |
} |
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
public static function wordCount($string, $characters = null) |
| 1900 |
{ |
| 1901 |
return str_word_count($string, 0, $characters); |
| 1902 |
} |
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
public static function wordWrap($string, $characters = 75, $break = "\n", $cutLongWords = false) |
| 1914 |
{ |
| 1915 |
return wordwrap($string, $characters, $break, $cutLongWords); |
| 1916 |
} |
| 1917 |
|
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
public static function uuid() |
| 1924 |
{ |
| 1925 |
return static::$uuidFactory |
| 1926 |
? call_user_func(static::$uuidFactory) |
| 1927 |
: Uuid::uuid4(); |
| 1928 |
} |
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
public static function uuid7($time = null) |
| 1937 |
{ |
| 1938 |
return static::$uuidFactory |
| 1939 |
? call_user_func(static::$uuidFactory) |
| 1940 |
: Uuid::uuid7($time); |
| 1941 |
} |
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
public static function orderedUuid() |
| 1949 |
{ |
| 1950 |
if (static::$uuidFactory) { |
| 1951 |
return call_user_func(static::$uuidFactory); |
| 1952 |
} |
| 1953 |
|
| 1954 |
$factory = new UuidFactory; |
| 1955 |
|
| 1956 |
$factory->setRandomGenerator(new CombGenerator( |
| 1957 |
$factory->getRandomGenerator(), |
| 1958 |
$factory->getNumberConverter() |
| 1959 |
)); |
| 1960 |
|
| 1961 |
$factory->setCodec(new TimestampFirstCombCodec( |
| 1962 |
$factory->getUuidBuilder() |
| 1963 |
)); |
| 1964 |
|
| 1965 |
return $factory->uuid4(); |
| 1966 |
} |
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
public static function createUuidsUsing(?callable $factory = null) |
| 1975 |
{ |
| 1976 |
static::$uuidFactory = $factory; |
| 1977 |
} |
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
public static function createUuidsUsingSequence(array $sequence, $whenMissing = null) |
| 1987 |
{ |
| 1988 |
$next = 0; |
| 1989 |
|
| 1990 |
$whenMissing ??= function () use (&$next) { |
| 1991 |
$factoryCache = static::$uuidFactory; |
| 1992 |
|
| 1993 |
static::$uuidFactory = null; |
| 1994 |
|
| 1995 |
$uuid = static::uuid(); |
| 1996 |
|
| 1997 |
static::$uuidFactory = $factoryCache; |
| 1998 |
|
| 1999 |
$next++; |
| 2000 |
|
| 2001 |
return $uuid; |
| 2002 |
}; |
| 2003 |
|
| 2004 |
static::createUuidsUsing(function () use (&$next, $sequence, $whenMissing) { |
| 2005 |
if (array_key_exists($next, $sequence)) { |
| 2006 |
return $sequence[$next++]; |
| 2007 |
} |
| 2008 |
|
| 2009 |
return $whenMissing(); |
| 2010 |
}); |
| 2011 |
} |
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
public static function freezeUuids(?Closure $callback = null) |
| 2020 |
{ |
| 2021 |
$uuid = Str::uuid(); |
| 2022 |
|
| 2023 |
Str::createUuidsUsing(fn () => $uuid); |
| 2024 |
|
| 2025 |
if ($callback !== null) { |
| 2026 |
try { |
| 2027 |
$callback($uuid); |
| 2028 |
} finally { |
| 2029 |
Str::createUuidsNormally(); |
| 2030 |
} |
| 2031 |
} |
| 2032 |
|
| 2033 |
return $uuid; |
| 2034 |
} |
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
public static function createUuidsNormally() |
| 2042 |
{ |
| 2043 |
static::$uuidFactory = null; |
| 2044 |
} |
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
|
| 2052 |
public static function ulid($time = null) |
| 2053 |
{ |
| 2054 |
if (static::$ulidFactory) { |
| 2055 |
return call_user_func(static::$ulidFactory); |
| 2056 |
} |
| 2057 |
|
| 2058 |
if ($time === null) { |
| 2059 |
return new Ulid(); |
| 2060 |
} |
| 2061 |
|
| 2062 |
return new Ulid(Ulid::generate($time)); |
| 2063 |
} |
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
|
| 2068 |
|
| 2069 |
|
| 2070 |
public static function createUlidsNormally() |
| 2071 |
{ |
| 2072 |
static::$ulidFactory = null; |
| 2073 |
} |
| 2074 |
|
| 2075 |
|
| 2076 |
|
| 2077 |
|
| 2078 |
|
| 2079 |
|
| 2080 |
|
| 2081 |
public static function createUlidsUsing(?callable $factory = null) |
| 2082 |
{ |
| 2083 |
static::$ulidFactory = $factory; |
| 2084 |
} |
| 2085 |
|
| 2086 |
|
| 2087 |
|
| 2088 |
|
| 2089 |
|
| 2090 |
|
| 2091 |
|
| 2092 |
|
| 2093 |
public static function createUlidsUsingSequence(array $sequence, $whenMissing = null) |
| 2094 |
{ |
| 2095 |
$next = 0; |
| 2096 |
|
| 2097 |
$whenMissing ??= function () use (&$next) { |
| 2098 |
$factoryCache = static::$ulidFactory; |
| 2099 |
|
| 2100 |
static::$ulidFactory = null; |
| 2101 |
|
| 2102 |
$ulid = static::ulid(); |
| 2103 |
|
| 2104 |
static::$ulidFactory = $factoryCache; |
| 2105 |
|
| 2106 |
$next++; |
| 2107 |
|
| 2108 |
return $ulid; |
| 2109 |
}; |
| 2110 |
|
| 2111 |
static::createUlidsUsing(function () use (&$next, $sequence, $whenMissing) { |
| 2112 |
if (array_key_exists($next, $sequence)) { |
| 2113 |
return $sequence[$next++]; |
| 2114 |
} |
| 2115 |
|
| 2116 |
return $whenMissing(); |
| 2117 |
}); |
| 2118 |
} |
| 2119 |
|
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
|
| 2125 |
|
| 2126 |
public static function freezeUlids(?Closure $callback = null) |
| 2127 |
{ |
| 2128 |
$ulid = Str::ulid(); |
| 2129 |
|
| 2130 |
Str::createUlidsUsing(fn () => $ulid); |
| 2131 |
|
| 2132 |
if ($callback !== null) { |
| 2133 |
try { |
| 2134 |
$callback($ulid); |
| 2135 |
} finally { |
| 2136 |
Str::createUlidsNormally(); |
| 2137 |
} |
| 2138 |
} |
| 2139 |
|
| 2140 |
return $ulid; |
| 2141 |
} |
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
public static function flushCache() |
| 2149 |
{ |
| 2150 |
static::$snakeCache = []; |
| 2151 |
static::$camelCache = []; |
| 2152 |
static::$studlyCache = []; |
| 2153 |
} |
| 2154 |
} |