Skip to content

Commit

Permalink
feature #229 [PHP 8.0] add str_contains function (IonBazan)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.15-dev branch.

Discussion
----------

[PHP 8.0] add `str_contains` function

Waiting for https://wiki.php.net/rfc/str_contains RFC.

Commits
-------

e80a892 add `str_contains` function
  • Loading branch information
nicolas-grekas committed Mar 3, 2020
2 parents 8ba9f43 + e80a892 commit 8f1b8e9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Polyfills are provided for:
introduced in PHP 7.4;
- the `fdiv` function introduced in PHP 8.0;
- the `preg_last_error_msg` function introduced in PHP 8.0;
- the `str_contains` function introduced in PHP 8.0;
- the `ValueError` class introduced in PHP 8.0;
- the `FILTER_VALIDATE_BOOL` constant introduced in PHP 8.0;

Expand Down
5 changes: 5 additions & 0 deletions src/Php80/Php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public static function preg_last_error_msg(): string
return 'Unknown error';
}
}

public static function str_contains(string $haystack, string $needle): bool
{
return '' === $needle || false !== strpos($haystack, $needle);
}
}
1 change: 1 addition & 0 deletions src/Php80/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This component provides features added to PHP 8.0 core:
- `ValueError` class
- `FILTER_VALIDATE_BOOL` constant
- [`preg_last_error_msg`](https://php.net/preg_last_error_msg)
- [`str_contains`](https://php.net/str_contains)

More information can be found in the
[main Polyfill README](https://github.com/symfony/polyfill/blob/master/README.md).
Expand Down
4 changes: 4 additions & 0 deletions src/Php80/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ function fdiv(float $dividend, float $divisor): float { return p\Php80::fdiv($di
function preg_last_error_msg(): string { return p\Php80::preg_last_error_msg(); }
}

if (!function_exists('str_contains')) {
function str_contains(string $haystack, string $needle): bool { return p\Php80::str_contains($haystack, $needle); }
}

if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) {
define('FILTER_VALIDATE_BOOL', FILTER_VALIDATE_BOOLEAN);
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Php80/Php80Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ public function testPregMalformedUtf8Offset()
);
}

/**
* @covers \Symfony\Polyfill\Php80\Php80::str_contains
*/
public function testStrContains()
{
$this->assertTrue(str_contains('abc', ''));
$this->assertTrue(str_contains('abc', 'a'));
$this->assertTrue(str_contains('abc', 'bc'));
$this->assertTrue(str_contains('abc', 'abc'));
$this->assertTrue(str_contains('한국어', '국'));
$this->assertTrue(str_contains('한국어', ''));
$this->assertTrue(str_contains('', ''));
$this->assertFalse(str_contains('abc', 'd'));
$this->assertFalse(str_contains('abc', 'abcd'));
$this->assertFalse(str_contains('DÉJÀ', 'à'));
$this->assertFalse(str_contains('a', 'à'));
}

public function fdivProvider()
{
return array(
Expand Down

0 comments on commit 8f1b8e9

Please sign in to comment.