|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
5 | 5 | namespace Tempest\Support\Str { |
6 | | - use ArrayAccess; |
7 | 6 | use Countable; |
8 | 7 | use Stringable; |
9 | | - use Tempest\Support\Arr\ImmutableArray; |
| 8 | + use Tempest\Support\Arr; |
10 | 9 | use Tempest\Support\Language; |
11 | 10 | use voku\helper\ASCII; |
12 | 11 |
|
@@ -177,11 +176,11 @@ function lower_first(Stringable|string $string): string |
177 | 176 | /** |
178 | 177 | * Replaces consecutive instances of a given character with a single character. |
179 | 178 | */ |
180 | | - function deduplicate(Stringable|string $string, Stringable|string|ArrayAccess|array $characters = ' '): string |
| 179 | + function deduplicate(Stringable|string $string, Stringable|string|iterable $characters = ' '): string |
181 | 180 | { |
182 | 181 | $string = (string) $string; |
183 | 182 |
|
184 | | - foreach (arr($characters) as $character) { |
| 183 | + foreach (Arr\wrap($characters) as $character) { |
185 | 184 | $string = preg_replace('/' . preg_quote($character, '/') . '+/u', $character, $string); |
186 | 185 | } |
187 | 186 |
|
@@ -231,7 +230,7 @@ function after_first(Stringable|string $string, Stringable|string|array $search) |
231 | 230 | $nearestPosition = mb_strlen($string); // Initialize with a large value |
232 | 231 | $foundSearch = ''; |
233 | 232 |
|
234 | | - foreach (arr($search) as $term) { |
| 233 | + foreach (Arr\wrap($search) as $term) { |
235 | 234 | $position = mb_strpos($string, $term); |
236 | 235 |
|
237 | 236 | if ($position !== false && $position < $nearestPosition) { |
@@ -262,7 +261,7 @@ function after_last(Stringable|string $string, Stringable|string|array $search): |
262 | 261 | $farthestPosition = -1; |
263 | 262 | $foundSearch = null; |
264 | 263 |
|
265 | | - foreach (arr($search) as $term) { |
| 264 | + foreach (Arr\wrap($search) as $term) { |
266 | 265 | $position = mb_strrpos($string, $term); |
267 | 266 |
|
268 | 267 | if ($position !== false && $position > $farthestPosition) { |
@@ -292,7 +291,7 @@ function before_first(Stringable|string $string, Stringable|string|array $search |
292 | 291 |
|
293 | 292 | $nearestPosition = mb_strlen($string); |
294 | 293 |
|
295 | | - foreach (arr($search) as $char) { |
| 294 | + foreach (Arr\wrap($search) as $char) { |
296 | 295 | $position = mb_strpos($string, $char); |
297 | 296 |
|
298 | 297 | if ($position !== false && $position < $nearestPosition) { |
@@ -321,7 +320,7 @@ function before_last(Stringable|string $string, Stringable|string|array $search) |
321 | 320 |
|
322 | 321 | $farthestPosition = -1; |
323 | 322 |
|
324 | | - foreach (arr($search) as $char) { |
| 323 | + foreach (Arr\wrap($search) as $char) { |
325 | 324 | $position = mb_strrpos($string, $char); |
326 | 325 |
|
327 | 326 | if ($position !== false && $position > $farthestPosition) { |
@@ -383,94 +382,110 @@ function ends_with(Stringable|string $string, Stringable|string|array $needles): |
383 | 382 | /** |
384 | 383 | * Replaces the first occurrence of `$search` with `$replace`. |
385 | 384 | */ |
386 | | - function replace_first(Stringable|string $string, Stringable|string $search, Stringable|string $replace): string |
| 385 | + function replace_first(Stringable|string $string, array|Stringable|string $search, Stringable|string $replace): string |
387 | 386 | { |
388 | 387 | $string = (string) $string; |
389 | 388 | $search = normalize_string($search); |
390 | 389 |
|
391 | | - if ($search === '') { |
392 | | - return $string; |
393 | | - } |
| 390 | + foreach (Arr\wrap($search) as $item) { |
| 391 | + if ($search === '') { |
| 392 | + continue; |
| 393 | + } |
394 | 394 |
|
395 | | - $position = strpos($string, (string) $search); |
| 395 | + $position = strpos($string, (string) $item); |
396 | 396 |
|
397 | | - if ($position === false) { |
398 | | - return $string; |
| 397 | + if ($position === false) { |
| 398 | + continue; |
| 399 | + } |
| 400 | + |
| 401 | + return substr_replace($string, $replace, $position, strlen($item)); |
399 | 402 | } |
400 | 403 |
|
401 | | - return substr_replace($string, $replace, $position, strlen($search)); |
| 404 | + return $string; |
402 | 405 | } |
403 | 406 |
|
404 | 407 | /** |
405 | 408 | * Replaces the last occurrence of `$search` with `$replace`. |
406 | 409 | */ |
407 | | - function replace_last(Stringable|string $string, Stringable|string $search, Stringable|string $replace): string |
| 410 | + function replace_last(Stringable|string $string, array|Stringable|string $search, Stringable|string $replace): string |
408 | 411 | { |
409 | 412 | $string = (string) $string; |
410 | 413 | $search = normalize_string($search); |
411 | 414 |
|
412 | | - if ($search === '') { |
413 | | - return $string; |
414 | | - } |
| 415 | + foreach (Arr\wrap($search) as $item) { |
| 416 | + if ($item === '') { |
| 417 | + continue; |
| 418 | + } |
415 | 419 |
|
416 | | - $position = strrpos($string, (string) $search); |
| 420 | + $position = strrpos($string, (string) $item); |
417 | 421 |
|
418 | | - if ($position === false) { |
419 | | - return $string; |
| 422 | + if ($position === false) { |
| 423 | + continue; |
| 424 | + } |
| 425 | + |
| 426 | + return substr_replace($string, $replace, $position, strlen($item)); |
420 | 427 | } |
421 | 428 |
|
422 | | - return substr_replace($string, $replace, $position, strlen($search)); |
| 429 | + return $string; |
423 | 430 | } |
424 | 431 |
|
425 | 432 | /** |
426 | 433 | * Replaces `$search` with `$replace` if `$search` is at the end of the string. |
427 | 434 | */ |
428 | | - function replace_end(Stringable|string $string, Stringable|string $search, Stringable|string $replace): string |
| 435 | + function replace_end(Stringable|string $string, array|Stringable|string $search, Stringable|string $replace): string |
429 | 436 | { |
430 | 437 | $string = (string) $string; |
431 | 438 | $search = normalize_string($search); |
432 | 439 |
|
433 | | - if ($search === '') { |
434 | | - return $string; |
435 | | - } |
| 440 | + foreach (Arr\wrap($search) as $item) { |
| 441 | + if ($search === '') { |
| 442 | + continue; |
| 443 | + } |
436 | 444 |
|
437 | | - if (! ends_with($string, $search)) { |
438 | | - return $string; |
| 445 | + if (! ends_with($string, $item)) { |
| 446 | + continue; |
| 447 | + } |
| 448 | + |
| 449 | + return replace_last($string, $item, $replace); |
439 | 450 | } |
440 | 451 |
|
441 | | - return replace_last($string, $search, $replace); |
| 452 | + return $string; |
442 | 453 | } |
443 | 454 |
|
444 | 455 | /** |
445 | 456 | * Replaces `$search` with `$replace` if `$search` is at the start of the string. |
446 | 457 | */ |
447 | | - function replace_start(Stringable|string $string, Stringable|string $search, Stringable|string $replace): string |
| 458 | + function replace_start(Stringable|string $string, array|Stringable|string $search, Stringable|string $replace): string |
448 | 459 | { |
449 | 460 | $string = (string) $string; |
450 | 461 |
|
451 | | - if ($search === '') { |
452 | | - return $string; |
453 | | - } |
| 462 | + foreach (Arr\wrap($search) as $item) { |
| 463 | + if ($search === '') { |
| 464 | + continue; |
| 465 | + } |
454 | 466 |
|
455 | | - if (! starts_with($string, $search)) { |
456 | | - return $string; |
| 467 | + if (! starts_with($string, $item)) { |
| 468 | + continue; |
| 469 | + } |
| 470 | + |
| 471 | + return replace_first($string, $item, $replace); |
457 | 472 | } |
458 | 473 |
|
459 | | - return replace_first($string, $search, $replace); |
| 474 | + return $string; |
460 | 475 | } |
461 | 476 |
|
462 | 477 | /** |
463 | 478 | * Strips the specified `$prefix` from the start of the string. |
464 | 479 | */ |
465 | | - function strip_start(Stringable|string $string, Stringable|string $prefix): string |
| 480 | + function strip_start(Stringable|string $string, array|Stringable|string $prefix): string |
466 | 481 | { |
467 | 482 | return replace_start($string, $prefix, ''); |
468 | 483 | } |
469 | 484 |
|
470 | 485 | /** |
471 | 486 | * Strips the specified `$suffix` from the end of the string. |
472 | 487 | */ |
473 | | - function strip_end(Stringable|string $string, Stringable|string $suffix): string |
| 488 | + function strip_end(Stringable|string $string, array|Stringable|string $suffix): string |
474 | 489 | { |
475 | 490 | return replace_end($string, $suffix, ''); |
476 | 491 | } |
@@ -635,9 +650,15 @@ function substring(Stringable|string $string, int $start, ?int $length = null): |
635 | 650 | /** |
636 | 651 | * Checks whether the given string contains the specified `$needle`. |
637 | 652 | */ |
638 | | - function contains(Stringable|string $string, string|Stringable $needle): bool |
| 653 | + function contains(Stringable|string $string, Stringable|string|array $needle): bool |
639 | 654 | { |
640 | | - return str_contains((string) $string, (string) $needle); |
| 655 | + foreach (Arr\wrap($needle) as $item) { |
| 656 | + if (str_contains((string) $string, (string) $item)) { |
| 657 | + return true; |
| 658 | + } |
| 659 | + } |
| 660 | + |
| 661 | + return false; |
641 | 662 | } |
642 | 663 |
|
643 | 664 | /** |
|
0 commit comments