Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Introduced basic padding (both, left, right) methods to Str and Stringable… #34053

Merged
merged 2 commits into from Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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));
}
}