|
2 | 2 |
|
3 | 3 | namespace Utilitte\Php; |
4 | 4 |
|
5 | | -use JetBrains\PhpStorm\ArrayShape; |
6 | | - |
7 | 5 | final class Strings |
8 | 6 | { |
9 | 7 |
|
10 | | - #[ArrayShape('string', 'string')] |
11 | | - public static function splitByPosition(string $haystack, int $pos): array |
| 8 | + /** |
| 9 | + * @return array{string|null, string} |
| 10 | + */ |
| 11 | + public static function splitByNeedle(string $haystack, string $needle): array |
| 12 | + { |
| 13 | + return self::splitByPositionFalseable($haystack, strpos($haystack, $needle), strlen($needle)); |
| 14 | + } |
| 15 | + |
| 16 | + public static function joinWith(string $join, string|null ... $arguments): string |
| 17 | + { |
| 18 | + return implode($join, array_filter($arguments)); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @return array{string, string} |
| 23 | + */ |
| 24 | + public static function splitByPosition(string $haystack, int $pos, int $length = 1): array |
12 | 25 | { |
13 | 26 | return [ |
14 | 27 | substr($haystack, 0, $pos), |
15 | | - substr($haystack, $pos + 1), |
| 28 | + substr($haystack, $pos + $length), |
16 | 29 | ]; |
17 | 30 | } |
18 | 31 |
|
19 | | - #[ArrayShape('string|null', 'string')] |
20 | | - public static function splitByPositionFalseable(string $haystack, int|false $pos): array |
| 32 | + /** |
| 33 | + * @return array{string|null, string} |
| 34 | + */ |
| 35 | + public static function splitByPositionFalseable(string $haystack, int|false $pos, int $length = 1): array |
21 | 36 | { |
22 | 37 | if ($pos === false) { |
23 | 38 | return [null, $haystack]; |
24 | 39 | } |
25 | 40 |
|
26 | | - return self::splitByPosition($haystack, $pos); |
| 41 | + return self::splitByPosition($haystack, $pos, $length); |
27 | 42 | } |
28 | 43 |
|
29 | 44 | public static function append(?string $string, string $append): ?string |
|
0 commit comments