Skip to content

Commit

Permalink
[7.x] Introduced basic padding (both, left, right) methods to Str and…
Browse files Browse the repository at this point in the history
… Stringable… (#34053)

* Introduced basic padding (both, left, right) methods to Str and Stringable.

* Addressed some CS issues.
  • Loading branch information
telkins committed Aug 28, 2020
1 parent 2bad3ad commit 6891cb5
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Illuminate/Support/Str.php
Expand Up @@ -370,6 +370,45 @@ public static function words($value, $words = 100, $end = '...')
return rtrim($matches[0]).$end;
}

/**
* Pad both sides of a string with another.
*
* @param string $value
* @param int $length
* @param string $pad
* @return string
*/
public static function padBoth($value, $length, $pad = ' ')
{
return str_pad($value, $length, $pad, STR_PAD_BOTH);
}

/**
* Pad the left side of a string with another.
*
* @param string $value
* @param int $length
* @param string $pad
* @return string
*/
public static function padLeft($value, $length, $pad = ' ')
{
return str_pad($value, $length, $pad, STR_PAD_LEFT);
}

/**
* Pad the right side of a string with another.
*
* @param string $value
* @param int $length
* @param string $pad
* @return string
*/
public static function padRight($value, $length, $pad = ' ')
{
return str_pad($value, $length, $pad, STR_PAD_RIGHT);
}

/**
* Parse a Class[@]method style callback into class and method.
*
Expand Down
36 changes: 36 additions & 0 deletions src/Illuminate/Support/Stringable.php
Expand Up @@ -338,6 +338,42 @@ public function matchAll($pattern)
return collect($matches[1] ?? $matches[0]);
}

/**
* Pad both sides of the string with another.
*
* @param int $length
* @param string $pad
* @return static
*/
public function padBoth($length, $pad = ' ')
{
return new static(Str::padBoth($this->value, $length, $pad));
}

/**
* Pad the left side of the string with another.
*
* @param int $length
* @param string $pad
* @return static
*/
public function padLeft($length, $pad = ' ')
{
return new static(Str::padLeft($this->value, $length, $pad));
}

/**
* Pad the right side of the string with another.
*
* @param int $length
* @param string $pad
* @return static
*/
public function padRight($length, $pad = ' ')
{
return new static(Str::padRight($this->value, $length, $pad));
}

/**
* Parse a Class@method style callback into class and method.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Support/SupportStrTest.php
Expand Up @@ -462,6 +462,24 @@ public function testAsciiNull()
$this->assertSame('', Str::slug(null));
}

public function testPadBoth()
{
$this->assertSame('__Alien___', Str::padBoth('Alien', 10, '_'));
$this->assertSame(' Alien ', Str::padBoth('Alien', 10));
}

public function testPadLeft()
{
$this->assertSame('-=-=-Alien', Str::padLeft('Alien', 10, '-='));
$this->assertSame(' Alien', Str::padLeft('Alien', 10));
}

public function testPadRight()
{
$this->assertSame('Alien-----', Str::padRight('Alien', 10, '-'));
$this->assertSame('Alien ', Str::padRight('Alien', 10));
}

public function validUuidList()
{
return [
Expand Down
18 changes: 18 additions & 0 deletions tests/Support/SupportStringableTest.php
Expand Up @@ -510,4 +510,22 @@ public function testSubstrCount()
$this->assertSame(3, $this->stringable('laravelPHPFramework')->substrCount('a', 1, -2));
$this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('a', -10, -3));
}

public function testPadBoth()
{
$this->assertSame('__Alien___', (string) $this->stringable('Alien')->padBoth(10, '_'));
$this->assertSame(' Alien ', (string) $this->stringable('Alien')->padBoth(10));
}

public function testPadLeft()
{
$this->assertSame('-=-=-Alien', (string) $this->stringable('Alien')->padLeft(10, '-='));
$this->assertSame(' Alien', (string) $this->stringable('Alien')->padLeft(10));
}

public function testPadRight()
{
$this->assertSame('Alien-----', (string) $this->stringable('Alien')->padRight(10, '-'));
$this->assertSame('Alien ', (string) $this->stringable('Alien')->padRight(10));
}
}

0 comments on commit 6891cb5

Please sign in to comment.