From 4ff58071e39ffa1ffce3f1d373eab7e629cd2a4f Mon Sep 17 00:00:00 2001 From: Christian Stoyanov Date: Wed, 15 Apr 2020 20:19:27 +0200 Subject: [PATCH 1/3] Add: substrCount to Stringable and Str --- src/Illuminate/Support/Str.php | 20 ++++++++++++++++++++ src/Illuminate/Support/Stringable.php | 13 +++++++++++++ tests/Support/SupportStrTest.php | 14 ++++++++++++++ tests/Support/SupportStringableTest.php | 16 ++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index dd181afd6028..ec943c20e725 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -86,6 +86,7 @@ public static function afterLast($subject, $search) return substr($subject, $position + strlen($search)); } + /** * Transliterate a UTF-8 value to ASCII. * @@ -644,6 +645,24 @@ public static function substr($string, $start, $length = null) return mb_substr($string, $start, $length, 'UTF-8'); } + /** + * Returns the number of substring occurrences. + * + * @param string $haystack + * @param string $needle + * @param int $offset + * @param int|null $length + * @return int + */ + public static function substrCount($haystack, $needle, $offset = 0, $length = null) + { + if ($length) { + return substr_count($haystack, $needle, $offset, $length); + } else { + return substr_count($haystack, $needle, $offset); + } + } + /** * Make a string's first character uppercase. * @@ -712,4 +731,5 @@ public static function createUuidsNormally() { static::$uuidFactory = null; } + } diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index ce1ae1562c56..feee30fa3d6b 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -529,6 +529,19 @@ public function substr($start, $length = null) return new static(Str::substr($this->value, $start, $length)); } + /** + * Returns the number of substring occurrences. + * + * @param string $needle + * @param int|null $offset + * @param int|null $length + * @return int + */ + public function substrCount($needle, $offset = null, $length = null) + { + return Str::substrCount($this->value, $needle, $offset, $length); + } + /** * Trim the string of the given characters. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index a61aff1ec522..6f3273e50508 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -424,6 +424,20 @@ public function testSubstr() $this->assertEmpty(Str::substr('Б', 2)); } + public function testSubstrCount() + { + $this->assertSame(3, Str::substrCount('laravelPHPFramework', 'a')); + $this->assertSame(0, Str::substrCount('laravelPHPFramework', 'z')); + $this->assertSame(1, Str::substrCount('laravelPHPFramework', 'l', 2)); + $this->assertSame(0, Str::substrCount('laravelPHPFramework', 'z', 2)); + $this->assertSame(1, Str::substrCount('laravelPHPFramework', 'k', -1)); + $this->assertSame(1, Str::substrCount('laravelPHPFramework', 'k', -1)); + $this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', 1, 2)); + $this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', 1, 2)); + $this->assertSame(3, Str::substrCount('laravelPHPFramework', 'a', 1, -2)); + $this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', -10, -3)); + } + public function testUcfirst() { $this->assertSame('Laravel', Str::ucfirst('laravel')); diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 9816e1487eb5..9f8ec9101d3c 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -463,4 +463,20 @@ public function testSubstr() $this->assertSame('Ё', (string) $this->stringable('БГДЖИЛЁ')->substr(-1, 1)); $this->assertSame('', (string) $this->stringable('Б')->substr(2)); } + + public function testSubstrCount() + { + $this->assertSame(3, $this->stringable('laravelPHPFramework')->substrCount('a')); + $this->assertSame(0, $this->stringable('laravelPHPFramework')->substrCount('z')); + $this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('l', 2)); + $this->assertSame(0, $this->stringable('laravelPHPFramework')->substrCount('z', 2)); + $this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('k', -1)); + $this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('k', -1)); + $this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('a', 1, 2)); + $this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('a', 1, 2)); + $this->assertSame(3, $this->stringable('laravelPHPFramework')->substrCount('a', 1, -2)); + $this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('a', -10, -3)); + } + + } From 005fc53b694c6d16f926b29c83dda919b0b203d0 Mon Sep 17 00:00:00 2001 From: Christian Stoyanov Date: Wed, 15 Apr 2020 20:26:50 +0200 Subject: [PATCH 2/3] Fix unnecesary newlines --- src/Illuminate/Support/Str.php | 2 -- tests/Support/SupportStringableTest.php | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index ec943c20e725..757ab89c10c8 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -86,7 +86,6 @@ public static function afterLast($subject, $search) return substr($subject, $position + strlen($search)); } - /** * Transliterate a UTF-8 value to ASCII. * @@ -731,5 +730,4 @@ public static function createUuidsNormally() { static::$uuidFactory = null; } - } diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 9f8ec9101d3c..7a0886f1b13a 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -477,6 +477,4 @@ public function testSubstrCount() $this->assertSame(3, $this->stringable('laravelPHPFramework')->substrCount('a', 1, -2)); $this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('a', -10, -3)); } - - } From 747a244fcabc840cd7c74947fa4fa8f82a118029 Mon Sep 17 00:00:00 2001 From: Christian Stoyanov Date: Wed, 15 Apr 2020 20:35:19 +0200 Subject: [PATCH 3/3] Fix: check if is null correctly. 0 would pass on falsy check --- src/Illuminate/Support/Str.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 757ab89c10c8..2dbceab08a28 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -655,7 +655,7 @@ public static function substr($string, $start, $length = null) */ public static function substrCount($haystack, $needle, $offset = 0, $length = null) { - if ($length) { + if (! is_null($length)) { return substr_count($haystack, $needle, $offset, $length); } else { return substr_count($haystack, $needle, $offset);