| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Stdlib; |
| 4 |
|
| 5 |
use Stringable; |
| 6 |
|
| 7 |
/** |
| 8 |
* Collection of string manipulation functions |
| 9 |
*/ |
| 10 |
class Str |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Convert the given string to camel case |
| 14 |
* |
| 15 |
* The given string may be delimited by the following characters: '_' (underscore), '-' (dash), ' ' (space). |
| 16 |
* |
| 17 |
* @param ?string $subject |
| 18 |
* |
| 19 |
* @return string |
| 20 |
*/ |
| 21 |
public static function camel(?string $subject): string |
| 22 |
{ |
| 23 |
if ($subject === null) { |
| 24 |
return ''; |
| 25 |
} |
| 26 |
|
| 27 |
$normalized = str_replace(['-', '_'], ' ', $subject); |
| 28 |
|
| 29 |
return lcfirst(str_replace(' ', '', ucwords(strtolower($normalized)))); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Check if the given string starts with the specified substring |
| 34 |
* |
| 35 |
* @param ?string $subject |
| 36 |
* @param string $start |
| 37 |
* @param bool $caseSensitive |
| 38 |
* |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
public static function startsWith(?string $subject, string $start, bool $caseSensitive = true): bool |
| 42 |
{ |
| 43 |
$subject = $subject ?? ''; |
| 44 |
if (! $caseSensitive) { |
| 45 |
return strncasecmp($subject, $start, strlen($start)) === 0; |
| 46 |
} |
| 47 |
|
| 48 |
return str_starts_with($subject, $start); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Split string into an array padded to the size specified by limit |
| 53 |
* |
| 54 |
* This method is a perfect fit if you need default values for symmetric array destructuring. |
| 55 |
* |
| 56 |
* @param ?string $subject |
| 57 |
* @param string $delimiter |
| 58 |
* @param int $limit |
| 59 |
* @param ?string $default |
| 60 |
* |
| 61 |
* @return array<int, ?string> |
| 62 |
*/ |
| 63 |
public static function symmetricSplit( |
| 64 |
?string $subject, |
| 65 |
string $delimiter, |
| 66 |
int $limit, |
| 67 |
?string $default = null |
| 68 |
): array { |
| 69 |
if ($subject === null || empty($delimiter)) { |
| 70 |
return array_pad([], $limit, $default); |
| 71 |
} |
| 72 |
|
| 73 |
return array_pad(explode($delimiter, $subject, $limit), $limit, $default); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Split string into an array and trim spaces |
| 78 |
* |
| 79 |
* @param ?string $subject |
| 80 |
* @param string $delimiter |
| 81 |
* @param ?int $limit |
| 82 |
* |
| 83 |
* @return array<string> |
| 84 |
*/ |
| 85 |
public static function trimSplit(?string $subject, string $delimiter = ',', ?int $limit = null): array |
| 86 |
{ |
| 87 |
if ($subject === null || empty($delimiter)) { |
| 88 |
return []; |
| 89 |
} |
| 90 |
|
| 91 |
$exploded = $limit !== null ? explode($delimiter, $subject, $limit) : explode($delimiter, $subject); |
| 92 |
|
| 93 |
return array_map('trim', $exploded); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Check if the given string is empty |
| 98 |
* |
| 99 |
* Null is considered empty, and strings consisting only of whitespace or visually |
| 100 |
* empty characters are considered empty. |
| 101 |
* |
| 102 |
* @param string|Stringable|null $subject |
| 103 |
* |
| 104 |
* @return bool |
| 105 |
*/ |
| 106 |
public static function isEmpty(string|Stringable|null $subject): bool |
| 107 |
{ |
| 108 |
return $subject === null || preg_match('/^[\s\x{3164}\x{1160}]*$/u', $subject) === 1; |
| 109 |
} |
| 110 |
} |