From fef1fc87362d92ecc2adbc0724163defccd99fe4 Mon Sep 17 00:00:00 2001 From: Simon Asika Date: Tue, 10 Dec 2024 12:14:11 +0800 Subject: [PATCH] Str for php8.4 #1140 --- packages/utilities/src/Str.php | 12 +- packages/utilities/src/Utf8String.php | 164 +------------ packages/utilities/test/MbUtf8StringTest.php | 243 +++++++++---------- 3 files changed, 126 insertions(+), 293 deletions(-) diff --git a/packages/utilities/src/Str.php b/packages/utilities/src/Str.php index 6f8561753..2db8f5731 100644 --- a/packages/utilities/src/Str.php +++ b/packages/utilities/src/Str.php @@ -8,20 +8,18 @@ /** * The StringHelper class. - * - * @since __DEPLOY_VERSION__ */ class Str { - public const CASE_SENSITIVE = true; + public const true CASE_SENSITIVE = true; - public const CASE_INSENSITIVE = false; + public const false CASE_INSENSITIVE = false; - public const ENCODING_DEFAULT_ISO = 'ISO-8859-1'; + public const string ENCODING_DEFAULT_ISO = 'ISO-8859-1'; - public const ENCODING_UTF8 = 'UTF-8'; + public const string ENCODING_UTF8 = 'UTF-8'; - public const ENCODING_US_ASCII = 'US-ASCII'; + public const string ENCODING_US_ASCII = 'US-ASCII'; public static function getChar(string $string, int $pos, ?string $encoding = null): string { diff --git a/packages/utilities/src/Utf8String.php b/packages/utilities/src/Utf8String.php index 9abf8dd5f..a0f6c8c22 100644 --- a/packages/utilities/src/Utf8String.php +++ b/packages/utilities/src/Utf8String.php @@ -31,18 +31,23 @@ * @method static mixed detectOrder($encodingList = null) * @method static string eregReplace(string $pattern, string $replacement, string $string, string $option = 'msr') * @method static string eregiReplace(string $pattern, string $replacement, string $string, string $option = 'msr') + * @method static string trim(string $string, ?string $characters = null, ?string $encoding = null) + * @method static string ltrim(string $string, ?string $characters = null, ?string $encoding = null) + * @method static string rtrim(string $string, ?string $characters = null, ?string $encoding = null) + * @method static string ucfirst(string $string, ?string $encoding = null) + * @method static string lcfirst(string $string, ?string $encoding = null) * * @since 2.0 */ abstract class Utf8String { - public const CASE_SENSITIVE = true; + public const true CASE_SENSITIVE = true; - public const CASE_INSENSITIVE = false; + public const false CASE_INSENSITIVE = false; - public const ENCODING_DEFAULT_ISO = 'ISO-8859-1'; + public const string ENCODING_DEFAULT_ISO = 'ISO-8859-1'; - public const ENCODING_UTF8 = 'UTF-8'; + public const string ENCODING_UTF8 = 'UTF-8'; public const ENCODING_US_ASCII = 'US-ASCII'; @@ -332,157 +337,6 @@ public static function substrReplace( return implode('', $ar[0]); } - /** - * UTF-8 aware replacement for ltrim() - * - * Strip whitespace (or other characters) from the beginning of a string - * You only need to use this if you are supplying the charlist - * optional arg and it contains UTF-8 characters. Otherwise ltrim will - * work normally on a UTF-8 string - * - * @param string $str The string to be trimmed - * @param string $charlist The optional charlist of additional characters to trim - * - * @return string The trimmed string - * - * @see http://www.php.net/ltrim - * @since 2.0 - */ - public static function ltrim(string $str, ?string $charlist = null): string - { - if ($charlist === null) { - return ltrim($str); - } - - if ($charlist === '') { - return $str; - } - - // quote charlist for use in a characterclass - $charlist = preg_replace('!([\\\\\\-\\]\\[/^])!', '\\\${1}', $charlist); - - return preg_replace('/^[' . $charlist . ']+/u', '', $str); - } - - /** - * UTF-8 aware replacement for rtrim() - * Strip whitespace (or other characters) from the end of a string - * You only need to use this if you are supplying the charlist - * optional arg and it contains UTF-8 characters. Otherwise rtrim will - * work normally on a UTF-8 string - * - * @param string $str The string to be trimmed - * @param string $charlist The optional charlist of additional characters to trim - * - * @return string The trimmed string - * - * @see http://www.php.net/rtrim - * @since 2.0 - */ - public static function rtrim(string $str, ?string $charlist = null): string - { - if ($charlist === null) { - return rtrim($str); - } - - if ($charlist === '') { - return $str; - } - - // quote charlist for use in a characterclass - $charlist = preg_replace('!([\\\\\\-\\]\\[/^])!', '\\\${1}', $charlist); - - return preg_replace('/[' . $charlist . ']+$/u', '', $str); - } - - /** - * UTF-8 aware replacement for trim() - * Strip whitespace (or other characters) from the beginning and end of a string - * Note: you only need to use this if you are supplying the charlist - * optional arg and it contains UTF-8 characters. Otherwise trim will - * work normally on a UTF-8 string - * - * @param string $str The string to be trimmed - * @param string $charlist The optional charlist of additional characters to trim - * - * @return string The trimmed string - * - * @see http://www.php.net/trim - * @since 2.0 - */ - public static function trim(string $str, ?string $charlist = null): string - { - if ($charlist === null) { - return trim($str); - } - - if ($charlist === '') { - return $str; - } - - return static::ltrim(static::rtrim($str, $charlist), $charlist); - } - - /** - * UTF-8 aware alternative to ucfirst - * Make a string's first character uppercase or all words' first character uppercase - * - * @param string $str String to be processed - * @param string|null $encoding - * - * @return string If $delimiter is null, return the string with first character as upper case (if applicable) - * else consider the string of words separated by the delimiter, apply the ucfirst to each words - * and return the string with the new delimiter - * - * @see http://www.php.net/ucfirst - * @since 2.0 - */ - public static function ucfirst(string $str, ?string $encoding = null): string - { - $encoding = $encoding ?? mb_internal_encoding(); - - switch (static::strlen($str, $encoding)) { - case 0: - return ''; - break; - case 1: - return static::strtoupper($str, $encoding); - break; - default: - preg_match('/^(.{1})(.*)$/us', $str, $matches); - - return static::strtoupper($matches[1], $encoding) . $matches[2]; - break; - } - } - - /** - * lcfirst - * - * @param string $str - * @param string|null $encoding - * - * @return string - */ - public static function lcfirst(string $str, ?string $encoding = null): string - { - $encoding = $encoding ?? mb_internal_encoding(); - - switch (static::strlen($str, $encoding)) { - case 0: - return ''; - break; - case 1: - return static::strtolower($str, $encoding); - break; - default: - preg_match('/^(.{1})(.*)$/us', $str, $matches); - - return static::strtolower($matches[1], $encoding) . $matches[2]; - break; - } - } - /** * UTF-8 aware alternative to ucwords * Uppercase the first character of each word in a string diff --git a/packages/utilities/test/MbUtf8StringTest.php b/packages/utilities/test/MbUtf8StringTest.php index e616434f2..071ede386 100644 --- a/packages/utilities/test/MbUtf8StringTest.php +++ b/packages/utilities/test/MbUtf8StringTest.php @@ -5,6 +5,7 @@ namespace Windwalker\Utilities\Test; use BadMethodCallException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Windwalker\Utilities\Utf8String; @@ -32,33 +33,33 @@ public function testCallStatic() /** * Test... * - * @param string $string @todo - * @param boolean $expected @todo + * @param string $string @todo + * @param boolean $expected @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('isAsciiProvider')] + #[DataProvider('isAsciiProvider')] public function testIsAscii($string, $expected) { $this->assertEquals( $expected, - Utf8String::isAscii($string) + Utf8String::isAscii($string), ); } /** * Test... * - * @param string $expect @todo - * @param string $haystack @todo - * @param string $needle @todo - * @param integer $offset @todo + * @param string $expect @todo + * @param string $haystack @todo + * @param string $needle @todo + * @param integer $offset @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('strposProvider')] + #[DataProvider('strposProvider')] public function testStrpos($expect, $haystack, $needle, $offset = 0) { $actual = Utf8String::strpos($haystack, $needle, $offset); @@ -68,15 +69,15 @@ public function testStrpos($expect, $haystack, $needle, $offset = 0) /** * Test... * - * @param string $expect @todo - * @param string $haystack @todo - * @param string $needle @todo - * @param integer $offset @todo + * @param string $expect @todo + * @param string $haystack @todo + * @param string $needle @todo + * @param integer $offset @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('strrposProvider')] + #[DataProvider('strrposProvider')] public function testStrrpos($expect, $haystack, $needle, $offset = 0) { $actual = Utf8String::strrpos($haystack, $needle, $offset); @@ -86,15 +87,15 @@ public function testStrrpos($expect, $haystack, $needle, $offset = 0) /** * Test... * - * @param string $expect @todo - * @param string $string @todo - * @param string $start @todo - * @param bool|int $length @todo + * @param string $expect @todo + * @param string $string @todo + * @param string $start @todo + * @param bool|int $length @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('substrProvider')] + #[DataProvider('substrProvider')] public function testSubstr($expect, $string, $start, $length = null) { $actual = Utf8String::substr($string, $start, $length); @@ -104,13 +105,13 @@ public function testSubstr($expect, $string, $start, $length = null) /** * Test... * - * @param string $string @todo - * @param string $expect @todo + * @param string $string @todo + * @param string $expect @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('strtolowerProvider')] + #[DataProvider('strtolowerProvider')] public function testStrtolower($string, $expect): void { $actual = Utf8String::strtolower($string); @@ -120,13 +121,13 @@ public function testStrtolower($string, $expect): void /** * Test... * - * @param string $string @todo - * @param string $expect @todo + * @param string $string @todo + * @param string $expect @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('strtoupperProvider')] + #[DataProvider('strtoupperProvider')] public function testStrtoupper($string, $expect): void { $actual = Utf8String::strtoupper($string); @@ -136,13 +137,13 @@ public function testStrtoupper($string, $expect): void /** * Test... * - * @param string $string @todo - * @param string $expect @todo + * @param string $string @todo + * @param string $expect @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('strlenProvider')] + #[DataProvider('strlenProvider')] public function testStrlen($string, $expect): void { $actual = Utf8String::strlen($string); @@ -152,16 +153,16 @@ public function testStrlen($string, $expect): void /** * Test... * - * @param string $search @todo - * @param string $replace @todo - * @param string $subject @todo - * @param integer $count @todo - * @param string $expect @todo + * @param string $search @todo + * @param string $replace @todo + * @param string $subject @todo + * @param integer $count @todo + * @param string $expect @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('strIreplaceProvider')] + #[DataProvider('strIreplaceProvider')] public function testStrIreplace($search, $replace, $subject, $count, $expect): void { $actual = Utf8String::strIreplace($search, $replace, $subject, $count); @@ -171,14 +172,14 @@ public function testStrIreplace($search, $replace, $subject, $count, $expect): v /** * Test... * - * @param string $string @todo - * @param int $split_length @todo - * @param string $expect @todo + * @param string $string @todo + * @param int $split_length @todo + * @param string $expect @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('strSplitProvider')] + #[DataProvider('strSplitProvider')] public function testStrSplit($string, $split_length, $expect): void { $actual = Utf8String::strSplit($string, $split_length); @@ -188,14 +189,14 @@ public function testStrSplit($string, $split_length, $expect): void /** * Test... * - * @param string $string1 @todo - * @param string $string2 @todo - * @param string $expect @todo + * @param string $string1 @todo + * @param string $string2 @todo + * @param string $expect @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('strcasecmpProvider')] + #[DataProvider('strcasecmpProvider')] public function testStrcasecmp($string1, $string2, $expect) { $actual = Utf8String::strcasecmp($string1, $string2); @@ -210,15 +211,15 @@ public function testStrcasecmp($string1, $string2, $expect) /** * Test... * - * @param string $string1 @todo - * @param string $string2 @todo - * @param string $locale @todo - * @param string $expect @todo + * @param string $string1 @todo + * @param string $string2 @todo + * @param string $locale @todo + * @param string $expect @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('strcmpProvider')] + #[DataProvider('strcmpProvider')] public function testStrcmp($string1, $string2, $expect): void { $actual = Utf8String::strcmp($string1, $string2); @@ -233,16 +234,16 @@ public function testStrcmp($string1, $string2, $expect): void /** * Test... * - * @param string $haystack @todo - * @param string $needles @todo - * @param integer $start @todo - * @param integer $len @todo - * @param string $expect @todo + * @param string $haystack @todo + * @param string $needles @todo + * @param integer $start @todo + * @param integer $len @todo + * @param string $expect @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('strcspnProvider')] + #[DataProvider('strcspnProvider')] public function testStrcspn($haystack, $needles, $start, $len, $expect): void { $actual = Utf8String::strcspn($haystack, $needles, $start, $len); @@ -252,14 +253,14 @@ public function testStrcspn($haystack, $needles, $start, $len, $expect): void /** * Test... * - * @param string $haystack @todo - * @param string $needle @todo - * @param string $expect @todo + * @param string $haystack @todo + * @param string $needle @todo + * @param string $expect @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('stristrProvider')] + #[DataProvider('stristrProvider')] public function testStristr($haystack, $needle, $expect): void { $actual = Utf8String::stristr($haystack, $needle); @@ -269,13 +270,13 @@ public function testStristr($haystack, $needle, $expect): void /** * Test... * - * @param string $string @todo - * @param string $expect @todo + * @param string $string @todo + * @param string $expect @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('strrevProvider')] + #[DataProvider('strrevProvider')] public function testStrrev($string, $expect): void { $actual = Utf8String::strrev($string); @@ -285,16 +286,16 @@ public function testStrrev($string, $expect): void /** * Test... * - * @param string $subject @todo - * @param string $mask @todo - * @param integer $start @todo - * @param integer $length @todo - * @param string $expect @todo + * @param string $subject @todo + * @param string $mask @todo + * @param integer $start @todo + * @param integer $length @todo + * @param string $expect @todo * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('strspnProvider')] + #[DataProvider('strspnProvider')] public function testStrspn($subject, $mask, $start, $length, $expect): void { $actual = Utf8String::strspn($subject, $mask, $start, $length); @@ -304,34 +305,24 @@ public function testStrspn($subject, $mask, $start, $length, $expect): void /** * Test... * - * @param string $expect @todo - * @param string $string @todo - * @param string $replacement @todo - * @param integer $start @todo - * @param integer $length @todo + * @param string $expect @todo + * @param string $string @todo + * @param string $replacement @todo + * @param integer $start @todo + * @param integer $length @todo * * @return array * */ - #[\PHPUnit\Framework\Attributes\DataProvider('substrReplaceProvider')] + #[DataProvider('substrReplaceProvider')] public function testSubstrReplace($expect, $string, $replacement, $start, $length): void { $actual = Utf8String::substrReplace($string, $replacement, $start, $length); $this->assertEquals($expect, $actual); } - /** - * Test... - * - * @param string $string @todo - * @param string $charlist @todo - * @param string $expect @todo - * - * @return array - * - */ - #[\PHPUnit\Framework\Attributes\DataProvider('ltrimProvider')] - public function testLtrim($string, $charlist, $expect): void + #[DataProvider('ltrimProvider')] + public function testLtrim(string $string, ?string $charlist, ?string $expect): void { if ($charlist === null) { $actual = Utf8String::ltrim($string); @@ -342,18 +333,8 @@ public function testLtrim($string, $charlist, $expect): void $this->assertEquals($expect, $actual); } - /** - * Test... - * - * @param string $string @todo - * @param string $charlist @todo - * @param string $expect @todo - * - * @return array - * - */ - #[\PHPUnit\Framework\Attributes\DataProvider('rtrimProvider')] - public function testRtrim($string, $charlist, $expect): void + #[DataProvider('rtrimProvider')] + public function testRtrim(string $string, ?string $charlist, string $expect): void { if ($charlist === null) { $actual = Utf8String::rtrim($string); @@ -367,14 +348,14 @@ public function testRtrim($string, $charlist, $expect): void /** * Test... * - * @param string $string @todo - * @param string $charlist @todo - * @param string $expect @todo + * @param string $string @todo + * @param string $charlist @todo + * @param string $expect @todo * * @return array * */ - #[\PHPUnit\Framework\Attributes\DataProvider('trimProvider')] + #[DataProvider('trimProvider')] public function testTrim($string, $charlist, $expect): void { if ($charlist === null) { @@ -389,13 +370,13 @@ public function testTrim($string, $charlist, $expect): void /** * Test... * - * @param string $string @todo - * @param string $expect @todo + * @param string $string @todo + * @param string $expect @todo * * @return array * */ - #[\PHPUnit\Framework\Attributes\DataProvider('ucfirstProvider')] + #[DataProvider('ucfirstProvider')] public function testUcfirst($string, $expect): void { $actual = Utf8String::ucfirst($string); @@ -405,13 +386,13 @@ public function testUcfirst($string, $expect): void /** * testLcfirst * - * @param string $string - * @param string $expect + * @param string $string + * @param string $expect * * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('lcfirstProvider')] + #[DataProvider('lcfirstProvider')] public function testLcfirst($string, $expect) { $actual = Utf8String::lcfirst($string); @@ -421,13 +402,13 @@ public function testLcfirst($string, $expect) /** * Test... * - * @param string $string @todo - * @param string $expect @todo + * @param string $string @todo + * @param string $expect @todo * * @return array * */ - #[\PHPUnit\Framework\Attributes\DataProvider('ucwordsProvider')] + #[DataProvider('ucwordsProvider')] public function testUcwords($string, $expect): void { $actual = Utf8String::ucwords($string); @@ -443,7 +424,7 @@ public function testUcwords($string, $expect): void * @param $caseSensitive * */ - #[\PHPUnit\Framework\Attributes\DataProvider('substrCountProvider')] + #[DataProvider('substrCountProvider')] public function testSubstrCount($string, $search, $expected, $caseSensitive) { self::assertEquals($expected, Utf8String::substrCount($string, $search, $caseSensitive)); @@ -452,15 +433,15 @@ public function testSubstrCount($string, $search, $expected, $caseSensitive) /** * Test... * - * @param string $source @todo - * @param string $from_encoding @todo - * @param string $to_encoding @todo - * @param string $expect @todo + * @param string $source @todo + * @param string $from_encoding @todo + * @param string $to_encoding @todo + * @param string $expect @todo * * @return array * */ - #[\PHPUnit\Framework\Attributes\DataProvider('convertEncodingProvider')] + #[DataProvider('convertEncodingProvider')] public function testConvertEncoding($source, $from_encoding, $to_encoding, $expect): void { $actual = Utf8String::convertEncoding($source, $from_encoding, $to_encoding); @@ -470,13 +451,13 @@ public function testConvertEncoding($source, $from_encoding, $to_encoding, $expe /** * Test... * - * @param string $string @todo - * @param string $expect @todo + * @param string $string @todo + * @param string $expect @todo * * @return array * */ - #[\PHPUnit\Framework\Attributes\DataProvider('isUtf8Provider')] + #[DataProvider('isUtf8Provider')] public function testValid($string, $expect): void { $actual = Utf8String::isUtf8($string); @@ -486,13 +467,13 @@ public function testValid($string, $expect): void /** * Test... * - * @param string $string @todo - * @param string $expect @todo + * @param string $string @todo + * @param string $expect @todo * * @return array * */ - #[\PHPUnit\Framework\Attributes\DataProvider('unicodeToUtf8Provider')] + #[DataProvider('unicodeToUtf8Provider')] public function testUnicodeToUtf8($string, $expect): void { $actual = Utf8String::unicodeToUtf8($string); @@ -502,13 +483,13 @@ public function testUnicodeToUtf8($string, $expect): void /** * Test... * - * @param string $string @todo - * @param string $expect @todo + * @param string $string @todo + * @param string $expect @todo * * @return array * */ - #[\PHPUnit\Framework\Attributes\DataProvider('unicodeToUtf16Provider')] + #[DataProvider('unicodeToUtf16Provider')] public function testUnicodeToUtf16($string, $expect): void { $actual = Utf8String::unicodeToUtf16($string); @@ -518,13 +499,13 @@ public function testUnicodeToUtf16($string, $expect): void /** * Test... * - * @param string $string @todo - * @param string $expect @todo + * @param string $string @todo + * @param string $expect @todo * * @return array * */ - #[\PHPUnit\Framework\Attributes\DataProvider('isUtf8Provider')] + #[DataProvider('isUtf8Provider')] public function testCompliant($string, $expect): void { $actual = Utf8String::compliant($string); @@ -537,7 +518,7 @@ public function testCompliant($string, $expect): void * @return void * */ - #[\PHPUnit\Framework\Attributes\DataProvider('providerTestShuffle')] + #[DataProvider('providerTestShuffle')] public function testShuffle($string) { $result = Utf8String::shuffle($string); @@ -548,9 +529,9 @@ public function testShuffle($string) $len = mb_strlen($string); for ($i = 0; $i < $len; $i++) { - $char = mb_substr($string, $i, 1); + $char = mb_substr($string, $i, 1); $countBefore = mb_substr_count($string, $char); - $countAfter = mb_substr_count($result, $char); + $countAfter = mb_substr_count($result, $char); self::assertEquals($countBefore, $countAfter); } @@ -567,7 +548,7 @@ public function testToAscii(): void 5 => 101, 6 => 114, ], - Utf8String::toAscii('Flower') + Utf8String::toAscii('Flower'), ); } }