From 1a2e771ff8a9cb8cc240abbce7a63dd8613785fa Mon Sep 17 00:00:00 2001 From: Heudon Date: Wed, 27 May 2015 15:45:24 -0300 Subject: [PATCH] Added $charList and $type to trim function Adding the option to trim the string with others characters, not only with the default characters provided by php. Also added the option to define the function which will be used to trim the string, if it's normal trim, left trim or right trim --- src/Stringy.php | 30 +++++++++++++++++++++++++++-- tests/CommonTest.php | 19 ++++++++++++++++++- tests/StaticStringyTest.php | 2 +- tests/StringyTest.php | 38 +++++++++++++++++++++++++++++++++++-- 4 files changed, 83 insertions(+), 6 deletions(-) diff --git a/src/Stringy.php b/src/Stringy.php index 4729c83..dcd2123 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -4,6 +4,13 @@ namespace Stringy; class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess { + + const TRIM_BOTH = 'trim'; + + const TRIM_LEFT = 'ltrim'; + + const TRIM_RIGHT = 'rtrim'; + /** * An instance's string. * @@ -1026,11 +1033,30 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess /** * Returns the trimmed string. An alias for PHP's trim() function. * + * @param string $charList list with characters to be removed + * @param int $type which function will be used to trim the string, trim, ltrim or rtrim * @return Stringy Object with a trimmed $str + * @throws \InvalidArgumentException */ - public function trim() + public function trim($charList = " \t\n\r\0\x0B", $type = self::TRIM_BOTH) { - return static::create(trim($this->str), $this->encoding); + if (!is_string($charList)) { + throw new \InvalidArgumentException( + 'Charset list must be a string' + ); + } + + if (!in_array($type, array( + self::TRIM_BOTH, + self::TRIM_LEFT, + self::TRIM_RIGHT, + ))) { + throw new \InvalidArgumentException( + 'Type of trim function must be trim (default), rtrim or ltrim, just as native php.' + ); + } + + return static::create($type($this->str, $charList), $this->encoding); } /** diff --git a/tests/CommonTest.php b/tests/CommonTest.php index 9795702..5976295 100644 --- a/tests/CommonTest.php +++ b/tests/CommonTest.php @@ -590,7 +590,7 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase ); } - public function trimProvider() + public function trimProviderWithoutParams() { return array( array('foo bar', ' foo bar '), @@ -604,6 +604,23 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase ); } + public function trimProviderWithParams() + { + return array( + array('foo bar', ' foo bar ', " \t\n\r\0\x0B", 'trim'), + array('foo bar', ' foo bar', " \t\n\r\0\x0B", 'trim'), + array('foo bar', 'foo bar ', " \t\n\r\0\x0B", 'trim'), + array('foo bar', "\n\t foo bar \n\t", " \t\n\r\0\x0B", 'trim'), + array('fòô bàř', ' fòô bàř ', " \t\n\r\0\x0B", 'trim'), + array('fòô bàř', ' fòô bàř', " \t\n\r\0\x0B", 'trim'), + array('fòô bàř', 'fòô bàř ', " \t\n\r\0\x0B", 'trim'), + array('fòô bàř', "\n\t fòô bàř \n\t", " \t\n\r\0\x0B", 'trim'), + array(' foo bar', ' foo bar ', " \t\n\r\0\x0B", 'rtrim'), + array('foo bar ', ' foo bar ', " \t\n\r\0\x0B", 'ltrim'), + array(' foo bar ', ' foo bar ', "\t\n\r\0\x0B", 'ltrim') + ); + } + public function longestCommonPrefixProvider() { return array( diff --git a/tests/StaticStringyTest.php b/tests/StaticStringyTest.php index d8a4739..3e1bd43 100644 --- a/tests/StaticStringyTest.php +++ b/tests/StaticStringyTest.php @@ -382,7 +382,7 @@ class StaticStringyTestCase extends CommonTest } /** - * @dataProvider trimProvider() + * @dataProvider trimProviderWithoutParams() */ public function testTrim($expected, $str) { diff --git a/tests/StringyTest.php b/tests/StringyTest.php index 617c065..97605c6 100644 --- a/tests/StringyTest.php +++ b/tests/StringyTest.php @@ -594,9 +594,9 @@ class StringyTestCase extends CommonTest } /** - * @dataProvider trimProvider() + * @dataProvider trimProviderWithoutParams() */ - public function testTrim($expected, $str) + public function testTrimWithoutParams($expected, $str) { $stringy = S::create($str); $result = $stringy->trim(); @@ -605,6 +605,40 @@ class StringyTestCase extends CommonTest $this->assertEquals($str, $stringy); } + /** + * @dataProvider trimProviderWithParams() + */ + public function testTrimWithParams($expected, $str, $charList, $type) + { + $stringy = S::create($str); + $result = $stringy->trim($charList, $type); + $this->assertStringy($result); + $this->assertEquals($expected, $result); + $this->assertEquals($str, $stringy); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testTrimWithInvalidCharset() + { + $stringy = S::create('test'); + $stringy->trim(array('test1', 'test2'), 'trim'); + $this->fail('Expecting exception when the first argument passed is not a string'); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testTrimWithInvalidType() + { + $stringy = S::create('test'); + $stringy->trim(' test ', 'aa'); + $stringy->trim('btest ', 'Trim'); + $stringy->trim(' btest', 'RTrim'); + $this->fail('Expecting exception when the first argument passed is not a string'); + } + /** * @dataProvider longestCommonPrefixProvider() */