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 authored Aug 28, 2020
1 parent 893a171 commit 8ee8e0e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Str.php
Original file line number Diff line number Diff line change
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 Stringable.php
Original file line number Diff line number Diff line change
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

0 comments on commit 8ee8e0e

Please sign in to comment.