diff --git a/README.md b/README.md index 13e3983..9641bea 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Compatible with PHP 5.3+. Inspired by underscore.string.js. * [chars](#chars) * [collapseWhitespace](#collapsewhitespace) * [contains](#contains) - * [count](#count) + * [countSubstr](#countsubstr) * [create](#create) * [dasherize](#dasherize) * [endsWith](#endswith) @@ -144,6 +144,14 @@ foreach ($stringy as $pos => $char) { // array('F', 'ò', 'ô', ' ', 'B', 'à', 'ř') ``` +It also implements the `countable` interface, enabling the use of `count()` to +retrieve the number of characters in the string, given the encoding: + +``` php +$stringy = S::create('Fòô', 'UTF-8'); +count($stringy); // 3 +``` + ## Methods In the list below, any static method other than S::create refers to a method in @@ -157,7 +165,7 @@ the original. $stringy->at(int $index) -S::substr(int $index [, string $encoding ]) +S::at(int $index [, string $encoding ]) Returns the character of the string at $index, with indexes starting at 0. @@ -224,19 +232,19 @@ S::create('Ο συγγραφέας είπε', 'UTF-8')->contains('συγγραφ S::contains('Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8'); // true ``` -#### count +#### countSubstr -$stringy->count(string $substring [, boolean $caseSensitive = true ]) +$stringy->countSubstr(string $substring [, boolean $caseSensitive = true ]) -S::count(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]]) +S::countSubstr(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]]) 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('α'); -S::count('Ο συγγραφέας είπε', 'α', 'UTF-8'); // 2 +S::create('Ο συγγραφέας είπε', 'UTF-8')->countSubstr('α'); +S::countSubstr('Ο συγγραφέας είπε', 'α', 'UTF-8'); // 2 ``` #### create diff --git a/src/Stringy/StaticStringy.php b/src/Stringy/StaticStringy.php index 1dc4118..1dafebd 100644 --- a/src/Stringy/StaticStringy.php +++ b/src/Stringy/StaticStringy.php @@ -739,10 +739,11 @@ class StaticStringy * @param string $encoding The character encoding * @return int The number of $substring occurrences */ - public static function count($str, $substring, $caseSensitive = true, - $encoding = null) + public static function countSubstr($str, $substring, $caseSensitive = true, + $encoding = null) { - return Stringy::create($str, $encoding)->count($substring, $caseSensitive); + return Stringy::create($str, $encoding) + ->countSubstr($substring, $caseSensitive); } /** diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 3a65d23..f8ca1dd 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -2,7 +2,7 @@ namespace Stringy; -class Stringy implements \IteratorAggregate +class Stringy implements \Countable, \IteratorAggregate { private $str; @@ -64,6 +64,16 @@ class Stringy implements \IteratorAggregate return $this->str; } + /** + * Returns the length of the string, implementing the countable interface. + * + * @return int The number of characters in the string, given the encoding + */ + public function count() + { + return $this->length(); + } + /** * Returns a new ArrayIterator, thus implementing the IteratorAggregate * interface. The ArrayIterator's constructor is passed an array of chars @@ -1161,16 +1171,16 @@ class Stringy implements \IteratorAggregate * @param bool $caseSensitive Whether or not to enforce case-sensitivity * @return int The number of $substring occurrences */ - public function count($substring, $caseSensitive = true) + public function countSubstr($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); + if ($caseSensitive) { + return mb_substr_count($this->str, $substring, $this->encoding); } - return mb_substr_count($this->str, $substring, $this->encoding); + $str = mb_strtoupper($this->str, $this->encoding); + $substring = mb_strtoupper($substring, $this->encoding); + + return mb_substr_count($str, $substring, $this->encoding); } /** diff --git a/tests/Stringy/CommonTest.php b/tests/Stringy/CommonTest.php index 3492172..1258c1f 100644 --- a/tests/Stringy/CommonTest.php +++ b/tests/Stringy/CommonTest.php @@ -807,7 +807,7 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase ); } - public function countProvider() + public function countSubstrProvider() { return array( array(0, '', 'foo'), diff --git a/tests/Stringy/StaticStringyTest.php b/tests/Stringy/StaticStringyTest.php index 7c48589..8227751 100644 --- a/tests/Stringy/StaticStringyTest.php +++ b/tests/Stringy/StaticStringyTest.php @@ -565,12 +565,12 @@ class StaticStringyTestCase extends CommonTest } /** - * @dataProvider countProvider() + * @dataProvider countSubstrProvider() */ - public function testCount($expected, $str, $substring, $caseSensitive = true, - $encoding = null) + public function testCountSubstr($expected, $str, $substring, + $caseSensitive = true, $encoding = null) { - $result = S::count($str, $substring, $caseSensitive, $encoding); + $result = S::countSubstr($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 a0429cc..2128e87 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -70,6 +70,13 @@ class StringyTestCase extends CommonTest $this->assertEquals('FÒÔ bÀŘ', $result); } + public function testCount() + { + $stringy = S::create('Fòô', 'UTF-8'); + $this->assertEquals(3, $stringy->count()); + $this->assertEquals(3, count($stringy)); + } + public function testGetIterator() { $stringy = S::create('Fòô Bàř', 'UTF-8'); @@ -721,13 +728,13 @@ class StringyTestCase extends CommonTest } /** - * @dataProvider countProvider() + * @dataProvider countSubstrProvider() */ - public function testCount($expected, $str, $substring, $caseSensitive = true, - $encoding = null) + public function testCountSubstr($expected, $str, $substring, + $caseSensitive = true, $encoding = null) { $stringy = S::create($str, $encoding); - $result = $stringy->count($substring, $caseSensitive); + $result = $stringy->countSubstr($substring, $caseSensitive); $this->assertInternalType('int', $result); $this->assertEquals($expected, $result); $this->assertEquals($str, $stringy);