From e46797a9f829f952d34a13e7e9679aa9d24b55b3 Mon Sep 17 00:00:00 2001 From: "Daniel St. Jules" Date: Tue, 18 Mar 2014 23:56:37 -0400 Subject: [PATCH] Updated pad methods --- .gitignore | 1 - src/Stringy.php | 91 ++++++++++++++++++++++++++----------- tests/CommonTest.php | 25 ++++++++++ tests/StaticStringyTest.php | 19 ++++++++ tests/StringyTest.php | 22 +++++++++ 5 files changed, 130 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index daf4619..8db156e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ vendor/ composer.lock .DS_Store - diff --git a/src/Stringy.php b/src/Stringy.php index 12bebc5..e26c24d 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -492,29 +492,34 @@ public function toAscii() } /** - * Adds the specified amount of left and right padding given by the - * specified padding string. The default padding string is a space (0x20). - * - * @param int $left Length of left padding. - * @param int $right Length of right padding. - * @param string $pad String used to pad. - * @return Stringy String with padding applied. - */ - private function pad($left = 0, $right = 0, $pad = ' ') + * Pads the string to a given length with $padStr. If length is less than + * or equal to the length of the string, no padding takes places. The + * default string used for padding is a space, and the default type (one of + * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException + * if $padType isn't one of those 3 values. + * + * @param int $length Desired string length after padding + * @param string $padStr String used to pad, defaults to space + * @param string $padType One of 'left', 'right', 'both' + * @return Stringy Object with a padded $str + * @throws InvalidArgumentException If $padType isn't one of 'right', + * 'left' or 'both' + */ + public function pad($length, $padStr = ' ', $padType = 'right') { - $stringy = self::create($this->str, $this->encoding); - $length = mb_strlen($pad, $stringy->encoding); - - //Nothing to do when padding string is empty. - if (!$length) return $stringy; - - $stringy->str = - mb_substr(str_repeat($pad, ceil($left / $length)), 0, $left, $stringy->encoding) - . $stringy->str - . mb_substr(str_repeat($pad, ceil($right / $length)), 0, $right, $stringy->encoding) - ; + if (!in_array($padType, array('left', 'right', 'both'))) { + throw new \InvalidArgumentException('Pad expects $padType ' . + "to be one of 'left', 'right' or 'both'"); + } - return $stringy; + switch ($padType) { + case 'left': + return $this->padLeft($length, $padStr); + case 'right': + return $this->padRight($length, $padStr); + default: + return $this->padBoth($length, $padStr); + } } /** @@ -523,11 +528,11 @@ private function pad($left = 0, $right = 0, $pad = ' ') * * @param int $length Desired string length after padding * @param string $padStr String used to pad, defaults to space - * @return Stringy String with padding applied. + * @return Stringy String with left padding */ public function padLeft($length, $padStr = ' ') { - return $this->pad($length - $this->length(), 0, $padStr); + return $this->applyPadding($length - $this->length(), 0, $padStr); } /** @@ -536,11 +541,11 @@ public function padLeft($length, $padStr = ' ') * * @param int $length Desired string length after padding * @param string $padStr String used to pad, defaults to space - * @return Stringy String with padding applied. + * @return Stringy String with right padding */ public function padRight($length, $padStr = ' ') { - return $this->pad(0, $length - $this->length(), $padStr); + return $this->applyPadding(0, $length - $this->length(), $padStr); } /** @@ -549,13 +554,45 @@ public function padRight($length, $padStr = ' ') * * @param int $length Desired string length after padding * @param string $padStr String used to pad, defaults to space - * @return Stringy String with padding applied. + * @return Stringy String with padding applied */ public function padBoth($length, $padStr = ' ') { $padding = $length - $this->length(); - return $this->pad(floor($padding / 2), ceil($padding / 2), $padStr); + return $this->applyPadding(floor($padding / 2), ceil($padding / 2), + $padStr); + } + + /** + * Adds the specified amount of left and right padding to the given string. + * The default character used is a space. + * + * @param int $left Length of left padding + * @param int $right Length of right padding + * @param string $padStr String used to pad + * @return Stringy String with padding applied + */ + private function applyPadding($left = 0, $right = 0, $padStr = ' ') + { + $stringy = self::create($this->str, $this->encoding); + $length = mb_strlen($padStr, $stringy->encoding); + + $strLength = $stringy->length(); + $paddedLength = $strLength + $left + $right; + + if (!$length || $paddedLength <= $strLength) { + return $stringy; + } + + $leftPadding = mb_substr(str_repeat($padStr, ceil($left / $length)), 0, + $left, $stringy->encoding); + $rightPadding = mb_substr(str_repeat($padStr, ceil($right / $length)), 0, + $right, $stringy->encoding); + + $stringy->str = $leftPadding . $stringy->str . $rightPadding; + + return $stringy; } /** diff --git a/tests/CommonTest.php b/tests/CommonTest.php index 0a8647f..b1a46cb 100644 --- a/tests/CommonTest.php +++ b/tests/CommonTest.php @@ -181,6 +181,31 @@ public function toAsciiProvider() ); } + public function padProvider() + { + return 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, '_*', 'right'), + array('fòô bàř¬ø¬', 'fòô bàř', 10, '¬ø', 'right', 'UTF-8'), + + // left + array(' foo bar', 'foo bar', 9, ' ', 'left'), + array('_*foo bar', 'foo bar', 9, '_*', 'left'), + array('¬ø¬fòô bàř', 'fòô bàř', 10, '¬ø', 'left', 'UTF-8'), + + // both + array('foo bar ', 'foo bar', 8, ' ', 'both'), + array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬ø', 'both', 'UTF-8'), + array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'both', 'UTF-8') + ); + } + public function padLeftProvider() { return array( diff --git a/tests/StaticStringyTest.php b/tests/StaticStringyTest.php index 4f9da98..676bff4 100644 --- a/tests/StaticStringyTest.php +++ b/tests/StaticStringyTest.php @@ -140,6 +140,25 @@ public function testToAscii($expected, $str) $this->assertEquals($expected, $result); } + /** + * @dataProvider padProvider() + */ + public function testPad($expected, $str, $length, $padStr = ' ', + $padType = 'right', $encoding = null) + { + $result = S::pad($str, $length, $padStr, $padType, $encoding); + $this->assertInternalType('string', $result); + $this->assertEquals($expected, $result); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testPadException() + { + $result = S::pad('string', 5, 'foo', 'bar'); + } + /** * @dataProvider padLeftProvider() */ diff --git a/tests/StringyTest.php b/tests/StringyTest.php index 6353a29..771bf85 100644 --- a/tests/StringyTest.php +++ b/tests/StringyTest.php @@ -309,6 +309,28 @@ public function testToAscii($expected, $str) $this->assertEquals($str, $stringy); } + /** + * @dataProvider padProvider() + */ + public function testPad($expected, $str, $length, $padStr = ' ', + $padType = 'right', $encoding = null) + { + $stringy = S::create($str, $encoding); + $result = $stringy->pad($length, $padStr, $padType); + $this->assertInstanceOf('Stringy\Stringy', $result); + $this->assertEquals($expected, $result); + $this->assertEquals($str, $stringy); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testPadException() + { + $stringy = S::create('foo'); + $result = $stringy->pad(5, 'foo', 'bar'); + } + /** * @dataProvider padLeftProvider() */