From 3a47be914ab89a2a28bced5f118707d6ee60cffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Mon, 24 Jul 2023 20:06:56 +0800 Subject: [PATCH 1/3] Fixed bug that `Str::contains` with `null` will case the error result. (#5969) * Fixed bug that `Str::contains` with `null` will case the wrong result. * Update --- src/Str.php | 3 ++- tests/StrTest.php | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Str.php b/src/Str.php index 97efd16..1e72bb4 100644 --- a/src/Str.php +++ b/src/Str.php @@ -211,7 +211,8 @@ public static function charAt($subject, $index) public static function contains(string $haystack, $needles): bool { foreach ((array) $needles as $needle) { - if ($needle !== '' && str_contains($haystack, (string) $needle)) { + $needle = (string) $needle; + if ($needle !== '' && str_contains($haystack, $needle)) { return true; } } diff --git a/tests/StrTest.php b/tests/StrTest.php index 2ba62d5..bccd445 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -245,4 +245,12 @@ public function testIsMatch() $this->assertTrue(Str::isMatch(['/laravel/i', '/laravel!(.*)/'], 'Hello, Laravel!')); $this->assertTrue(Str::isMatch(['/^[a-zA-Z,!]+$/', '/^(.*(.*(.*)))/'], 'Hello, Laravel!')); } + + public function testContains() + { + $this->assertTrue(Str::contains('Hperfy', ['H'])); + $this->assertFalse(Str::contains('Hperfy', [''])); + $this->assertFalse(Str::contains('Hperfy', [null])); + $this->assertFalse(Str::contains('Hperfy', null)); + } } From 9f56aac76b96999382a8f714f58a071475cb1150 Mon Sep 17 00:00:00 2001 From: guandeng Date: Tue, 25 Jul 2023 09:40:20 +0800 Subject: [PATCH 2/3] Fixed bug that `Str::startsWith` and `Str::endsWith` will cause the error result when the `$needles` is `[null]`. (#5970) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 范冠登 Co-authored-by: 李铭昕 <715557344@qq.com> --- src/Str.php | 6 ++++-- tests/StrTest.php | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Str.php b/src/Str.php index 1e72bb4..1e0a026 100644 --- a/src/Str.php +++ b/src/Str.php @@ -247,7 +247,8 @@ public static function containsAll($haystack, array $needles) public static function endsWith(string $haystack, $needles) { foreach ((array) $needles as $needle) { - if ($needle !== '' && str_ends_with($haystack, (string) $needle)) { + $needle = (string) $needle; + if ($needle !== '' && str_ends_with($haystack, $needle)) { return true; } } @@ -704,7 +705,8 @@ public static function snake(string $value, string $delimiter = '_'): string public static function startsWith(string $haystack, $needles): bool { foreach ((array) $needles as $needle) { - if ($needle !== '' && str_starts_with($haystack, (string) $needle)) { + $needle = (string) $needle; + if ($needle !== '' && str_starts_with($haystack, $needle)) { return true; } } diff --git a/tests/StrTest.php b/tests/StrTest.php index bccd445..fdce697 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -169,6 +169,9 @@ public function testStartsWith() $this->assertFalse(Str::startsWith('hyperf.wiki', ['http://', 'https://'])); $this->assertTrue(Str::startsWith('http://www.hyperf.io', 'http://')); $this->assertTrue(Str::startsWith('https://www.hyperf.io', ['http://', 'https://'])); + $this->assertFalse(Str::startsWith('Hyperf', [''])); + $this->assertFalse(Str::startsWith('Hyperf', [null])); + $this->assertFalse(Str::startsWith('Hyperf', null)); } public function testStripTags() @@ -248,9 +251,17 @@ public function testIsMatch() public function testContains() { - $this->assertTrue(Str::contains('Hperfy', ['H'])); - $this->assertFalse(Str::contains('Hperfy', [''])); - $this->assertFalse(Str::contains('Hperfy', [null])); - $this->assertFalse(Str::contains('Hperfy', null)); + $this->assertTrue(Str::contains('Hyperf', ['H'])); + $this->assertFalse(Str::contains('Hyperf', [''])); + $this->assertFalse(Str::contains('Hyperf', [null])); + $this->assertFalse(Str::contains('Hyperf', null)); + } + + public function testEndsWith() + { + $this->assertTrue(Str::endsWith('Hyperf', ['f'])); + $this->assertFalse(Str::endsWith('Hyperf', [''])); + $this->assertFalse(Str::endsWith('Hyperf', [null])); + $this->assertFalse(Str::endsWith('Hyperf', null)); } } From 1bcea1466a87267c780cd67df05de0127c237dcd Mon Sep 17 00:00:00 2001 From: guandeng Date: Tue, 25 Jul 2023 20:44:39 +0800 Subject: [PATCH 3/3] Added `Str::containsIgnoreCase()` (#5971) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 李铭昕 <715557344@qq.com> --- src/Str.php | 28 ++++++++++++++++++++++++---- tests/StrTest.php | 7 +++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Str.php b/src/Str.php index 1e0a026..f86610c 100644 --- a/src/Str.php +++ b/src/Str.php @@ -208,8 +208,12 @@ public static function charAt($subject, $index) * * @param array|string $needles */ - public static function contains(string $haystack, $needles): bool + public static function contains(string $haystack, mixed $needles, bool $ignoreCase = false): bool { + if ($ignoreCase) { + return static::containsIgnoreCase($haystack, $needles); + } + foreach ((array) $needles as $needle) { $needle = (string) $needle; if ($needle !== '' && str_contains($haystack, $needle)) { @@ -220,17 +224,33 @@ public static function contains(string $haystack, $needles): bool return false; } + /** + * Determine if a given string contains a given substring regardless of case sensitivity. + * + * @param array|string $needles + */ + public static function containsIgnoreCase(string $haystack, $needles): bool + { + foreach ((array) $needles as $needle) { + $needle = (string) $needle; + if ($needle !== '' && stripos($haystack, $needle) !== false) { + return true; + } + } + + return false; + } + /** * Determine if a given string contains all array values. * - * @param string $haystack * @param string[] $needles * @return bool */ - public static function containsAll($haystack, array $needles) + public static function containsAll(string $haystack, array $needles, bool $ignoreCase = false) { foreach ($needles as $needle) { - if (! static::contains($haystack, $needle)) { + if (! static::contains($haystack, $needle, $ignoreCase)) { return false; } } diff --git a/tests/StrTest.php b/tests/StrTest.php index fdce697..8bce21a 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -251,6 +251,7 @@ public function testIsMatch() public function testContains() { + $this->assertTrue(Str::contains('Hyperf', ['h'], true)); $this->assertTrue(Str::contains('Hyperf', ['H'])); $this->assertFalse(Str::contains('Hyperf', [''])); $this->assertFalse(Str::contains('Hyperf', [null])); @@ -264,4 +265,10 @@ public function testEndsWith() $this->assertFalse(Str::endsWith('Hyperf', [null])); $this->assertFalse(Str::endsWith('Hyperf', null)); } + + public function testContainsAll() + { + $this->assertTrue(Str::containsAll('Hyperf', ['h'], true)); + $this->assertFalse(Str::containsAll('Hyperf', ['h'])); + } }