diff --git a/README.md b/README.md index 7d85a3d..089a93a 100644 --- a/README.md +++ b/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. diff --git a/src/Stringy.php b/src/Stringy.php index 2a700a6..4dd897c 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -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. * diff --git a/tests/StringyTest.php b/tests/StringyTest.php index 884f551..4a28b31 100644 --- a/tests/StringyTest.php +++ b/tests/StringyTest.php @@ -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() */