diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 76dfa9d8852c..59158f4c6ee7 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -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. * diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 0ccb9f480e02..82ecac845c65 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -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. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 1bbcb4f54817..87bc3c0956c4 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -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 [ diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index d27f9ddf6d85..f742dfba6753 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -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)); + } }