mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-15 09:44:14 +02:00
Added at()
This commit is contained in:
16
README.md
16
README.md
@@ -40,6 +40,7 @@ Note: The methods listed below are subject to change until we reach a 1.0.0 rele
|
|||||||
* [longestCommonSubstring](#longestcommonsubstring)
|
* [longestCommonSubstring](#longestcommonsubstring)
|
||||||
* [length](#length)
|
* [length](#length)
|
||||||
* [substr](#substr)
|
* [substr](#substr)
|
||||||
|
* [at](#at)
|
||||||
* [Tests](#tests)
|
* [Tests](#tests)
|
||||||
* [License](#license)
|
* [License](#license)
|
||||||
|
|
||||||
@@ -588,6 +589,19 @@ S::create('fòô bàř', 'UTF-8')->substr(2, 3);
|
|||||||
S::substr('fòô bàř', 2, 3, 'UTF-8'); // 'ô b'
|
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
|
## TODO
|
||||||
|
|
||||||
**count** => substr_count
|
**count** => substr_count
|
||||||
@@ -612,8 +626,6 @@ S::substr('fòô bàř', 2, 3, 'UTF-8'); // 'ô b'
|
|||||||
|
|
||||||
**pluralize** ($count, $singular, $plural = null)
|
**pluralize** ($count, $singular, $plural = null)
|
||||||
|
|
||||||
**at** $position
|
|
||||||
|
|
||||||
**first**
|
**first**
|
||||||
|
|
||||||
**last**
|
**last**
|
||||||
|
@@ -465,10 +465,23 @@ class StaticStringy
|
|||||||
* @param string $str The string to get the length of
|
* @param string $str The string to get the length of
|
||||||
* @param int $start Position of the first character to use from str
|
* @param int $start Position of the first character to use from str
|
||||||
* @param int $length Maximum number of characters used
|
* @param int $length Maximum number of characters used
|
||||||
|
* @param string $encoding The character encoding
|
||||||
* @return string The substring of $str
|
* @return string The substring of $str
|
||||||
*/
|
*/
|
||||||
public static function substr($str, $start, $length = null, $encoding = null)
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -753,4 +753,17 @@ class Stringy
|
|||||||
|
|
||||||
return $this;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -546,6 +546,22 @@ class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
return $testData;
|
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
|
// A test is required so as not to throw an error
|
||||||
// This is a lot cleaner than using PHPUnit's mocks to spy
|
// This is a lot cleaner than using PHPUnit's mocks to spy
|
||||||
public function test() {
|
public function test() {
|
||||||
|
@@ -317,4 +317,13 @@ class StaticStringyTestCase extends CommonTest
|
|||||||
$result = S::substr($str, $start, $length, $encoding);
|
$result = S::substr($str, $start, $length, $encoding);
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider stringsForAt
|
||||||
|
*/
|
||||||
|
public function testAt($expected, $str, $index, $encoding = null)
|
||||||
|
{
|
||||||
|
$result = S::at($str, $index, $encoding);
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -309,4 +309,13 @@ class StringyTestCase extends CommonTest
|
|||||||
$result = S::create($str, $encoding)->substr($start, $length);
|
$result = S::create($str, $encoding)->substr($start, $length);
|
||||||
$this->assertEquals($expected, $result);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user