From 41ea0277b27b15b98ef1ff6cc47177dc5da96c78 Mon Sep 17 00:00:00 2001 From: "Daniel St. Jules" Date: Sat, 14 Sep 2013 14:57:18 -0400 Subject: [PATCH] Added $caseSensitive to count() --- README.md | 9 +++++---- src/Stringy/StaticStringy.php | 15 +++++++++------ src/Stringy/Stringy.php | 15 ++++++++++++--- tests/Stringy/CommonTest.php | 9 ++++++++- tests/Stringy/StaticStringyTest.php | 5 +++-- tests/Stringy/StringyTest.php | 5 +++-- 6 files changed, 40 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index f10f142..1f802fb 100644 --- a/README.md +++ b/README.md @@ -184,12 +184,13 @@ S::contains('Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8') #### count -$stringy->count(string $substring) +$stringy->count(string $substring [, boolean $caseSensitive = true ]) -S::count(string $str, string $substring [, string $encoding ]) +S::count(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]]) -Returns the number of occurrences of $substring in the given string. An alias for -mb_substr_count() +Returns the number of occurrences of $substring in the given string. +By default, the comparison is case-sensitive, but can be made insensitive +by setting $caseSensitive to false. ```php S::create('Ο συγγραφέας είπε', 'UTF-8')->count('α'); diff --git a/src/Stringy/StaticStringy.php b/src/Stringy/StaticStringy.php index 9926e06..f95e2d6 100644 --- a/src/Stringy/StaticStringy.php +++ b/src/Stringy/StaticStringy.php @@ -716,16 +716,19 @@ class StaticStringy /** * Returns the number of occurrences of $substring in the given string. - * An alias for mb_substr_count() + * By default, the comparison is case-sensitive, but can be made insensitive + * by setting $caseSensitive to false. * - * @param string $str The string to search through - * @param string $substring The substring to search for - * @param string $encoding The character encoding + * @param string $str The string to search through + * @param string $substring The substring to search for + * @param bool $caseSensitive Whether or not to enforce case-sensitivity + * @param string $encoding The character encoding * @return int The number of $substring occurrences */ - public static function count($str, $substring, $encoding = null) + public static function count($str, $substring, $caseSensitive = true, + $encoding = null) { - return Stringy::create($str, $encoding)->count($substring); + return Stringy::create($str, $encoding)->count($substring, $caseSensitive); } /** diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 23ec6be..d91e965 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -1104,13 +1104,22 @@ class Stringy /** * Returns the number of occurrences of $substring in the given string. - * An alias for mb_substr_count() + * By default, the comparison is case-sensitive, but can be made insensitive + * by setting $caseSensitive to false. * - * @param string $substring The substring to search for + * @param string $substring The substring to search for + * @param bool $caseSensitive Whether or not to enforce case-sensitivity * @return int The number of $substring occurrences */ - public function count($substring) + public function count($substring, $caseSensitive = true) { + if (!$caseSensitive) { + $str = mb_strtoupper($this->str, $this->encoding); + $substring = mb_strtoupper($substring, $this->encoding); + + return mb_substr_count($str, $substring, $this->encoding); + } + return mb_substr_count($this->str, $substring, $this->encoding); } diff --git a/tests/Stringy/CommonTest.php b/tests/Stringy/CommonTest.php index f89dfbb..2c228e4 100644 --- a/tests/Stringy/CommonTest.php +++ b/tests/Stringy/CommonTest.php @@ -907,7 +907,14 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase array(0, '', 'fòô', 'UTF-8'), array(0, 'fòô', 'bàř', 'UTF-8'), array(1, 'fòô bàř', 'fòô', 'UTF-8'), - array(2, 'fôòô bàř', 'ô', 'UTF-8') + array(2, 'fôòô bàř', 'ô', 'UTF-8'), + array(0, 'fÔÒÔ bàř', 'ô', 'UTF-8'), + array(0, 'foo', 'BAR', false), + array(1, 'foo bar', 'FOo', false), + array(2, 'foo bar', 'O', false), + array(1, 'fòô bàř', 'fÒÔ', false, 'UTF-8'), + array(2, 'fôòô bàř', 'Ô', false, 'UTF-8'), + array(2, 'συγγραφέας', 'Σ', false, 'UTF-8') ); return $testData; diff --git a/tests/Stringy/StaticStringyTest.php b/tests/Stringy/StaticStringyTest.php index 17b9bd7..f28d40e 100644 --- a/tests/Stringy/StaticStringyTest.php +++ b/tests/Stringy/StaticStringyTest.php @@ -554,9 +554,10 @@ class StaticStringyTestCase extends CommonTest /** * @dataProvider stringsForCount */ - public function testCount($expected, $str, $substring, $encoding = null) + public function testCount($expected, $str, $substring, $caseSensitive = true, + $encoding = null) { - $result = S::count($str, $substring, $encoding); + $result = S::count($str, $substring, $caseSensitive, $encoding); $this->assertInternalType('int', $result); $this->assertEquals($expected, $result); } diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index 5534b7a..66043db 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -653,10 +653,11 @@ class StringyTestCase extends CommonTest /** * @dataProvider stringsForCount */ - public function testCount($expected, $str, $substring, $encoding = null) + public function testCount($expected, $str, $substring, $caseSensitive = true, + $encoding = null) { $stringy = S::create($str, $encoding); - $result = $stringy->count($substring); + $result = $stringy->count($substring, $caseSensitive); $this->assertInternalType('int', $result); $this->assertEquals($expected, $result); $this->assertEquals($str, $stringy);