mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-09 14:56:31 +02:00
Added lines
This commit is contained in:
10
README.md
10
README.md
@@ -51,6 +51,7 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
|
||||
* [isUpperCase](#isuppercase)
|
||||
* [last](#last)
|
||||
* [length](#length)
|
||||
* [lines](#lines)
|
||||
* [longestCommonPrefix](#longestcommonprefixstring-otherstr)
|
||||
* [longestCommonSuffix](#longestcommonsuffixstring-otherstr)
|
||||
* [longestCommonSubstring](#longestcommonsubstringstring-otherstr)
|
||||
@@ -525,6 +526,15 @@ Returns the length of the string. An alias for PHP's mb_strlen() function.
|
||||
s('fòô bàř')->length(); // 7
|
||||
```
|
||||
|
||||
##### lines()
|
||||
|
||||
Splits on newlines and carriage returns, returning an array of Stringy
|
||||
objects corresponding to the lines in the string.
|
||||
|
||||
```php
|
||||
s("fòô\r\nbàř\n")->lines(); // ['fòô', 'bàř', '']
|
||||
```
|
||||
|
||||
##### longestCommonPrefix(string $otherStr)
|
||||
|
||||
Returns the longest common prefix between the string and $otherStr.
|
||||
|
@@ -215,6 +215,22 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
return $chars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits on newlines and carriage returns, returning an array of Stringy
|
||||
* objects corresponding to the lines in the string.
|
||||
*
|
||||
* @return Stringy[] An array of Stringy objects
|
||||
*/
|
||||
public function lines()
|
||||
{
|
||||
$array = mb_split('[\r\n]{1,2}', $this->str);
|
||||
for ($i = 0; $i < count($array); $i++) {
|
||||
$array[$i] = static::create($array[$i], $this->encoding);
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of the supplied string to upper case.
|
||||
*
|
||||
|
@@ -259,6 +259,43 @@ class StringyTestCase extends PHPUnit_Framework_TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider linesProvider()
|
||||
*/
|
||||
public function testLines($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::create($str, $encoding)->lines();
|
||||
|
||||
$this->assertInternalType('array', $result);
|
||||
foreach ($result as $line) {
|
||||
$this->assertStringy($line);
|
||||
}
|
||||
|
||||
for ($i = 0; $i < count($expected); $i++) {
|
||||
$this->assertEquals($expected[$i], $result[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
public function linesProvider()
|
||||
{
|
||||
return array(
|
||||
array(array(), ""),
|
||||
array(array(''), "\r\n"),
|
||||
array(array('foo', 'bar'), "foo\nbar"),
|
||||
array(array('foo', 'bar'), "foo\rbar"),
|
||||
array(array('foo', 'bar'), "foo\r\nbar"),
|
||||
array(array('foo', '', 'bar'), "foo\r\n\r\nbar"),
|
||||
array(array('foo', 'bar', ''), "foo\r\nbar\r\n"),
|
||||
array(array('', 'foo', 'bar'), "\r\nfoo\r\nbar"),
|
||||
array(array('fòô', 'bàř'), "fòô\nbàř", 'UTF-8'),
|
||||
array(array('fòô', 'bàř'), "fòô\rbàř", 'UTF-8'),
|
||||
array(array('fòô', 'bàř'), "fòô\n\rbàř", 'UTF-8'),
|
||||
array(array('fòô', 'bàř'), "fòô\r\nbàř", 'UTF-8'),
|
||||
array(array('fòô', '', 'bàř'), "fòô\r\n\r\nbàř", 'UTF-8'),
|
||||
array(array('fòô', 'bàř', ''), "fòô\r\nbàř\r\n", 'UTF-8'),
|
||||
array(array('', 'fòô', 'bàř'), "\r\nfòô\r\nbàř", 'UTF-8'),
|
||||
);
|
||||
}
|
||||
/**
|
||||
* @dataProvider upperCaseFirstProvider()
|
||||
*/
|
||||
|
Reference in New Issue
Block a user