diff --git a/README.md b/README.md index ade69773..c8602c8e 100644 --- a/README.md +++ b/README.md @@ -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; diff --git a/src/Php80/Php80.php b/src/Php80/Php80.php index 77cc230b..557ace2b 100644 --- a/src/Php80/Php80.php +++ b/src/Php80/Php80.php @@ -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); + } } diff --git a/src/Php80/README.md b/src/Php80/README.md index c52acfbb..e569c113 100644 --- a/src/Php80/README.md +++ b/src/Php80/README.md @@ -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). diff --git a/src/Php80/bootstrap.php b/src/Php80/bootstrap.php index f239357b..6b1d27d9 100644 --- a/src/Php80/bootstrap.php +++ b/src/Php80/bootstrap.php @@ -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); } diff --git a/tests/Php80/Php80Test.php b/tests/Php80/Php80Test.php index fab292cb..ba5233d3 100644 --- a/tests/Php80/Php80Test.php +++ b/tests/Php80/Php80Test.php @@ -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(