Skip to content

Commit bc54ad9

Browse files
authored
Add option to ignore case in Str::contains and Str::containsAll (#37330)
1 parent 1e5662a commit bc54ad9

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

src/Illuminate/Support/Str.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,16 @@ public static function camel($value)
176176
*
177177
* @param string $haystack
178178
* @param string|string[] $needles
179+
* @param bool $ignoreCase
179180
* @return bool
180181
*/
181-
public static function contains($haystack, $needles)
182+
public static function contains($haystack, $needles, $ignoreCase = false)
182183
{
184+
if ($ignoreCase) {
185+
$haystack = mb_strtolower($haystack);
186+
$needles = array_map('mb_strtolower', (array) $needles);
187+
}
188+
183189
foreach ((array) $needles as $needle) {
184190
if ($needle !== '' && mb_strpos($haystack, $needle) !== false) {
185191
return true;
@@ -194,10 +200,16 @@ public static function contains($haystack, $needles)
194200
*
195201
* @param string $haystack
196202
* @param string[] $needles
203+
* @param bool $ignoreCase
197204
* @return bool
198205
*/
199-
public static function containsAll($haystack, array $needles)
206+
public static function containsAll($haystack, array $needles, $ignoreCase = false)
200207
{
208+
if ($ignoreCase) {
209+
$haystack = mb_strtolower($haystack);
210+
$needles = array_map('mb_strtolower', $needles);
211+
}
212+
201213
foreach ($needles as $needle) {
202214
if (! static::contains($haystack, $needle)) {
203215
return false;

tests/Support/SupportStrTest.php

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,23 +184,20 @@ public function testStrAfterLast()
184184
$this->assertSame('foo', Str::afterLast('----foo', '---'));
185185
}
186186

187-
public function testStrContains()
187+
/**
188+
* @dataProvider strContainsProvider
189+
*/
190+
public function testStrContains($haystack, $needles, $expected, $ignoreCase = false)
188191
{
189-
$this->assertTrue(Str::contains('taylor', 'ylo'));
190-
$this->assertTrue(Str::contains('taylor', 'taylor'));
191-
$this->assertTrue(Str::contains('taylor', ['ylo']));
192-
$this->assertTrue(Str::contains('taylor', ['xxx', 'ylo']));
193-
$this->assertFalse(Str::contains('taylor', 'xxx'));
194-
$this->assertFalse(Str::contains('taylor', ['xxx']));
195-
$this->assertFalse(Str::contains('taylor', ''));
196-
$this->assertFalse(Str::contains('', ''));
192+
$this->assertEquals($expected, Str::contains($haystack, $needles, $ignoreCase));
197193
}
198194

199-
public function testStrContainsAll()
195+
/**
196+
* @dataProvider strContainsAllProvider
197+
*/
198+
public function testStrContainsAll($haystack, $needles, $expected, $ignoreCase = false)
200199
{
201-
$this->assertTrue(Str::containsAll('taylor otwell', ['taylor', 'otwell']));
202-
$this->assertTrue(Str::containsAll('taylor otwell', ['taylor']));
203-
$this->assertFalse(Str::containsAll('taylor otwell', ['taylor', 'xxx']));
200+
$this->assertEquals($expected, Str::containsAll($haystack, $needles, $ignoreCase));
204201
}
205202

206203
public function testParseCallback()
@@ -553,6 +550,36 @@ public function invalidUuidList()
553550
];
554551
}
555552

553+
public function strContainsProvider()
554+
{
555+
return [
556+
['Taylor', 'ylo', true, true],
557+
['Taylor', 'ylo', true, false],
558+
['Taylor', 'taylor', true, true],
559+
['Taylor', 'taylor', false, false],
560+
['Taylor', ['ylo'], true, true],
561+
['Taylor', ['ylo'], true, false],
562+
['Taylor', ['xxx', 'ylo'], true, true],
563+
['Taylor', ['xxx', 'ylo'], true, false],
564+
['Taylor', 'xxx', false],
565+
['Taylor', ['xxx'], false],
566+
['Taylor', '', false],
567+
['', '', false],
568+
];
569+
}
570+
571+
public function strContainsAllProvider()
572+
{
573+
return [
574+
['Taylor Otwell', ['taylor', 'otwell'], false, false],
575+
['Taylor Otwell', ['taylor', 'otwell'], true, true],
576+
['Taylor Otwell', ['taylor'], false, false],
577+
['Taylor Otwell', ['taylor'], true, true],
578+
['Taylor Otwell', ['taylor', 'xxx'], false, false],
579+
['Taylor Otwell', ['taylor', 'xxx'], false, true],
580+
];
581+
}
582+
556583
public function testMarkdown()
557584
{
558585
$this->assertSame("<p><em>hello world</em></p>\n", Str::markdown('*hello world*'));

0 commit comments

Comments
 (0)