66
77use InvalidArgumentException ;
88
9+ use function array_map ;
910use function array_slice ;
11+ use function base64_decode ;
12+ use function base64_encode ;
13+ use function ceil ;
1014use function count ;
11- use function function_exists ;
15+ use function implode ;
1216use function max ;
1317use function mb_strlen ;
18+ use function mb_strrpos ;
1419use function mb_strtolower ;
1520use function mb_strtoupper ;
1621use function mb_substr ;
1722use function preg_match ;
23+ use function preg_quote ;
1824use function preg_replace ;
25+ use function preg_split ;
26+ use function rtrim ;
27+ use function sprintf ;
1928use function str_ends_with ;
29+ use function str_repeat ;
30+ use function str_replace ;
2031use function str_starts_with ;
2132use function strlen ;
33+ use function strtr ;
34+ use function trim ;
2235
2336/**
2437 * Provides static methods to work with strings.
@@ -36,7 +49,7 @@ final class StringHelper
3649 *
3750 * @return int The number of bytes in the given string.
3851 */
39- public static function byteLength (? string $ input ): int
52+ public static function byteLength (string | null $ input ): int
4053 {
4154 return mb_strlen ((string )$ input , '8bit ' );
4255 }
@@ -140,8 +153,13 @@ public static function substring(string $string, int $start, int $length = null,
140153 * If length is zero then this function will have the effect of inserting replacement into string at the given start offset.
141154 * @param string $encoding The encoding to use, defaults to "UTF-8".
142155 */
143- public static function replaceSubstring (string $ string , string $ replacement , int $ start , ?int $ length = null , string $ encoding = 'UTF-8 ' ): string
144- {
156+ public static function replaceSubstring (
157+ string $ string ,
158+ string $ replacement ,
159+ int $ start ,
160+ int |null $ length = null ,
161+ string $ encoding = 'UTF-8 ' ,
162+ ): string {
145163 $ stringLength = mb_strlen ($ string , $ encoding );
146164
147165 if ($ start < 0 ) {
@@ -160,7 +178,9 @@ public static function replaceSubstring(string $string, string $replacement, int
160178 $ length = $ stringLength - $ start ;
161179 }
162180
163- return mb_substr ($ string , 0 , $ start , $ encoding ) . $ replacement . mb_substr ($ string , $ start + $ length , $ stringLength - $ start - $ length , $ encoding );
181+ return mb_substr ($ string , 0 , $ start , $ encoding )
182+ . $ replacement
183+ . mb_substr ($ string , $ start + $ length , $ stringLength - $ start - $ length , $ encoding );
164184 }
165185
166186 /**
@@ -172,22 +192,9 @@ public static function replaceSubstring(string $string, string $replacement, int
172192 *
173193 * @return bool Returns true if first input starts with second input, false otherwise.
174194 */
175- public static function startsWith (string $ input , ? string $ with ): bool
195+ public static function startsWith (string $ input , string | null $ with ): bool
176196 {
177- if ($ with === null ) {
178- return true ;
179- }
180-
181- if (function_exists ('\str_starts_with ' )) {
182- return str_starts_with ($ input , $ with );
183- }
184-
185- $ bytes = self ::byteLength ($ with );
186- if ($ bytes === 0 ) {
187- return true ;
188- }
189-
190- return strncmp ($ input , $ with , $ bytes ) === 0 ;
197+ return $ with === null || str_starts_with ($ input , $ with );
191198 }
192199
193200 /**
@@ -199,16 +206,15 @@ public static function startsWith(string $input, ?string $with): bool
199206 *
200207 * @return bool Returns true if first input starts with second input, false otherwise.
201208 */
202- public static function startsWithIgnoringCase (string $ input , ? string $ with ): bool
209+ public static function startsWithIgnoringCase (string $ input , string | null $ with ): bool
203210 {
204211 $ bytes = self ::byteLength ($ with );
212+
205213 if ($ bytes === 0 ) {
206214 return true ;
207215 }
208216
209- /**
210- * @psalm-suppress PossiblyNullArgument
211- */
217+ /** @psalm-suppress PossiblyNullArgument */
212218 return self ::lowercase (self ::substring ($ input , 0 , $ bytes , '8bit ' )) === self ::lowercase ($ with );
213219 }
214220
@@ -221,27 +227,9 @@ public static function startsWithIgnoringCase(string $input, ?string $with): boo
221227 *
222228 * @return bool Returns true if first input ends with second input, false otherwise.
223229 */
224- public static function endsWith (string $ input , ? string $ with ): bool
230+ public static function endsWith (string $ input , string | null $ with ): bool
225231 {
226- if ($ with === null ) {
227- return true ;
228- }
229-
230- if (function_exists ('\str_ends_with ' )) {
231- return str_ends_with ($ input , $ with );
232- }
233-
234- $ bytes = self ::byteLength ($ with );
235- if ($ bytes === 0 ) {
236- return true ;
237- }
238-
239- // Warning check, see https://php.net/manual/en/function.substr-compare.php#refsect1-function.substr-compare-returnvalues
240- if (self ::byteLength ($ input ) < $ bytes ) {
241- return false ;
242- }
243-
244- return substr_compare ($ input , $ with , -$ bytes , $ bytes ) === 0 ;
232+ return $ with === null || str_ends_with ($ input , $ with );
245233 }
246234
247235 /**
@@ -253,16 +241,15 @@ public static function endsWith(string $input, ?string $with): bool
253241 *
254242 * @return bool Returns true if first input ends with second input, false otherwise.
255243 */
256- public static function endsWithIgnoringCase (string $ input , ? string $ with ): bool
244+ public static function endsWithIgnoringCase (string $ input , string | null $ with ): bool
257245 {
258246 $ bytes = self ::byteLength ($ with );
247+
259248 if ($ bytes === 0 ) {
260249 return true ;
261250 }
262251
263- /**
264- * @psalm-suppress PossiblyNullArgument
265- */
252+ /** @psalm-suppress PossiblyNullArgument */
266253 return self ::lowercase (mb_substr ($ input , -$ bytes , mb_strlen ($ input , '8bit ' ), '8bit ' )) === self ::lowercase ($ with );
267254 }
268255
@@ -433,9 +420,7 @@ public static function uppercaseFirstCharacterInEachWord(string $string, string
433420 $ words = preg_split ('/\s/u ' , $ string , -1 , PREG_SPLIT_NO_EMPTY );
434421
435422 $ wordsWithUppercaseFirstCharacter = array_map (
436- static function (string $ word ) use ($ encoding ) {
437- return self ::uppercaseFirstCharacter ($ word , $ encoding );
438- },
423+ static fn (string $ word ) => self ::uppercaseFirstCharacter ($ word , $ encoding ),
439424 $ words
440425 );
441426
0 commit comments