diff --git a/README.md b/README.md index acca471..d3049dc 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,9 @@ A PHP library with a variety of string manipulation functions with multibyte sup * [clean](#clean) * [standardize](#standardize) * [pad](#pad) + * [padLeft](#padleft) + * [padRight](#padright) + * [padBoth](#padboth) * [Tests](#tests) * [License](#license) @@ -203,9 +206,40 @@ default string used for padding is a space, and the default type (one of S::pad('fòô bàř', 10, '¬ø', 'left', 'UTF-8'); // '¬ø¬fòô bàř' ``` -## TODO +##### padLeft -**center** +S::padLeft(string $str , int $length [, string $padStr [, string $encoding]]) + +Returns a new string of a given length such that the beginning of the +string is padded. Alias for pad($str, $length, $padStr, 'left', $encoding) + +```php +S::padLeft('foo bar', 9, ' '); // ' foo bar' +``` + +##### padRight + +S::padRight(string $str , int $length [, string $padStr [, string $encoding]]) + +Returns a new string of a given length such that the end of the string is +padded. Alias for pad($str, $length, $padStr, 'right', $encoding) + +```php +S::padRight('foo bar', 10, '_*'); // 'foo bar_*_' +``` + +##### padBoth + +S::padBoth(string $str , int $length [, string $padStr [, string $encoding]]) + +Returns a new string of a given length such that both sides of the string +string are padded. Alias for pad($str, $length, $padStr, 'both', $encoding) + +```php +S::padBoth('foo bar', 9, ' '); // ' foo bar ' +``` + +## TODO **startsWith** diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 01204f0..db447eb 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -323,6 +323,48 @@ class Stringy { return $paddedStr; } + + /** + * Returns a new string of a given length such that the beginning of the + * string is padded. Alias for pad($str, $length, $padStr, 'left', $encoding) + * + * @param string $str String to pad + * @param int $length Desired string length after padding + * @param string $padStr String used to pad, defaults to space + * @param string $encoding The character encoding + * @return string The padded string + */ + public static function padLeft($str, $length, $padStr = ' ', $encoding = null) { + return self::pad($str, $length, $padStr, 'left', $encoding); + } + + /** + * Returns a new string of a given length such that the end of the string is + * padded. Alias for pad($str, $length, $padStr, 'right', $encoding) + * + * @param string $str String to pad + * @param int $length Desired string length after padding + * @param string $padStr String used to pad, defaults to space + * @param string $encoding The character encoding + * @return string The padded string + */ + public static function padRight($str, $length, $padStr = ' ', $encoding = null) { + return self::pad($str, $length, $padStr, 'right', $encoding); + } + + /** + * Returns a new string of a given length such that both sides of the string + * string are padded. Alias for pad($str, $length, $padStr, 'both', $encoding) + * + * @param string $str String to pad + * @param int $length Desired string length after padding + * @param string $padStr String used to pad, defaults to space + * @param string $encoding The character encoding + * @return string The padded string + */ + public static function padBoth ($str, $length, $padStr = ' ', $encoding = null) { + return self::pad($str, $length, $padStr, 'both', $encoding); + } } ?> diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index 8816767..00fc597 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -185,7 +185,8 @@ class StringyTestCase extends PHPUnit_Framework_TestCase { /** * @dataProvider stringsForTitleize */ - public function testTitleize($string, $expected, $ignore = null, $encoding = null) { + public function testTitleize($string, $expected, $ignore = null, + $encoding = null) { $result = S::titleize($string, $ignore, $encoding); $this->assertEquals($expected, $result); } @@ -284,39 +285,106 @@ class StringyTestCase extends PHPUnit_Framework_TestCase { * @dataProvider stringsForPad */ public function testPad($string, $expected, $length, $padStr = ' ', - $padType = 'right', $encoding = null) { + $padType = 'right', $encoding = null) { $result = S::pad($string, $length, $padStr, $padType, $encoding); $this->assertEquals($expected, $result); } public function stringsForPad() { $testData = array( + // $length <= $str array('foo bar', 'foo bar', -1), array('foo bar', 'foo bar', 7), + array('fòô bàř', 'fòô bàř', 7, ' ', 'right', 'UTF-8'), + + // right array('foo bar', 'foo bar ', 9), - array('foo bar', ' foo bar', 9, ' ', 'left'), - array('foo bar', 'foo bar ', 8, ' ', 'both'), - array('foo bar', ' foo bar ', 9, ' ', 'both'), - array('foo bar', '_*foo bar', 9, '_*', 'left'), - array('foo bar', '_*_foo bar', 10, '_*', 'left'), array('foo bar', 'foo bar_*', 9, '_*', 'right'), array('foo bar', 'foo bar_*_', 10, '_*', 'right'), - array('fòô bàř', 'fòô bàř', -1, 'UTF-8'), - array('fòô bàř', 'fòô bàř', 7, 'UTF-8'), array('fòô bàř', 'fòô bàř ', 9, ' ', 'right', 'UTF-8'), - array('fòô bàř', ' fòô bàř', 9, ' ', 'left', 'UTF-8'), - array('fòô bàř', 'fòô bàř ', 8, ' ', 'both', 'UTF-8'), - array('fòô bàř', ' fòô bàř ', 9, ' ', 'both', 'UTF-8'), - array('fòô bàř', '¬øfòô bàř', 9, '¬ø', 'left', 'UTF-8'), - array('fòô bàř', '¬ø¬fòô bàř', 10, '¬ø', 'left', 'UTF-8'), - array('fòô bàř', '¬ø¬øfòô bàř', 11, '¬ø', 'left', 'UTF-8'), array('fòô bàř', 'fòô bàř¬ø', 9, '¬ø', 'right', 'UTF-8'), array('fòô bàř', 'fòô bàř¬ø¬', 10, '¬ø', 'right', 'UTF-8'), array('fòô bàř', 'fòô bàř¬ø¬ø', 11, '¬ø', 'right', 'UTF-8'), + + // left + array('foo bar', ' foo bar', 9, ' ', 'left'), + array('foo bar', '_*foo bar', 9, '_*', 'left'), + array('foo bar', '_*_foo bar', 10, '_*', 'left'), + array('fòô bàř', ' fòô bàř', 9, ' ', 'left', 'UTF-8'), + array('fòô bàř', '¬øfòô bàř', 9, '¬ø', 'left', 'UTF-8'), + array('fòô bàř', '¬ø¬fòô bàř', 10, '¬ø', 'left', 'UTF-8'), + array('fòô bàř', '¬ø¬øfòô bàř', 11, '¬ø', 'left', 'UTF-8'), + + // both + array('foo bar', 'foo bar ', 8, ' ', 'both'), + array('foo bar', ' foo bar ', 9, ' ', 'both'), + array('fòô bàř', 'fòô bàř ', 8, ' ', 'both', 'UTF-8'), + array('fòô bàř', ' fòô bàř ', 9, ' ', 'both', 'UTF-8'), array('fòô bàř', 'fòô bàř¬', 8, '¬ø', 'both', 'UTF-8'), array('fòô bàř', '¬fòô bàř¬', 9, '¬ø', 'both', 'UTF-8'), array('fòô bàř', '¬fòô bàř¬ø', 10, '¬ø', 'both', 'UTF-8'), array('fòô bàř', '¬øfòô bàř¬ø', 11, '¬ø', 'both', 'UTF-8'), + array('fòô bàř', '¬fòô bàř¬ø', 10, '¬øÿ', 'both', 'UTF-8'), + array('fòô bàř', '¬øfòô bàř¬ø', 11, '¬øÿ', 'both', 'UTF-8'), + array('fòô bàř', '¬øfòô bàř¬øÿ', 12, '¬øÿ', 'both', 'UTF-8') + ); + + return $testData; + } + + /** + * @dataProvider stringsForPadLeft + */ + public function testPadLeft($string, $expected, $length, $padStr = ' ', + $encoding = null) { + $result = S::padLeft($string, $length, $padStr, $encoding); + $this->assertEquals($expected, $result); + } + + public function stringsForPadLeft() { + $testData = array( + array('foo bar', ' foo bar', 9), + array('foo bar', '_*_foo bar', 10, '_*'), + array('fòô bàř', '¬ø¬øfòô bàř', 11, '¬ø', 'UTF-8'), + ); + + return $testData; + } + + /** + * @dataProvider stringsForPadRight + */ + public function testPadRight($string, $expected, $length, $padStr = ' ', + $encoding = null) { + $result = S::padRight($string, $length, $padStr, $encoding); + $this->assertEquals($expected, $result); + } + + public function stringsForPadRight() { + $testData = array( + array('foo bar', 'foo bar ', 9), + array('foo bar', 'foo bar_*_', 10, '_*'), + array('fòô bàř', 'fòô bàř¬ø¬ø', 11, '¬ø', 'UTF-8'), + ); + + return $testData; + } + + /** + * @dataProvider stringsForPadBoth + */ + public function testPadBoth($string, $expected, $length, $padStr = ' ', + $encoding = null) { + $result = S::padBoth($string, $length, $padStr, $encoding); + $this->assertEquals($expected, $result); + } + + public function stringsForPadBoth() { + $testData = array( + array('foo bar', 'foo bar ', 8), + array('foo bar', ' foo bar ', 9, ' '), + array('fòô bàř', '¬fòô bàř¬ø', 10, '¬øÿ', 'UTF-8'), + array('fòô bàř', '¬øfòô bàř¬øÿ', 12, '¬øÿ', 'UTF-8') ); return $testData;