Skip to content

Commit 69e4465

Browse files
committed
add str_contains function
1 parent 8b8954b commit 69e4465

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Polyfills are provided for:
4545
introduced in PHP 7.4;
4646
- the `fdiv` function introduced in PHP 8.0;
4747
- the `preg_last_error_msg` function introduced in PHP 8.0;
48+
- the `str_contains` function introduced in PHP 8.0;
4849
- the `ValueError` class introduced in PHP 8.0;
4950
- the `FILTER_VALIDATE_BOOL` constant introduced in PHP 8.0;
5051

src/Php80/Php80.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public static function preg_last_error_msg()
4949
}
5050
}
5151

52+
public static function str_contains($haystack, $needle)
53+
{
54+
return '' === $needle || false !== \strpos($haystack, $needle);
55+
}
56+
5257
private static function floatArg($value, $caller, $pos)
5358
{
5459
if (\is_float($value)) {

src/Php80/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
Symfony Polyfill / Php80
22
========================
33

4-
This component provides functions added to PHP 8.0 core:
4+
This component provides features added to PHP 8.0 core:
55

6-
- [`fdiv`](https://php.net/fdiv)
6+
- [`fdiv`](https://php.net/fdiv) function
7+
- [`preg_last_error_msg`](https://php.net/preg_last_error_msg) function
8+
- [`str_contains`](https://php.net/str_contains) function
79
- `ValueError` class
810
- `FILTER_VALIDATE_BOOL` constant
9-
- [`preg_last_error_msg`](https://php.net/preg_last_error_msg)
1011

1112
More information can be found in the
1213
[main Polyfill README](https://github.com/symfony/polyfill/blob/master/README.md).

src/Php80/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ function fdiv($dividend, $divisor) { return p\Php80::fdiv($dividend, $divisor);
2020
function preg_last_error_msg() { return p\Php80::preg_last_error_msg(); }
2121
}
2222

23+
if (!function_exists('str_contains')) {
24+
function str_contains($haystack, $needle) { return p\Php80::str_contains($haystack, $needle); }
25+
}
26+
2327
if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) {
2428
define('FILTER_VALIDATE_BOOL', FILTER_VALIDATE_BOOLEAN);
2529
}

tests/Php80/Php80Test.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ public function testFilterValidateBool()
5959
}
6060

6161
/**
62-
* @covers \Symfony\Polyfill\Php80\Php80::pregLastErrorMsg
62+
* @covers \Symfony\Polyfill\Php80\Php80::preg_last_error_msg
6363
*/
6464
public function testPregNoError()
6565
{
6666
$this->assertSame('No error', preg_last_error_msg());
6767
}
6868

6969
/**
70-
* @covers \Symfony\Polyfill\Php80\Php80::pregLastErrorMsg
70+
* @covers \Symfony\Polyfill\Php80\Php80::preg_last_error_msg
7171
*/
7272
public function testPregMalformedUtfError()
7373
{
@@ -76,7 +76,7 @@ public function testPregMalformedUtfError()
7676
}
7777

7878
/**
79-
* @covers \Symfony\Polyfill\Php80\Php80::pregLastErrorMsg
79+
* @covers \Symfony\Polyfill\Php80\Php80::preg_last_error_msg
8080
*/
8181
public function testPregMalformedUtf8Offset()
8282
{
@@ -87,6 +87,24 @@ public function testPregMalformedUtf8Offset()
8787
);
8888
}
8989

90+
/**
91+
* @covers \Symfony\Polyfill\Php80\Php80::str_contains
92+
*/
93+
public function testStrContains()
94+
{
95+
$this->assertTrue(str_contains('abc', ''));
96+
$this->assertTrue(str_contains('abc', 'a'));
97+
$this->assertTrue(str_contains('abc', 'bc'));
98+
$this->assertTrue(str_contains('abc', 'abc'));
99+
$this->assertTrue(str_contains('abc', 'abc'));
100+
$this->assertTrue(str_contains('한국어', ''));
101+
$this->assertTrue(str_contains('한국어', ''));
102+
$this->assertTrue(str_contains('', ''));
103+
$this->assertFalse(str_contains('abc', 'd'));
104+
$this->assertFalse(str_contains('abc', 'abcd'));
105+
$this->assertFalse(str_contains('DÉJÀ', 'à'));
106+
}
107+
90108
public function fdivProvider()
91109
{
92110
return array(

0 commit comments

Comments
 (0)