Skip to content

Commit

Permalink
[10.x] Add toBase64() and fromBase64() methods to Stringable and …
Browse files Browse the repository at this point in the history
…Str classes (#49984)

* Add toBase64 and fromBase64 methods to Stringable

* Update SupportStringableTest.php

* Call __toString

* Styleci

* More styleci shenannys

* Adjust test

* Add toBase64 & fromBase64 to Str class + tests

* Styleci fix

* Update Stringable.php

* Update Str.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
mtownsend5512 and taylorotwell committed Feb 8, 2024
1 parent b8cf546 commit 80e0fec
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,29 @@ public static function take($string, int $limit): string
return static::substr($string, 0, $limit);
}

/**
* Convert the given string to Base64 encoding.
*
* @param string $string
* @return string
*/
public static function toBase64($string): string
{
return base64_encode($string);
}

/**
* Decode the given Base64 encoded string.
*
* @param string $string
* @param bool $strict
* @return void
*/
public static function fromBase64($string, $strict = false)
{
return base64_decode($string, $strict);
}

/**
* Make a string's first character lowercase.
*
Expand Down
21 changes: 21 additions & 0 deletions Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,27 @@ public function toHtmlString()
return new HtmlString($this->value);
}

/**
* Convert the string to Base64 encoding.
*
* @return void
*/
public function toBase64()
{
return new static(base64_encode($this->value));
}

/**
* Decode the Base64 encoded string.
*
* @param bool $strict
* @return void
*/
public function fromBase64($strict = false)
{
return new static(base64_decode($this->value, $strict));
}

/**
* Dump the string.
*
Expand Down

0 comments on commit 80e0fec

Please sign in to comment.