1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-13 08:44:01 +02:00

Added at()

This commit is contained in:
Daniel St. Jules
2013-07-27 13:57:30 -04:00
parent d757ce38de
commit f3ac2bf1f3
6 changed files with 75 additions and 3 deletions

View File

@@ -40,6 +40,7 @@ Note: The methods listed below are subject to change until we reach a 1.0.0 rele
* [longestCommonSubstring](#longestcommonsubstring)
* [length](#length)
* [substr](#substr)
* [at](#at)
* [Tests](#tests)
* [License](#license)
@@ -588,6 +589,19 @@ S::create('fòô bàř', 'UTF-8')->substr(2, 3);
S::substr('fòô bàř', 2, 3, 'UTF-8'); // 'ô b'
```
##### at
$stringy->at(int $index)
S::substr(int $index [, string $encoding ])
Gets the character of $str at $index, with indexes starting at 0.
```php
S::create('fòô bàř', 'UTF-8')->at(6);
S::at('fòô bàř', 6, 'UTF-8'); // 'ř'
```
## TODO
**count** => substr_count
@@ -612,8 +626,6 @@ S::substr('fòô bàř', 2, 3, 'UTF-8'); // 'ô b'
**pluralize** ($count, $singular, $plural = null)
**at** $position
**first**
**last**

View File

@@ -465,10 +465,23 @@ class StaticStringy
* @param string $str The string to get the length of
* @param int $start Position of the first character to use from str
* @param int $length Maximum number of characters used
* @param string $encoding The character encoding
* @return string The substring of $str
*/
public static function substr($str, $start, $length = null, $encoding = null)
{
return Stringy::create($str, $encoding)->substr($start, $length);
return Stringy::create($str, $encoding)->substr($start, $length)->str;
}
/**
* Gets the character of $str at $index, with indexes starting at 0.
*
* @param int $index Position of the character
* @param string $encoding The character encoding
* @return string The character at $index
*/
public static function at($str, $index, $encoding = null)
{
return Stringy::create($str, $encoding)->at($index)->str;
}
}

View File

@@ -753,4 +753,17 @@ class Stringy
return $this;
}
/**
* Gets the character of $str at $index, with indexes starting at 0.
*
* @param int $index Position of the character
* @return Stringy Object with its $str being the selected character
*/
public function at($index)
{
$this->substr($index, 1);
return $this;
}
}

View File

@@ -546,6 +546,22 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData;
}
public function stringsForAt()
{
$testData = array(
array('f', 'foo bar', 0),
array('o', 'foo bar', 1),
array('r', 'foo bar', 6),
array('', 'foo bar', 7),
array('f', 'fòô bàř', 0, 'UTF-8'),
array('ò', 'fòô bàř', 1, 'UTF-8'),
array('ř', 'fòô bàř', 6, 'UTF-8'),
array('', 'fòô bàř', 7, 'UTF-8'),
);
return $testData;
}
// A test is required so as not to throw an error
// This is a lot cleaner than using PHPUnit's mocks to spy
public function test() {

View File

@@ -317,4 +317,13 @@ class StaticStringyTestCase extends CommonTest
$result = S::substr($str, $start, $length, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForAt
*/
public function testAt($expected, $str, $index, $encoding = null)
{
$result = S::at($str, $index, $encoding);
$this->assertEquals($expected, $result);
}
}

View File

@@ -309,4 +309,13 @@ class StringyTestCase extends CommonTest
$result = S::create($str, $encoding)->substr($start, $length);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForAt
*/
public function testAt($expected, $str, $index, $encoding = null)
{
$result = S::create($str, $encoding)->at($index);
$this->assertEquals($expected, $result);
}
}