From 355356407bfd99a16fa7df52aca2b3a765490816 Mon Sep 17 00:00:00 2001 From: Kai Sassnowski Date: Mon, 10 May 2021 10:48:56 +0200 Subject: [PATCH] Add option to ignore case in `Str::contains` and `Str::containsAll` --- src/Illuminate/Support/Str.php | 16 ++++++++-- tests/Support/SupportStrTest.php | 53 ++++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index d023a446b74e..7a8cc8b4460c 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -176,10 +176,16 @@ public static function camel($value) * * @param string $haystack * @param string|string[] $needles + * @param bool $ignoreCase * @return bool */ - public static function contains($haystack, $needles) + public static function contains($haystack, $needles, $ignoreCase = false) { + if ($ignoreCase) { + $haystack = mb_strtolower($haystack); + $needles = array_map('mb_strtolower', (array) $needles); + } + foreach ((array) $needles as $needle) { if ($needle !== '' && mb_strpos($haystack, $needle) !== false) { return true; @@ -194,10 +200,16 @@ public static function contains($haystack, $needles) * * @param string $haystack * @param string[] $needles + * @param bool $ignoreCase * @return bool */ - public static function containsAll($haystack, array $needles) + public static function containsAll($haystack, array $needles, $ignoreCase = false) { + if ($ignoreCase) { + $haystack = mb_strtolower($haystack); + $needles = array_map('mb_strtolower', $needles); + } + foreach ($needles as $needle) { if (! static::contains($haystack, $needle)) { return false; diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index e9e81d373cef..0973c2a77196 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -176,23 +176,20 @@ public function testStrAfterLast() $this->assertSame('foo', Str::afterLast('----foo', '---')); } - public function testStrContains() + /** + * @dataProvider strContainsProvider + */ + public function testStrContains($haystack, $needles, $expected, $ignoreCase = false) { - $this->assertTrue(Str::contains('taylor', 'ylo')); - $this->assertTrue(Str::contains('taylor', 'taylor')); - $this->assertTrue(Str::contains('taylor', ['ylo'])); - $this->assertTrue(Str::contains('taylor', ['xxx', 'ylo'])); - $this->assertFalse(Str::contains('taylor', 'xxx')); - $this->assertFalse(Str::contains('taylor', ['xxx'])); - $this->assertFalse(Str::contains('taylor', '')); - $this->assertFalse(Str::contains('', '')); + $this->assertEquals($expected, Str::contains($haystack, $needles, $ignoreCase)); } - public function testStrContainsAll() + /** + * @dataProvider strContainsAllProvider + */ + public function testStrContainsAll($haystack, $needles, $expected, $ignoreCase = false) { - $this->assertTrue(Str::containsAll('taylor otwell', ['taylor', 'otwell'])); - $this->assertTrue(Str::containsAll('taylor otwell', ['taylor'])); - $this->assertFalse(Str::containsAll('taylor otwell', ['taylor', 'xxx'])); + $this->assertEquals($expected, Str::containsAll($haystack, $needles, $ignoreCase)); } public function testParseCallback() @@ -545,6 +542,36 @@ public function invalidUuidList() ]; } + public function strContainsProvider() + { + return [ + ['Taylor', 'ylo', true, true], + ['Taylor', 'ylo', true, false], + ['Taylor', 'taylor', true, true], + ['Taylor', 'taylor', false, false], + ['Taylor', ['ylo'], true, true], + ['Taylor', ['ylo'], true, false], + ['Taylor', ['xxx', 'ylo'], true, true], + ['Taylor', ['xxx', 'ylo'], true, false], + ['Taylor', 'xxx', false], + ['Taylor', ['xxx'], false], + ['Taylor', '', false], + ['', '', false], + ]; + } + + public function strContainsAllProvider() + { + return [ + ['Taylor Otwell', ['taylor', 'otwell'], false, false], + ['Taylor Otwell', ['taylor', 'otwell'], true, true], + ['Taylor Otwell', ['taylor'], false, false], + ['Taylor Otwell', ['taylor'], true, true], + ['Taylor Otwell', ['taylor', 'xxx'], false, false], + ['Taylor Otwell', ['taylor', 'xxx'], false, true], + ]; + } + public function testMarkdown() { $this->assertSame("

hello world

\n", Str::markdown('*hello world*'));