Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add toBase64() and fromBase64() methods to Stringable and Str classes #49984

Merged
merged 10 commits into from
Feb 8, 2024
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());
}
}
Loading