mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-06 05:17:34 +02:00
Merge branch 'pr/20' after cleanup
This commit is contained in:
@@ -492,62 +492,27 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pads the string to a given length with $padStr. If length is less than
|
* Adds the specified amount of left and right padding given by the
|
||||||
* or equal to the length of the string, no padding takes places. The
|
* specified padding string. The default padding string is a space (0x20).
|
||||||
* 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 int $left Length of left padding.
|
||||||
* @param string $padStr String used to pad, defaults to space
|
* @param int $right Length of right padding.
|
||||||
* @param string $padType One of 'left', 'right', 'both'
|
* @param string $pad String used to pad.
|
||||||
* @return Stringy Object with a padded $str
|
* @return Stringy String with padding applied.
|
||||||
* @throws InvalidArgumentException If $padType isn't one of 'right',
|
|
||||||
* 'left' or 'both'
|
|
||||||
*/
|
*/
|
||||||
public function pad($length, $padStr = ' ', $padType = 'right')
|
private function pad($left = 0, $right = 0, $pad = ' ')
|
||||||
{
|
{
|
||||||
if (!in_array($padType, array('left', 'right', 'both'))) {
|
|
||||||
throw new \InvalidArgumentException('Pad expects $padType ' .
|
|
||||||
"to be one of 'left', 'right' or 'both'");
|
|
||||||
}
|
|
||||||
|
|
||||||
$stringy = self::create($this->str, $this->encoding);
|
$stringy = self::create($this->str, $this->encoding);
|
||||||
$encoding = $stringy->encoding;
|
$length = mb_strlen($pad, $stringy->encoding);
|
||||||
$strLength = $stringy->length();
|
|
||||||
$padStrLength = mb_strlen($padStr, $encoding);
|
|
||||||
|
|
||||||
if ($length <= $strLength || $padStrLength <= 0) {
|
//Nothing to do when padding string is empty.
|
||||||
return $stringy;
|
if (!$length) return $stringy;
|
||||||
}
|
|
||||||
|
|
||||||
// Number of times to repeat the padStr if left or right
|
$stringy->str =
|
||||||
$times = ceil(($length - $strLength) / $padStrLength);
|
mb_substr(str_repeat($pad, ceil($left / $length)), 0, $left, $stringy->encoding)
|
||||||
$paddedStr = '';
|
. $stringy->str
|
||||||
|
. mb_substr(str_repeat($pad, ceil($right / $length)), 0, $right, $stringy->encoding)
|
||||||
if ($padType == 'left') {
|
;
|
||||||
// Repeat the pad, cut it, and prepend
|
|
||||||
$leftPad = str_repeat($padStr, $times);
|
|
||||||
$leftPad = mb_substr($leftPad, 0, $length - $strLength, $encoding);
|
|
||||||
$stringy->str = $leftPad . $stringy->str;
|
|
||||||
} elseif ($padType == 'right') {
|
|
||||||
// Append the repeated pad and get a substring of the given length
|
|
||||||
$stringy->str = $stringy->str . str_repeat($padStr, $times);
|
|
||||||
$stringy->str = mb_substr($stringy->str, 0, $length, $encoding);
|
|
||||||
} else {
|
|
||||||
// Number of times to repeat the padStr on both sides
|
|
||||||
$paddingSize = ($length - $strLength) / 2;
|
|
||||||
$times = ceil($paddingSize / $padStrLength);
|
|
||||||
|
|
||||||
// Favour right padding over left, as with str_pad()
|
|
||||||
$rightPad = str_repeat($padStr, $times);
|
|
||||||
$rightPad = mb_substr($rightPad, 0, ceil($paddingSize), $encoding);
|
|
||||||
|
|
||||||
$leftPad = str_repeat($padStr, $times);
|
|
||||||
$leftPad = mb_substr($leftPad, 0, floor($paddingSize), $encoding);
|
|
||||||
|
|
||||||
$stringy->str = $leftPad . $stringy->str . $rightPad;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $stringy;
|
return $stringy;
|
||||||
}
|
}
|
||||||
@@ -558,11 +523,11 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*
|
*
|
||||||
* @param int $length Desired string length after padding
|
* @param int $length Desired string length after padding
|
||||||
* @param string $padStr String used to pad, defaults to space
|
* @param string $padStr String used to pad, defaults to space
|
||||||
* @return Stringy Object with a left padded $str
|
* @return Stringy String with padding applied.
|
||||||
*/
|
*/
|
||||||
public function padLeft($length, $padStr = ' ')
|
public function padLeft($length, $padStr = ' ')
|
||||||
{
|
{
|
||||||
return $this->pad($length, $padStr, 'left');
|
return $this->pad($length - $this->length(), 0, $padStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -571,11 +536,11 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*
|
*
|
||||||
* @param int $length Desired string length after padding
|
* @param int $length Desired string length after padding
|
||||||
* @param string $padStr String used to pad, defaults to space
|
* @param string $padStr String used to pad, defaults to space
|
||||||
* @return Stringy Object with a right padded $str
|
* @return Stringy String with padding applied.
|
||||||
*/
|
*/
|
||||||
public function padRight($length, $padStr = ' ')
|
public function padRight($length, $padStr = ' ')
|
||||||
{
|
{
|
||||||
return $this->pad($length, $padStr, 'right');
|
return $this->pad(0, $length - $this->length(), $padStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -584,11 +549,13 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*
|
*
|
||||||
* @param int $length Desired string length after padding
|
* @param int $length Desired string length after padding
|
||||||
* @param string $padStr String used to pad, defaults to space
|
* @param string $padStr String used to pad, defaults to space
|
||||||
* @return Stringy The padded string
|
* @return Stringy String with padding applied.
|
||||||
*/
|
*/
|
||||||
public function padBoth($length, $padStr = ' ')
|
public function padBoth($length, $padStr = ' ')
|
||||||
{
|
{
|
||||||
return $this->pad($length, $padStr, 'both');
|
$padding = $length - $this->length();
|
||||||
|
|
||||||
|
return $this->pad(floor($padding / 2), ceil($padding / 2), $padStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -181,52 +181,15 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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('foo bar_*_', 'foo bar', 10, '_*', 'right'),
|
|
||||||
array('fòô bàř ', 'fòô bàř', 9, ' ', 'right', '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')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function padLeftProvider()
|
public function padLeftProvider()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array(' foo bar', 'foo bar', 9),
|
array(' foo bar', 'foo bar', 9),
|
||||||
|
array('_*foo bar', 'foo bar', 9, '_*'),
|
||||||
array('_*_foo bar', 'foo bar', 10, '_*'),
|
array('_*_foo bar', 'foo bar', 10, '_*'),
|
||||||
|
array(' fòô bàř', 'fòô bàř', 9, ' ', 'UTF-8'),
|
||||||
|
array('¬øfòô bàř', 'fòô bàř', 9, '¬ø', 'UTF-8'),
|
||||||
|
array('¬ø¬fòô bàř', 'fòô bàř', 10, '¬ø', 'UTF-8'),
|
||||||
array('¬ø¬øfòô bàř', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
array('¬ø¬øfòô bàř', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -235,7 +198,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array('foo bar ', 'foo bar', 9),
|
array('foo bar ', 'foo bar', 9),
|
||||||
|
array('foo bar_*', 'foo bar', 9, '_*'),
|
||||||
array('foo bar_*_', 'foo bar', 10, '_*'),
|
array('foo bar_*_', 'foo bar', 10, '_*'),
|
||||||
|
array('fòô bàř ', 'fòô bàř', 9, ' ', 'UTF-8'),
|
||||||
|
array('fòô bàř¬ø', 'fòô bàř', 9, '¬ø', 'UTF-8'),
|
||||||
|
array('fòô bàř¬ø¬', 'fòô bàř', 10, '¬ø', 'UTF-8'),
|
||||||
array('fòô bàř¬ø¬ø', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
array('fòô bàř¬ø¬ø', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -245,7 +212,14 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
return array(
|
return array(
|
||||||
array('foo bar ', 'foo bar', 8),
|
array('foo bar ', 'foo bar', 8),
|
||||||
array(' foo bar ', 'foo bar', 9, ' '),
|
array(' foo bar ', 'foo bar', 9, ' '),
|
||||||
|
array('fòô bàř ', 'fòô bàř', 8, ' ', 'UTF-8'),
|
||||||
|
array(' fòô bàř ', 'fòô bàř', 9, ' ', 'UTF-8'),
|
||||||
|
array('fòô bàř¬', 'fòô bàř', 8, '¬ø', 'UTF-8'),
|
||||||
|
array('¬fòô bàř¬', 'fòô bàř', 9, '¬ø', 'UTF-8'),
|
||||||
|
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬ø', 'UTF-8'),
|
||||||
|
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
||||||
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬øÿ', 'UTF-8'),
|
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬øÿ', 'UTF-8'),
|
||||||
|
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬øÿ', 'UTF-8'),
|
||||||
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'UTF-8')
|
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'UTF-8')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@@ -140,25 +140,6 @@ class StaticStringyTestCase extends CommonTest
|
|||||||
$this->assertEquals($expected, $result);
|
$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()
|
* @dataProvider padLeftProvider()
|
||||||
*/
|
*/
|
||||||
|
@@ -309,28 +309,6 @@ class StringyTestCase extends CommonTest
|
|||||||
$this->assertEquals($str, $stringy);
|
$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()
|
* @dataProvider padLeftProvider()
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user