Facades
2 years ago
Testing
2 years ago
Traits
2 years ago
AggregateServiceProvider.php
2 years ago
Carbon.php
2 years ago
Composer.php
2 years ago
ConfigurationUrlParser.php
2 years ago
DateFactory.php
2 years ago
Env.php
2 years ago
Fluent.php
2 years ago
HigherOrderTapProxy.php
2 years ago
HtmlString.php
2 years ago
InteractsWithTime.php
2 years ago
Js.php
2 years ago
Manager.php
2 years ago
MessageBag.php
2 years ago
MultipleInstanceManager.php
2 years ago
NamespacedItemResolver.php
2 years ago
Optional.php
2 years ago
Pluralizer.php
2 years ago
ProcessUtils.php
2 years ago
Reflector.php
2 years ago
ServiceProvider.php
2 years ago
Str.php
2 years ago
Stringable.php
2 years ago
Timebox.php
2 years ago
ValidatedInput.php
2 years ago
ViewErrorBag.php
2 years ago
helpers.php
2 years ago
Str.php
877 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\Illuminate\Support; |
| 4 | |
| 5 | use IAWP_SCOPED\Illuminate\Support\Traits\Macroable; |
| 6 | use IAWP_SCOPED\League\CommonMark\GithubFlavoredMarkdownConverter; |
| 7 | use IAWP_SCOPED\Ramsey\Uuid\Codec\TimestampFirstCombCodec; |
| 8 | use IAWP_SCOPED\Ramsey\Uuid\Generator\CombGenerator; |
| 9 | use IAWP_SCOPED\Ramsey\Uuid\Uuid; |
| 10 | use IAWP_SCOPED\Ramsey\Uuid\UuidFactory; |
| 11 | use IAWP_SCOPED\voku\helper\ASCII; |
| 12 | /** @internal */ |
| 13 | class Str |
| 14 | { |
| 15 | use Macroable; |
| 16 | /** |
| 17 | * The cache of snake-cased words. |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | protected static $snakeCache = []; |
| 22 | /** |
| 23 | * The cache of camel-cased words. |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | protected static $camelCache = []; |
| 28 | /** |
| 29 | * The cache of studly-cased words. |
| 30 | * |
| 31 | * @var array |
| 32 | */ |
| 33 | protected static $studlyCache = []; |
| 34 | /** |
| 35 | * The callback that should be used to generate UUIDs. |
| 36 | * |
| 37 | * @var callable |
| 38 | */ |
| 39 | protected static $uuidFactory; |
| 40 | /** |
| 41 | * Get a new stringable object from the given string. |
| 42 | * |
| 43 | * @param string $string |
| 44 | * @return \Illuminate\Support\Stringable |
| 45 | */ |
| 46 | public static function of($string) |
| 47 | { |
| 48 | return new Stringable($string); |
| 49 | } |
| 50 | /** |
| 51 | * Return the remainder of a string after the first occurrence of a given value. |
| 52 | * |
| 53 | * @param string $subject |
| 54 | * @param string $search |
| 55 | * @return string |
| 56 | */ |
| 57 | public static function after($subject, $search) |
| 58 | { |
| 59 | return $search === '' ? $subject : \array_reverse(\explode($search, $subject, 2))[0]; |
| 60 | } |
| 61 | /** |
| 62 | * Return the remainder of a string after the last occurrence of a given value. |
| 63 | * |
| 64 | * @param string $subject |
| 65 | * @param string $search |
| 66 | * @return string |
| 67 | */ |
| 68 | public static function afterLast($subject, $search) |
| 69 | { |
| 70 | if ($search === '') { |
| 71 | return $subject; |
| 72 | } |
| 73 | $position = \strrpos($subject, (string) $search); |
| 74 | if ($position === \false) { |
| 75 | return $subject; |
| 76 | } |
| 77 | return \substr($subject, $position + \strlen($search)); |
| 78 | } |
| 79 | /** |
| 80 | * Transliterate a UTF-8 value to ASCII. |
| 81 | * |
| 82 | * @param string $value |
| 83 | * @param string $language |
| 84 | * @return string |
| 85 | */ |
| 86 | public static function ascii($value, $language = 'en') |
| 87 | { |
| 88 | return ASCII::to_ascii((string) $value, $language); |
| 89 | } |
| 90 | /** |
| 91 | * Transliterate a string to its closest ASCII representation. |
| 92 | * |
| 93 | * @param string $string |
| 94 | * @param string|null $unknown |
| 95 | * @param bool|null $strict |
| 96 | * @return string |
| 97 | */ |
| 98 | public static function transliterate($string, $unknown = '?', $strict = \false) |
| 99 | { |
| 100 | return ASCII::to_transliterate($string, $unknown, $strict); |
| 101 | } |
| 102 | /** |
| 103 | * Get the portion of a string before the first occurrence of a given value. |
| 104 | * |
| 105 | * @param string $subject |
| 106 | * @param string $search |
| 107 | * @return string |
| 108 | */ |
| 109 | public static function before($subject, $search) |
| 110 | { |
| 111 | if ($search === '') { |
| 112 | return $subject; |
| 113 | } |
| 114 | $result = \strstr($subject, (string) $search, \true); |
| 115 | return $result === \false ? $subject : $result; |
| 116 | } |
| 117 | /** |
| 118 | * Get the portion of a string before the last occurrence of a given value. |
| 119 | * |
| 120 | * @param string $subject |
| 121 | * @param string $search |
| 122 | * @return string |
| 123 | */ |
| 124 | public static function beforeLast($subject, $search) |
| 125 | { |
| 126 | if ($search === '') { |
| 127 | return $subject; |
| 128 | } |
| 129 | $pos = \mb_strrpos($subject, $search); |
| 130 | if ($pos === \false) { |
| 131 | return $subject; |
| 132 | } |
| 133 | return static::substr($subject, 0, $pos); |
| 134 | } |
| 135 | /** |
| 136 | * Get the portion of a string between two given values. |
| 137 | * |
| 138 | * @param string $subject |
| 139 | * @param string $from |
| 140 | * @param string $to |
| 141 | * @return string |
| 142 | */ |
| 143 | public static function between($subject, $from, $to) |
| 144 | { |
| 145 | if ($from === '' || $to === '') { |
| 146 | return $subject; |
| 147 | } |
| 148 | return static::beforeLast(static::after($subject, $from), $to); |
| 149 | } |
| 150 | /** |
| 151 | * Convert a value to camel case. |
| 152 | * |
| 153 | * @param string $value |
| 154 | * @return string |
| 155 | */ |
| 156 | public static function camel($value) |
| 157 | { |
| 158 | if (isset(static::$camelCache[$value])) { |
| 159 | return static::$camelCache[$value]; |
| 160 | } |
| 161 | return static::$camelCache[$value] = \lcfirst(static::studly($value)); |
| 162 | } |
| 163 | /** |
| 164 | * Determine if a given string contains a given substring. |
| 165 | * |
| 166 | * @param string $haystack |
| 167 | * @param string|string[] $needles |
| 168 | * @return bool |
| 169 | */ |
| 170 | public static function contains($haystack, $needles) |
| 171 | { |
| 172 | foreach ((array) $needles as $needle) { |
| 173 | if ($needle !== '' && \mb_strpos($haystack, $needle) !== \false) { |
| 174 | return \true; |
| 175 | } |
| 176 | } |
| 177 | return \false; |
| 178 | } |
| 179 | /** |
| 180 | * Determine if a given string contains all array values. |
| 181 | * |
| 182 | * @param string $haystack |
| 183 | * @param string[] $needles |
| 184 | * @return bool |
| 185 | */ |
| 186 | public static function containsAll($haystack, array $needles) |
| 187 | { |
| 188 | foreach ($needles as $needle) { |
| 189 | if (!static::contains($haystack, $needle)) { |
| 190 | return \false; |
| 191 | } |
| 192 | } |
| 193 | return \true; |
| 194 | } |
| 195 | /** |
| 196 | * Determine if a given string ends with a given substring. |
| 197 | * |
| 198 | * @param string $haystack |
| 199 | * @param string|string[] $needles |
| 200 | * @return bool |
| 201 | */ |
| 202 | public static function endsWith($haystack, $needles) |
| 203 | { |
| 204 | foreach ((array) $needles as $needle) { |
| 205 | if ($needle !== '' && $needle !== null && \substr($haystack, -\strlen($needle)) === (string) $needle) { |
| 206 | return \true; |
| 207 | } |
| 208 | } |
| 209 | return \false; |
| 210 | } |
| 211 | /** |
| 212 | * Cap a string with a single instance of a given value. |
| 213 | * |
| 214 | * @param string $value |
| 215 | * @param string $cap |
| 216 | * @return string |
| 217 | */ |
| 218 | public static function finish($value, $cap) |
| 219 | { |
| 220 | $quoted = \preg_quote($cap, '/'); |
| 221 | return \preg_replace('/(?:' . $quoted . ')+$/u', '', $value) . $cap; |
| 222 | } |
| 223 | /** |
| 224 | * Determine if a given string matches a given pattern. |
| 225 | * |
| 226 | * @param string|array $pattern |
| 227 | * @param string $value |
| 228 | * @return bool |
| 229 | */ |
| 230 | public static function is($pattern, $value) |
| 231 | { |
| 232 | $patterns = Arr::wrap($pattern); |
| 233 | $value = (string) $value; |
| 234 | if (empty($patterns)) { |
| 235 | return \false; |
| 236 | } |
| 237 | foreach ($patterns as $pattern) { |
| 238 | $pattern = (string) $pattern; |
| 239 | // If the given value is an exact match we can of course return true right |
| 240 | // from the beginning. Otherwise, we will translate asterisks and do an |
| 241 | // actual pattern match against the two strings to see if they match. |
| 242 | if ($pattern == $value) { |
| 243 | return \true; |
| 244 | } |
| 245 | $pattern = \preg_quote($pattern, '#'); |
| 246 | // Asterisks are translated into zero-or-more regular expression wildcards |
| 247 | // to make it convenient to check if the strings starts with the given |
| 248 | // pattern such as "library/*", making any string check convenient. |
| 249 | $pattern = \str_replace('\\*', '.*', $pattern); |
| 250 | if (\preg_match('#^' . $pattern . '\\z#u', $value) === 1) { |
| 251 | return \true; |
| 252 | } |
| 253 | } |
| 254 | return \false; |
| 255 | } |
| 256 | /** |
| 257 | * Determine if a given string is 7 bit ASCII. |
| 258 | * |
| 259 | * @param string $value |
| 260 | * @return bool |
| 261 | */ |
| 262 | public static function isAscii($value) |
| 263 | { |
| 264 | return ASCII::is_ascii((string) $value); |
| 265 | } |
| 266 | /** |
| 267 | * Determine if a given string is a valid UUID. |
| 268 | * |
| 269 | * @param string $value |
| 270 | * @return bool |
| 271 | */ |
| 272 | public static function isUuid($value) |
| 273 | { |
| 274 | if (!\is_string($value)) { |
| 275 | return \false; |
| 276 | } |
| 277 | return \preg_match('/^[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}$/iD', $value) > 0; |
| 278 | } |
| 279 | /** |
| 280 | * Convert a string to kebab case. |
| 281 | * |
| 282 | * @param string $value |
| 283 | * @return string |
| 284 | */ |
| 285 | public static function kebab($value) |
| 286 | { |
| 287 | return static::snake($value, '-'); |
| 288 | } |
| 289 | /** |
| 290 | * Return the length of the given string. |
| 291 | * |
| 292 | * @param string $value |
| 293 | * @param string|null $encoding |
| 294 | * @return int |
| 295 | */ |
| 296 | public static function length($value, $encoding = null) |
| 297 | { |
| 298 | if ($encoding) { |
| 299 | return \mb_strlen($value, $encoding); |
| 300 | } |
| 301 | return \mb_strlen($value); |
| 302 | } |
| 303 | /** |
| 304 | * Limit the number of characters in a string. |
| 305 | * |
| 306 | * @param string $value |
| 307 | * @param int $limit |
| 308 | * @param string $end |
| 309 | * @return string |
| 310 | */ |
| 311 | public static function limit($value, $limit = 100, $end = '...') |
| 312 | { |
| 313 | if (\mb_strwidth($value, 'UTF-8') <= $limit) { |
| 314 | return $value; |
| 315 | } |
| 316 | return \rtrim(\mb_strimwidth($value, 0, $limit, '', 'UTF-8')) . $end; |
| 317 | } |
| 318 | /** |
| 319 | * Convert the given string to lower-case. |
| 320 | * |
| 321 | * @param string $value |
| 322 | * @return string |
| 323 | */ |
| 324 | public static function lower($value) |
| 325 | { |
| 326 | return \mb_strtolower($value, 'UTF-8'); |
| 327 | } |
| 328 | /** |
| 329 | * Limit the number of words in a string. |
| 330 | * |
| 331 | * @param string $value |
| 332 | * @param int $words |
| 333 | * @param string $end |
| 334 | * @return string |
| 335 | */ |
| 336 | public static function words($value, $words = 100, $end = '...') |
| 337 | { |
| 338 | \preg_match('/^\\s*+(?:\\S++\\s*+){1,' . $words . '}/u', $value, $matches); |
| 339 | if (!isset($matches[0]) || static::length($value) === static::length($matches[0])) { |
| 340 | return $value; |
| 341 | } |
| 342 | return \rtrim($matches[0]) . $end; |
| 343 | } |
| 344 | /** |
| 345 | * Converts GitHub flavored Markdown into HTML. |
| 346 | * |
| 347 | * @param string $string |
| 348 | * @param array $options |
| 349 | * @return string |
| 350 | */ |
| 351 | public static function markdown($string, array $options = []) |
| 352 | { |
| 353 | $converter = new GithubFlavoredMarkdownConverter($options); |
| 354 | return (string) $converter->convertToHtml($string); |
| 355 | } |
| 356 | /** |
| 357 | * Masks a portion of a string with a repeated character. |
| 358 | * |
| 359 | * @param string $string |
| 360 | * @param string $character |
| 361 | * @param int $index |
| 362 | * @param int|null $length |
| 363 | * @param string $encoding |
| 364 | * @return string |
| 365 | */ |
| 366 | public static function mask($string, $character, $index, $length = null, $encoding = 'UTF-8') |
| 367 | { |
| 368 | if ($character === '') { |
| 369 | return $string; |
| 370 | } |
| 371 | if (\is_null($length) && \PHP_MAJOR_VERSION < 8) { |
| 372 | $length = \mb_strlen($string, $encoding); |
| 373 | } |
| 374 | $segment = \mb_substr($string, $index, $length, $encoding); |
| 375 | if ($segment === '') { |
| 376 | return $string; |
| 377 | } |
| 378 | $strlen = \mb_strlen($string, $encoding); |
| 379 | $startIndex = $index; |
| 380 | if ($index < 0) { |
| 381 | $startIndex = $index < -$strlen ? 0 : $strlen + $index; |
| 382 | } |
| 383 | $start = \mb_substr($string, 0, $startIndex, $encoding); |
| 384 | $segmentLen = \mb_strlen($segment, $encoding); |
| 385 | $end = \mb_substr($string, $startIndex + $segmentLen); |
| 386 | return $start . \str_repeat(\mb_substr($character, 0, 1, $encoding), $segmentLen) . $end; |
| 387 | } |
| 388 | /** |
| 389 | * Get the string matching the given pattern. |
| 390 | * |
| 391 | * @param string $pattern |
| 392 | * @param string $subject |
| 393 | * @return string |
| 394 | */ |
| 395 | public static function match($pattern, $subject) |
| 396 | { |
| 397 | \preg_match($pattern, $subject, $matches); |
| 398 | if (!$matches) { |
| 399 | return ''; |
| 400 | } |
| 401 | return $matches[1] ?? $matches[0]; |
| 402 | } |
| 403 | /** |
| 404 | * Get the string matching the given pattern. |
| 405 | * |
| 406 | * @param string $pattern |
| 407 | * @param string $subject |
| 408 | * @return \Illuminate\Support\Collection |
| 409 | */ |
| 410 | public static function matchAll($pattern, $subject) |
| 411 | { |
| 412 | \preg_match_all($pattern, $subject, $matches); |
| 413 | if (empty($matches[0])) { |
| 414 | return \IAWP_SCOPED\collect(); |
| 415 | } |
| 416 | return \IAWP_SCOPED\collect($matches[1] ?? $matches[0]); |
| 417 | } |
| 418 | /** |
| 419 | * Pad both sides of a string with another. |
| 420 | * |
| 421 | * @param string $value |
| 422 | * @param int $length |
| 423 | * @param string $pad |
| 424 | * @return string |
| 425 | */ |
| 426 | public static function padBoth($value, $length, $pad = ' ') |
| 427 | { |
| 428 | return \str_pad($value, \strlen($value) - \mb_strlen($value) + $length, $pad, \STR_PAD_BOTH); |
| 429 | } |
| 430 | /** |
| 431 | * Pad the left side of a string with another. |
| 432 | * |
| 433 | * @param string $value |
| 434 | * @param int $length |
| 435 | * @param string $pad |
| 436 | * @return string |
| 437 | */ |
| 438 | public static function padLeft($value, $length, $pad = ' ') |
| 439 | { |
| 440 | return \str_pad($value, \strlen($value) - \mb_strlen($value) + $length, $pad, \STR_PAD_LEFT); |
| 441 | } |
| 442 | /** |
| 443 | * Pad the right side of a string with another. |
| 444 | * |
| 445 | * @param string $value |
| 446 | * @param int $length |
| 447 | * @param string $pad |
| 448 | * @return string |
| 449 | */ |
| 450 | public static function padRight($value, $length, $pad = ' ') |
| 451 | { |
| 452 | return \str_pad($value, \strlen($value) - \mb_strlen($value) + $length, $pad, \STR_PAD_RIGHT); |
| 453 | } |
| 454 | /** |
| 455 | * Parse a Class[@]method style callback into class and method. |
| 456 | * |
| 457 | * @param string $callback |
| 458 | * @param string|null $default |
| 459 | * @return array<int, string|null> |
| 460 | */ |
| 461 | public static function parseCallback($callback, $default = null) |
| 462 | { |
| 463 | return static::contains($callback, '@') ? \explode('@', $callback, 2) : [$callback, $default]; |
| 464 | } |
| 465 | /** |
| 466 | * Get the plural form of an English word. |
| 467 | * |
| 468 | * @param string $value |
| 469 | * @param int|array|\Countable $count |
| 470 | * @return string |
| 471 | */ |
| 472 | public static function plural($value, $count = 2) |
| 473 | { |
| 474 | return Pluralizer::plural($value, $count); |
| 475 | } |
| 476 | /** |
| 477 | * Pluralize the last word of an English, studly caps case string. |
| 478 | * |
| 479 | * @param string $value |
| 480 | * @param int|array|\Countable $count |
| 481 | * @return string |
| 482 | */ |
| 483 | public static function pluralStudly($value, $count = 2) |
| 484 | { |
| 485 | $parts = \preg_split('/(.)(?=[A-Z])/u', $value, -1, \PREG_SPLIT_DELIM_CAPTURE); |
| 486 | $lastWord = \array_pop($parts); |
| 487 | return \implode('', $parts) . self::plural($lastWord, $count); |
| 488 | } |
| 489 | /** |
| 490 | * Generate a more truly "random" alpha-numeric string. |
| 491 | * |
| 492 | * @param int $length |
| 493 | * @return string |
| 494 | */ |
| 495 | public static function random($length = 16) |
| 496 | { |
| 497 | $string = ''; |
| 498 | while (($len = \strlen($string)) < $length) { |
| 499 | $size = $length - $len; |
| 500 | $bytes = \random_bytes($size); |
| 501 | $string .= \substr(\str_replace(['/', '+', '='], '', \base64_encode($bytes)), 0, $size); |
| 502 | } |
| 503 | return $string; |
| 504 | } |
| 505 | /** |
| 506 | * Repeat the given string. |
| 507 | * |
| 508 | * @param string $string |
| 509 | * @param int $times |
| 510 | * @return string |
| 511 | */ |
| 512 | public static function repeat(string $string, int $times) |
| 513 | { |
| 514 | return \str_repeat($string, $times); |
| 515 | } |
| 516 | /** |
| 517 | * Replace a given value in the string sequentially with an array. |
| 518 | * |
| 519 | * @param string $search |
| 520 | * @param array<int|string, string> $replace |
| 521 | * @param string $subject |
| 522 | * @return string |
| 523 | */ |
| 524 | public static function replaceArray($search, array $replace, $subject) |
| 525 | { |
| 526 | $segments = \explode($search, $subject); |
| 527 | $result = \array_shift($segments); |
| 528 | foreach ($segments as $segment) { |
| 529 | $result .= (\array_shift($replace) ?? $search) . $segment; |
| 530 | } |
| 531 | return $result; |
| 532 | } |
| 533 | /** |
| 534 | * Replace the given value in the given string. |
| 535 | * |
| 536 | * @param string|string[] $search |
| 537 | * @param string|string[] $replace |
| 538 | * @param string|string[] $subject |
| 539 | * @return string |
| 540 | */ |
| 541 | public static function replace($search, $replace, $subject) |
| 542 | { |
| 543 | return \str_replace($search, $replace, $subject); |
| 544 | } |
| 545 | /** |
| 546 | * Replace the first occurrence of a given value in the string. |
| 547 | * |
| 548 | * @param string $search |
| 549 | * @param string $replace |
| 550 | * @param string $subject |
| 551 | * @return string |
| 552 | */ |
| 553 | public static function replaceFirst($search, $replace, $subject) |
| 554 | { |
| 555 | if ($search === '') { |
| 556 | return $subject; |
| 557 | } |
| 558 | $position = \strpos($subject, $search); |
| 559 | if ($position !== \false) { |
| 560 | return \substr_replace($subject, $replace, $position, \strlen($search)); |
| 561 | } |
| 562 | return $subject; |
| 563 | } |
| 564 | /** |
| 565 | * Replace the last occurrence of a given value in the string. |
| 566 | * |
| 567 | * @param string $search |
| 568 | * @param string $replace |
| 569 | * @param string $subject |
| 570 | * @return string |
| 571 | */ |
| 572 | public static function replaceLast($search, $replace, $subject) |
| 573 | { |
| 574 | if ($search === '') { |
| 575 | return $subject; |
| 576 | } |
| 577 | $position = \strrpos($subject, $search); |
| 578 | if ($position !== \false) { |
| 579 | return \substr_replace($subject, $replace, $position, \strlen($search)); |
| 580 | } |
| 581 | return $subject; |
| 582 | } |
| 583 | /** |
| 584 | * Remove any occurrence of the given string in the subject. |
| 585 | * |
| 586 | * @param string|array<string> $search |
| 587 | * @param string $subject |
| 588 | * @param bool $caseSensitive |
| 589 | * @return string |
| 590 | */ |
| 591 | public static function remove($search, $subject, $caseSensitive = \true) |
| 592 | { |
| 593 | $subject = $caseSensitive ? \str_replace($search, '', $subject) : \str_ireplace($search, '', $subject); |
| 594 | return $subject; |
| 595 | } |
| 596 | /** |
| 597 | * Reverse the given string. |
| 598 | * |
| 599 | * @param string $value |
| 600 | * @return string |
| 601 | */ |
| 602 | public static function reverse(string $value) |
| 603 | { |
| 604 | return \implode(\array_reverse(\mb_str_split($value))); |
| 605 | } |
| 606 | /** |
| 607 | * Begin a string with a single instance of a given value. |
| 608 | * |
| 609 | * @param string $value |
| 610 | * @param string $prefix |
| 611 | * @return string |
| 612 | */ |
| 613 | public static function start($value, $prefix) |
| 614 | { |
| 615 | $quoted = \preg_quote($prefix, '/'); |
| 616 | return $prefix . \preg_replace('/^(?:' . $quoted . ')+/u', '', $value); |
| 617 | } |
| 618 | /** |
| 619 | * Convert the given string to upper-case. |
| 620 | * |
| 621 | * @param string $value |
| 622 | * @return string |
| 623 | */ |
| 624 | public static function upper($value) |
| 625 | { |
| 626 | return \mb_strtoupper($value, 'UTF-8'); |
| 627 | } |
| 628 | /** |
| 629 | * Convert the given string to title case. |
| 630 | * |
| 631 | * @param string $value |
| 632 | * @return string |
| 633 | */ |
| 634 | public static function title($value) |
| 635 | { |
| 636 | return \mb_convert_case($value, \MB_CASE_TITLE, 'UTF-8'); |
| 637 | } |
| 638 | /** |
| 639 | * Convert the given string to title case for each word. |
| 640 | * |
| 641 | * @param string $value |
| 642 | * @return string |
| 643 | */ |
| 644 | public static function headline($value) |
| 645 | { |
| 646 | $parts = \explode(' ', $value); |
| 647 | $parts = \count($parts) > 1 ? $parts = \array_map([static::class, 'title'], $parts) : ($parts = \array_map([static::class, 'title'], static::ucsplit(\implode('_', $parts)))); |
| 648 | $collapsed = static::replace(['-', '_', ' '], '_', \implode('_', $parts)); |
| 649 | return \implode(' ', \array_filter(\explode('_', $collapsed))); |
| 650 | } |
| 651 | /** |
| 652 | * Get the singular form of an English word. |
| 653 | * |
| 654 | * @param string $value |
| 655 | * @return string |
| 656 | */ |
| 657 | public static function singular($value) |
| 658 | { |
| 659 | return Pluralizer::singular($value); |
| 660 | } |
| 661 | /** |
| 662 | * Generate a URL friendly "slug" from a given string. |
| 663 | * |
| 664 | * @param string $title |
| 665 | * @param string $separator |
| 666 | * @param string|null $language |
| 667 | * @return string |
| 668 | */ |
| 669 | public static function slug($title, $separator = '-', $language = 'en') |
| 670 | { |
| 671 | $title = $language ? static::ascii($title, $language) : $title; |
| 672 | // Convert all dashes/underscores into separator |
| 673 | $flip = $separator === '-' ? '_' : '-'; |
| 674 | $title = \preg_replace('![' . \preg_quote($flip) . ']+!u', $separator, $title); |
| 675 | // Replace @ with the word 'at' |
| 676 | $title = \str_replace('@', $separator . 'at' . $separator, $title); |
| 677 | // Remove all characters that are not the separator, letters, numbers, or whitespace. |
| 678 | $title = \preg_replace('![^' . \preg_quote($separator) . '\\pL\\pN\\s]+!u', '', static::lower($title)); |
| 679 | // Replace all separator characters and whitespace by a single separator |
| 680 | $title = \preg_replace('![' . \preg_quote($separator) . '\\s]+!u', $separator, $title); |
| 681 | return \trim($title, $separator); |
| 682 | } |
| 683 | /** |
| 684 | * Convert a string to snake case. |
| 685 | * |
| 686 | * @param string $value |
| 687 | * @param string $delimiter |
| 688 | * @return string |
| 689 | */ |
| 690 | public static function snake($value, $delimiter = '_') |
| 691 | { |
| 692 | $key = $value; |
| 693 | if (isset(static::$snakeCache[$key][$delimiter])) { |
| 694 | return static::$snakeCache[$key][$delimiter]; |
| 695 | } |
| 696 | if (!\ctype_lower($value)) { |
| 697 | $value = \preg_replace('/\\s+/u', '', \ucwords($value)); |
| 698 | $value = static::lower(\preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value)); |
| 699 | } |
| 700 | return static::$snakeCache[$key][$delimiter] = $value; |
| 701 | } |
| 702 | /** |
| 703 | * Determine if a given string starts with a given substring. |
| 704 | * |
| 705 | * @param string $haystack |
| 706 | * @param string|string[] $needles |
| 707 | * @return bool |
| 708 | */ |
| 709 | public static function startsWith($haystack, $needles) |
| 710 | { |
| 711 | foreach ((array) $needles as $needle) { |
| 712 | if ((string) $needle !== '' && \strncmp($haystack, $needle, \strlen($needle)) === 0) { |
| 713 | return \true; |
| 714 | } |
| 715 | } |
| 716 | return \false; |
| 717 | } |
| 718 | /** |
| 719 | * Convert a value to studly caps case. |
| 720 | * |
| 721 | * @param string $value |
| 722 | * @return string |
| 723 | */ |
| 724 | public static function studly($value) |
| 725 | { |
| 726 | $key = $value; |
| 727 | if (isset(static::$studlyCache[$key])) { |
| 728 | return static::$studlyCache[$key]; |
| 729 | } |
| 730 | $words = \explode(' ', static::replace(['-', '_'], ' ', $value)); |
| 731 | $studlyWords = \array_map(function ($word) { |
| 732 | return static::ucfirst($word); |
| 733 | }, $words); |
| 734 | return static::$studlyCache[$key] = \implode($studlyWords); |
| 735 | } |
| 736 | /** |
| 737 | * Returns the portion of the string specified by the start and length parameters. |
| 738 | * |
| 739 | * @param string $string |
| 740 | * @param int $start |
| 741 | * @param int|null $length |
| 742 | * @return string |
| 743 | */ |
| 744 | public static function substr($string, $start, $length = null) |
| 745 | { |
| 746 | return \mb_substr($string, $start, $length, 'UTF-8'); |
| 747 | } |
| 748 | /** |
| 749 | * Returns the number of substring occurrences. |
| 750 | * |
| 751 | * @param string $haystack |
| 752 | * @param string $needle |
| 753 | * @param int $offset |
| 754 | * @param int|null $length |
| 755 | * @return int |
| 756 | */ |
| 757 | public static function substrCount($haystack, $needle, $offset = 0, $length = null) |
| 758 | { |
| 759 | if (!\is_null($length)) { |
| 760 | return \substr_count($haystack, $needle, $offset, $length); |
| 761 | } else { |
| 762 | return \substr_count($haystack, $needle, $offset); |
| 763 | } |
| 764 | } |
| 765 | /** |
| 766 | * Replace text within a portion of a string. |
| 767 | * |
| 768 | * @param string|array $string |
| 769 | * @param string|array $replace |
| 770 | * @param array|int $offset |
| 771 | * @param array|int|null $length |
| 772 | * @return string|array |
| 773 | */ |
| 774 | public static function substrReplace($string, $replace, $offset = 0, $length = null) |
| 775 | { |
| 776 | if ($length === null) { |
| 777 | $length = \strlen($string); |
| 778 | } |
| 779 | return \substr_replace($string, $replace, $offset, $length); |
| 780 | } |
| 781 | /** |
| 782 | * Swap multiple keywords in a string with other keywords. |
| 783 | * |
| 784 | * @param array $map |
| 785 | * @param string $subject |
| 786 | * @return string |
| 787 | */ |
| 788 | public static function swap(array $map, $subject) |
| 789 | { |
| 790 | return \strtr($subject, $map); |
| 791 | } |
| 792 | /** |
| 793 | * Make a string's first character uppercase. |
| 794 | * |
| 795 | * @param string $string |
| 796 | * @return string |
| 797 | */ |
| 798 | public static function ucfirst($string) |
| 799 | { |
| 800 | return static::upper(static::substr($string, 0, 1)) . static::substr($string, 1); |
| 801 | } |
| 802 | /** |
| 803 | * Split a string into pieces by uppercase characters. |
| 804 | * |
| 805 | * @param string $string |
| 806 | * @return array |
| 807 | */ |
| 808 | public static function ucsplit($string) |
| 809 | { |
| 810 | return \preg_split('/(?=\\p{Lu})/u', $string, -1, \PREG_SPLIT_NO_EMPTY); |
| 811 | } |
| 812 | /** |
| 813 | * Get the number of words a string contains. |
| 814 | * |
| 815 | * @param string $string |
| 816 | * @return int |
| 817 | */ |
| 818 | public static function wordCount($string) |
| 819 | { |
| 820 | return \str_word_count($string); |
| 821 | } |
| 822 | /** |
| 823 | * Generate a UUID (version 4). |
| 824 | * |
| 825 | * @return \Ramsey\Uuid\UuidInterface |
| 826 | */ |
| 827 | public static function uuid() |
| 828 | { |
| 829 | return static::$uuidFactory ? \call_user_func(static::$uuidFactory) : Uuid::uuid4(); |
| 830 | } |
| 831 | /** |
| 832 | * Generate a time-ordered UUID (version 4). |
| 833 | * |
| 834 | * @return \Ramsey\Uuid\UuidInterface |
| 835 | */ |
| 836 | public static function orderedUuid() |
| 837 | { |
| 838 | if (static::$uuidFactory) { |
| 839 | return \call_user_func(static::$uuidFactory); |
| 840 | } |
| 841 | $factory = new UuidFactory(); |
| 842 | $factory->setRandomGenerator(new CombGenerator($factory->getRandomGenerator(), $factory->getNumberConverter())); |
| 843 | $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder())); |
| 844 | return $factory->uuid4(); |
| 845 | } |
| 846 | /** |
| 847 | * Set the callable that will be used to generate UUIDs. |
| 848 | * |
| 849 | * @param callable|null $factory |
| 850 | * @return void |
| 851 | */ |
| 852 | public static function createUuidsUsing(callable $factory = null) |
| 853 | { |
| 854 | static::$uuidFactory = $factory; |
| 855 | } |
| 856 | /** |
| 857 | * Indicate that UUIDs should be created normally and not using a custom factory. |
| 858 | * |
| 859 | * @return void |
| 860 | */ |
| 861 | public static function createUuidsNormally() |
| 862 | { |
| 863 | static::$uuidFactory = null; |
| 864 | } |
| 865 | /** |
| 866 | * Remove all strings from the casing caches. |
| 867 | * |
| 868 | * @return void |
| 869 | */ |
| 870 | public static function flushCache() |
| 871 | { |
| 872 | static::$snakeCache = []; |
| 873 | static::$camelCache = []; |
| 874 | static::$studlyCache = []; |
| 875 | } |
| 876 | } |
| 877 |