1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-11 15:54:04 +02:00

Renamed count to countSubstr, implemented Countable interface

This commit is contained in:
Daniel St. Jules
2014-02-03 23:41:57 -05:00
parent 7313dcc07e
commit 1c13f2ad7e
6 changed files with 53 additions and 27 deletions

View File

@@ -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

View File

@@ -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);
}
/**

View File

@@ -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);
}
/**

View File

@@ -807,7 +807,7 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
);
}
public function countProvider()
public function countSubstrProvider()
{
return array(
array(0, '', 'foo'),

View File

@@ -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);
}

View File

@@ -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);