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 <[email protected]>
  • Loading branch information
mtownsend5512 and taylorotwell authored Feb 8, 2024
1 parent 7fac93b commit 1d0f002
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Illuminate/Support/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 src/Illuminate/Support/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
12 changes: 12 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,18 @@ public function testPasswordCreation()
Str::of(Str::password())->contains(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
);
}

public function testToBase64()
{
$this->assertSame(base64_encode('foo'), Str::toBase64('foo'));
$this->assertSame(base64_encode('foobar'), Str::toBase64('foobar'));
}

public function testFromBase64()
{
$this->assertSame('foo', Str::fromBase64(base64_encode('foo')));
$this->assertSame('foobar', Str::fromBase64(base64_encode('foobar'), true));
}
}

class StringableObjectStub
Expand Down
14 changes: 14 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1302,4 +1302,18 @@ public function testArrayAccess()
$this->assertTrue(isset($str[2]));
$this->assertFalse(isset($str[10]));
}

public function testToBase64()
{
$this->assertSame(base64_encode('foo'), (string) $this->stringable('foo')->toBase64());
$this->assertSame(base64_encode('foobar'), (string) $this->stringable('foobar')->toBase64());
$this->assertSame(base64_encode('foobarbaz'), (string) $this->stringable('foobarbaz')->toBase64());
}

public function testFromBase64()
{
$this->assertSame('foo', (string) $this->stringable(base64_encode('foo'))->fromBase64());
$this->assertSame('foobar', (string) $this->stringable(base64_encode('foobar'))->fromBase64(true));
$this->assertSame('foobarbaz', (string) $this->stringable(base64_encode('foobarbaz'))->fromBase64());
}
}

0 comments on commit 1d0f002

Please sign in to comment.