Skip to content

Commit 6891cb5

Browse files
authored
[7.x] Introduced basic padding (both, left, right) methods to Str and Stringable… (#34053)
* Introduced basic padding (both, left, right) methods to Str and Stringable. * Addressed some CS issues.
1 parent 2bad3ad commit 6891cb5

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,45 @@ public static function words($value, $words = 100, $end = '...')
370370
return rtrim($matches[0]).$end;
371371
}
372372

373+
/**
374+
* Pad both sides of a string with another.
375+
*
376+
* @param string $value
377+
* @param int $length
378+
* @param string $pad
379+
* @return string
380+
*/
381+
public static function padBoth($value, $length, $pad = ' ')
382+
{
383+
return str_pad($value, $length, $pad, STR_PAD_BOTH);
384+
}
385+
386+
/**
387+
* Pad the left side of a string with another.
388+
*
389+
* @param string $value
390+
* @param int $length
391+
* @param string $pad
392+
* @return string
393+
*/
394+
public static function padLeft($value, $length, $pad = ' ')
395+
{
396+
return str_pad($value, $length, $pad, STR_PAD_LEFT);
397+
}
398+
399+
/**
400+
* Pad the right side of a string with another.
401+
*
402+
* @param string $value
403+
* @param int $length
404+
* @param string $pad
405+
* @return string
406+
*/
407+
public static function padRight($value, $length, $pad = ' ')
408+
{
409+
return str_pad($value, $length, $pad, STR_PAD_RIGHT);
410+
}
411+
373412
/**
374413
* Parse a Class[@]method style callback into class and method.
375414
*

src/Illuminate/Support/Stringable.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,42 @@ public function matchAll($pattern)
338338
return collect($matches[1] ?? $matches[0]);
339339
}
340340

341+
/**
342+
* Pad both sides of the string with another.
343+
*
344+
* @param int $length
345+
* @param string $pad
346+
* @return static
347+
*/
348+
public function padBoth($length, $pad = ' ')
349+
{
350+
return new static(Str::padBoth($this->value, $length, $pad));
351+
}
352+
353+
/**
354+
* Pad the left side of the string with another.
355+
*
356+
* @param int $length
357+
* @param string $pad
358+
* @return static
359+
*/
360+
public function padLeft($length, $pad = ' ')
361+
{
362+
return new static(Str::padLeft($this->value, $length, $pad));
363+
}
364+
365+
/**
366+
* Pad the right side of the string with another.
367+
*
368+
* @param int $length
369+
* @param string $pad
370+
* @return static
371+
*/
372+
public function padRight($length, $pad = ' ')
373+
{
374+
return new static(Str::padRight($this->value, $length, $pad));
375+
}
376+
341377
/**
342378
* Parse a Class@method style callback into class and method.
343379
*

tests/Support/SupportStrTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,24 @@ public function testAsciiNull()
462462
$this->assertSame('', Str::slug(null));
463463
}
464464

465+
public function testPadBoth()
466+
{
467+
$this->assertSame('__Alien___', Str::padBoth('Alien', 10, '_'));
468+
$this->assertSame(' Alien ', Str::padBoth('Alien', 10));
469+
}
470+
471+
public function testPadLeft()
472+
{
473+
$this->assertSame('-=-=-Alien', Str::padLeft('Alien', 10, '-='));
474+
$this->assertSame(' Alien', Str::padLeft('Alien', 10));
475+
}
476+
477+
public function testPadRight()
478+
{
479+
$this->assertSame('Alien-----', Str::padRight('Alien', 10, '-'));
480+
$this->assertSame('Alien ', Str::padRight('Alien', 10));
481+
}
482+
465483
public function validUuidList()
466484
{
467485
return [

tests/Support/SupportStringableTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,4 +510,22 @@ public function testSubstrCount()
510510
$this->assertSame(3, $this->stringable('laravelPHPFramework')->substrCount('a', 1, -2));
511511
$this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('a', -10, -3));
512512
}
513+
514+
public function testPadBoth()
515+
{
516+
$this->assertSame('__Alien___', (string) $this->stringable('Alien')->padBoth(10, '_'));
517+
$this->assertSame(' Alien ', (string) $this->stringable('Alien')->padBoth(10));
518+
}
519+
520+
public function testPadLeft()
521+
{
522+
$this->assertSame('-=-=-Alien', (string) $this->stringable('Alien')->padLeft(10, '-='));
523+
$this->assertSame(' Alien', (string) $this->stringable('Alien')->padLeft(10));
524+
}
525+
526+
public function testPadRight()
527+
{
528+
$this->assertSame('Alien-----', (string) $this->stringable('Alien')->padRight(10, '-'));
529+
$this->assertSame('Alien ', (string) $this->stringable('Alien')->padRight(10));
530+
}
513531
}

0 commit comments

Comments
 (0)