Skip to content

Commit 6e7d13b

Browse files
committed
add str_contains function
1 parent 5a095c9 commit 6e7d13b

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Polyfills are provided for:
4444
- the `get_mangled_object_vars`, `mb_str_split` and `password_algos` functions
4545
introduced in PHP 7.4;
4646
- the `fdiv` function introduced in PHP 8.0;
47+
- the `str_contains` function introduced in PHP 8.0;
4748
- the `ValueError` class introduced in PHP 8.0;
4849
- the `FILTER_VALIDATE_BOOL` constant introduced in PHP 8.0;
4950

src/Php80/Php80.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public static function fdiv($dividend, $divisor)
2626
return (float) @($dividend / $divisor);
2727
}
2828

29+
public static function str_contains($haystack, $needle)
30+
{
31+
return '' === $needle || false !== \strpos($haystack, $needle);
32+
}
33+
2934
private static function floatArg($value, $caller, $pos)
3035
{
3136
if (\is_float($value)) {

src/Php80/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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) (only for PHP 7.0+)
6+
- [`fdiv`](https://php.net/fdiv) function (only for PHP 7.0+)
7+
- [`str_contains`](https://php.net/str_contains) function
78
- `ValueError` class
89
- `FILTER_VALIDATE_BOOL` constant
910

src/Php80/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
function fdiv($dividend, $divisor) { return p\Php80::fdiv($dividend, $divisor); }
1717
}
1818

19+
if (!function_exists('str_contains')) {
20+
function str_contains($haystack, $needle) { return p\Php80::str_contains($haystack, $needle); }
21+
}
22+
1923
if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) {
2024
define('FILTER_VALIDATE_BOOL', FILTER_VALIDATE_BOOLEAN);
2125
}

tests/Php80/Php80Test.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515

1616
/**
1717
* @author Ion Bazan <[email protected]>
18+
*
19+
* @requires PHP 7.0
1820
*/
1921
class Php80Test extends TestCase
2022
{
2123
/**
22-
* @requires PHP 7.0
2324
* @covers \Symfony\Polyfill\Php80\Php80::fdiv
2425
* @dataProvider fdivProvider
2526
*/
@@ -32,7 +33,6 @@ public function testFdiv($expected, $divident, $divisor)
3233
}
3334

3435
/**
35-
* @requires PHP 7.0
3636
* @covers \Symfony\Polyfill\Php80\Php80::fdiv
3737
* @dataProvider nanFdivProvider
3838
*/
@@ -42,7 +42,6 @@ public function testFdivNan($divident, $divisor)
4242
}
4343

4444
/**
45-
* @requires PHP 7.0
4645
* @covers \Symfony\Polyfill\Php80\Php80::fdiv
4746
* @dataProvider invalidFloatProvider
4847
*/
@@ -52,15 +51,27 @@ public function testFdivTypeError($divident, $divisor)
5251
fdiv($divident, $divisor);
5352
}
5453

55-
/**
56-
* @requires PHP 7.0
57-
*/
5854
public function testFilterValidateBool()
5955
{
6056
$this->assertTrue(\defined('FILTER_VALIDATE_BOOL'));
6157
$this->assertSame(FILTER_VALIDATE_BOOLEAN, FILTER_VALIDATE_BOOL);
6258
}
6359

60+
public function testStrContains()
61+
{
62+
$this->assertTrue(str_contains('abc', ''));
63+
$this->assertTrue(str_contains('abc', 'a'));
64+
$this->assertTrue(str_contains('abc', 'bc'));
65+
$this->assertTrue(str_contains('abc', 'abc'));
66+
$this->assertTrue(str_contains('abc', 'abc'));
67+
$this->assertTrue(str_contains('한국어', ''));
68+
$this->assertTrue(str_contains('한국어', ''));
69+
$this->assertTrue(str_contains('', ''));
70+
$this->assertFalse(str_contains('abc', 'd'));
71+
$this->assertFalse(str_contains('abc', 'abcd'));
72+
$this->assertFalse(str_contains('DÉJÀ', 'à'));
73+
}
74+
6475
public function fdivProvider()
6576
{
6677
return array(

0 commit comments

Comments
 (0)