Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feature #35936 [String] Add AbstractString::containsAny() (nicolas-gr…
…ekas)

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
-------

de79ae7 [String] Add AbstractString::containsAny()
  • Loading branch information
fabpot committed Mar 16, 2020
2 parents 313d865 + de79ae7 commit 34583b7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Symfony/Component/String/AbstractString.php
Expand Up @@ -254,6 +254,14 @@ public function collapseWhitespace(): self
return $str;
}

/**
* @param string|string[] $needle
*/
public function containsAny($needle): bool
{
return null !== $this->indexOf($needle);
}

/**
* @param string|string[] $suffix
*/
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/String/CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
* added the `s()` helper method to get either an `UnicodeString` or `ByteString` instance,
depending of the input string UTF-8 compliancy
* added `$cut` parameter to `Symfony\Component\String\AbstractString::truncate()`
* added `AbstractString::containsAny()`

5.0.0
-----
Expand Down
26 changes: 24 additions & 2 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Expand Up @@ -55,6 +55,26 @@ public static function provideBytesAt(): array
];
}

/**
* @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()
{
$expected = ['hello', 'world'];
Expand Down Expand Up @@ -161,7 +181,7 @@ public static function provideLength(): array
/**
* @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);

Expand All @@ -180,6 +200,7 @@ public static function provideIndexOf(): array
[null, 'abc', 'a', -1],
[null, '123abc', 'B', -3],
[null, '123abc', 'b', 6],
[0, 'abc', ['a', 'e'], 0],
[0, 'abc', 'a', 0],
[1, 'abc', 'b', 1],
[2, 'abc', 'c', 1],
Expand All @@ -191,7 +212,7 @@ public static function provideIndexOf(): array
/**
* @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);

Expand All @@ -208,6 +229,7 @@ public static function provideIndexOfIgnoreCase(): array
[null, 'abc', 'a', -1],
[null, 'abc', 'A', -1],
[null, '123abc', 'B', 6],
[0, 'ABC', ['a', 'e'], 0],
[0, 'ABC', 'a', 0],
[0, 'ABC', 'A', 0],
[1, 'ABC', 'b', 0],
Expand Down

0 comments on commit 34583b7

Please sign in to comment.