1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-09 23:06:41 +02:00

Added lines

This commit is contained in:
Daniel St. Jules
2015-07-25 22:19:36 -07:00
parent cce314f4a5
commit b9cf4b6ae3
3 changed files with 63 additions and 0 deletions

View File

@@ -51,6 +51,7 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
* [isUpperCase](#isuppercase) * [isUpperCase](#isuppercase)
* [last](#last) * [last](#last)
* [length](#length) * [length](#length)
* [lines](#lines)
* [longestCommonPrefix](#longestcommonprefixstring-otherstr) * [longestCommonPrefix](#longestcommonprefixstring-otherstr)
* [longestCommonSuffix](#longestcommonsuffixstring-otherstr) * [longestCommonSuffix](#longestcommonsuffixstring-otherstr)
* [longestCommonSubstring](#longestcommonsubstringstring-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 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) ##### longestCommonPrefix(string $otherStr)
Returns the longest common prefix between the string and $otherStr. Returns the longest common prefix between the string and $otherStr.

View File

@@ -215,6 +215,22 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
return $chars; 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. * Converts the first character of the supplied string to upper case.
* *

View File

@@ -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() * @dataProvider upperCaseFirstProvider()
*/ */