Skip to content

Commit 204076f

Browse files
committed
feature #35936 [String] Add AbstractString::containsAny() (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [String] Add AbstractString::containsAny() | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - We decided to not have a `contains()` method because we didn't know how to handle the case where several needles are passed. But https://wiki.php.net/rfc/str_contains made me reconsider this idea and I think `containsAny()` works great. WDYT? Commits ------- de79ae7f35 [String] Add AbstractString::containsAny()
2 parents 4e24d03 + 2336888 commit 204076f

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

AbstractString.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ public function collapseWhitespace(): self
254254
return $str;
255255
}
256256

257+
/**
258+
* @param string|string[] $needle
259+
*/
260+
public function containsAny($needle): bool
261+
{
262+
return null !== $this->indexOf($needle);
263+
}
264+
257265
/**
258266
* @param string|string[] $suffix
259267
*/

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* added the `s()` helper method to get either an `UnicodeString` or `ByteString` instance,
1212
depending of the input string UTF-8 compliancy
1313
* added `$cut` parameter to `Symfony\Component\String\AbstractString::truncate()`
14+
* added `AbstractString::containsAny()`
1415

1516
5.0.0
1617
-----

Tests/AbstractAsciiTestCase.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ public static function provideBytesAt(): array
5555
];
5656
}
5757

58+
/**
59+
* @dataProvider provideIndexOf
60+
*/
61+
public function testContainsAny(?int $result, string $string, $needle)
62+
{
63+
$instance = static::createFromString($string);
64+
65+
$this->assertSame(null !== $instance->indexOf($needle), $instance->containsAny($needle));
66+
}
67+
68+
/**
69+
* @dataProvider provideIndexOfIgnoreCase
70+
*/
71+
public function testContainsAnyIgnoreCase(?int $result, string $string, $needle)
72+
{
73+
$instance = static::createFromString($string);
74+
75+
$this->assertSame(null !== $instance->ignoreCase()->indexOf($needle), $instance->ignoreCase()->containsAny($needle));
76+
}
77+
5878
public function testUnwrap()
5979
{
6080
$expected = ['hello', 'world'];
@@ -161,7 +181,7 @@ public static function provideLength(): array
161181
/**
162182
* @dataProvider provideIndexOf
163183
*/
164-
public function testIndexOf(?int $result, string $string, string $needle, int $offset)
184+
public function testIndexOf(?int $result, string $string, $needle, int $offset)
165185
{
166186
$instance = static::createFromString($string);
167187

@@ -180,6 +200,7 @@ public static function provideIndexOf(): array
180200
[null, 'abc', 'a', -1],
181201
[null, '123abc', 'B', -3],
182202
[null, '123abc', 'b', 6],
203+
[0, 'abc', ['a', 'e'], 0],
183204
[0, 'abc', 'a', 0],
184205
[1, 'abc', 'b', 1],
185206
[2, 'abc', 'c', 1],
@@ -191,7 +212,7 @@ public static function provideIndexOf(): array
191212
/**
192213
* @dataProvider provideIndexOfIgnoreCase
193214
*/
194-
public function testIndexOfIgnoreCase(?int $result, string $string, string $needle, int $offset)
215+
public function testIndexOfIgnoreCase(?int $result, string $string, $needle, int $offset)
195216
{
196217
$instance = static::createFromString($string);
197218

@@ -208,6 +229,7 @@ public static function provideIndexOfIgnoreCase(): array
208229
[null, 'abc', 'a', -1],
209230
[null, 'abc', 'A', -1],
210231
[null, '123abc', 'B', 6],
232+
[0, 'ABC', ['a', 'e'], 0],
211233
[0, 'ABC', 'a', 0],
212234
[0, 'ABC', 'A', 0],
213235
[1, 'ABC', 'b', 0],

0 commit comments

Comments
 (0)