[String] Add AbstractString::containsAny()

This commit is contained in:
Nicolas Grekas 2020-03-03 14:08:48 +01:00
parent f632b76824
commit de79ae7f35
3 changed files with 33 additions and 2 deletions

View File

@ -254,6 +254,14 @@ abstract class AbstractString implements \JsonSerializable
return $str; return $str;
} }
/**
* @param string|string[] $needle
*/
public function containsAny($needle): bool
{
return null !== $this->indexOf($needle);
}
/** /**
* @param string|string[] $suffix * @param string|string[] $suffix
*/ */

View File

@ -11,6 +11,7 @@ CHANGELOG
* added the `s()` helper method to get either an `UnicodeString` or `ByteString` instance, * added the `s()` helper method to get either an `UnicodeString` or `ByteString` instance,
depending of the input string UTF-8 compliancy depending of the input string UTF-8 compliancy
* added `$cut` parameter to `Symfony\Component\String\AbstractString::truncate()` * added `$cut` parameter to `Symfony\Component\String\AbstractString::truncate()`
* added `AbstractString::containsAny()`
5.0.0 5.0.0
----- -----

View File

@ -55,6 +55,26 @@ abstract class AbstractAsciiTestCase extends TestCase
]; ];
} }
/**
* @dataProvider provideIndexOf
*/
public function testContainsAny(?int $result, string $string, $needle)
{
$instance = static::createFromString($string);
$this->assertSame(null !== $instance->indexOf($needle), $instance->containsAny($needle));
}
/**
* @dataProvider provideIndexOfIgnoreCase
*/
public function testContainsAnyIgnoreCase(?int $result, string $string, $needle)
{
$instance = static::createFromString($string);
$this->assertSame(null !== $instance->ignoreCase()->indexOf($needle), $instance->ignoreCase()->containsAny($needle));
}
public function testUnwrap() public function testUnwrap()
{ {
$expected = ['hello', 'world']; $expected = ['hello', 'world'];
@ -161,7 +181,7 @@ abstract class AbstractAsciiTestCase extends TestCase
/** /**
* @dataProvider provideIndexOf * @dataProvider provideIndexOf
*/ */
public function testIndexOf(?int $result, string $string, string $needle, int $offset) public function testIndexOf(?int $result, string $string, $needle, int $offset)
{ {
$instance = static::createFromString($string); $instance = static::createFromString($string);
@ -180,6 +200,7 @@ abstract class AbstractAsciiTestCase extends TestCase
[null, 'abc', 'a', -1], [null, 'abc', 'a', -1],
[null, '123abc', 'B', -3], [null, '123abc', 'B', -3],
[null, '123abc', 'b', 6], [null, '123abc', 'b', 6],
[0, 'abc', ['a', 'e'], 0],
[0, 'abc', 'a', 0], [0, 'abc', 'a', 0],
[1, 'abc', 'b', 1], [1, 'abc', 'b', 1],
[2, 'abc', 'c', 1], [2, 'abc', 'c', 1],
@ -191,7 +212,7 @@ abstract class AbstractAsciiTestCase extends TestCase
/** /**
* @dataProvider provideIndexOfIgnoreCase * @dataProvider provideIndexOfIgnoreCase
*/ */
public function testIndexOfIgnoreCase(?int $result, string $string, string $needle, int $offset) public function testIndexOfIgnoreCase(?int $result, string $string, $needle, int $offset)
{ {
$instance = static::createFromString($string); $instance = static::createFromString($string);
@ -208,6 +229,7 @@ abstract class AbstractAsciiTestCase extends TestCase
[null, 'abc', 'a', -1], [null, 'abc', 'a', -1],
[null, 'abc', 'A', -1], [null, 'abc', 'A', -1],
[null, '123abc', 'B', 6], [null, '123abc', 'B', 6],
[0, 'ABC', ['a', 'e'], 0],
[0, 'ABC', 'a', 0], [0, 'ABC', 'a', 0],
[0, 'ABC', 'A', 0], [0, 'ABC', 'A', 0],
[1, 'ABC', 'b', 0], [1, 'ABC', 'b', 0],