Skip to content

Commit 6167e97

Browse files
authored
Support numbers and fromBase64 methods for \Hyperf\Stringable\Str (#6799)
* Support numbers and fromBase64 to \Hyperf\Stringable\Str * Up CHANGELOG
1 parent 71e5e5c commit 6167e97

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Diff for: src/Str.php

+16
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,22 @@ public static function wordWrap($string, $characters = 75, $break = "\n", $cutLo
13761376
return wordwrap($string, $characters, $break, $cutLongWords);
13771377
}
13781378

1379+
/**
1380+
* Remove all non-numeric characters from a string.
1381+
*/
1382+
public static function numbers(array|string $value): array|string
1383+
{
1384+
return preg_replace('/[^0-9]/', '', $value);
1385+
}
1386+
1387+
/**
1388+
* Decode the given Base64 encoded string.
1389+
*/
1390+
public static function fromBase64(string $string, bool $strict = false): false|string
1391+
{
1392+
return base64_decode($string, $strict);
1393+
}
1394+
13791395
/**
13801396
* Returns the replacements for the ascii method.
13811397
* Note: Adapted from Stringy\Stringy.

Diff for: tests/StrTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -1114,4 +1114,21 @@ public function testReplaceMatches()
11141114
$this->assertSame('http://hyperf.io', Str::replaceMatches('/^https:\/\//', 'http://', 'https://hyperf.io'));
11151115
$this->assertSame('http://hyperf.io', Str::replaceMatches('/^https:\/\//', fn ($matches) => 'http://', 'https://hyperf.io'));
11161116
}
1117+
1118+
public function testNumbers(): void
1119+
{
1120+
$this->assertSame('5551234567', Str::numbers('(555) 123-4567'));
1121+
$this->assertSame('443', Str::numbers('L4r4v3l!'));
1122+
$this->assertSame('', Str::numbers('Laravel!'));
1123+
1124+
$arrayValue = ['(555) 123-4567', 'L4r4v3l', 'Laravel!'];
1125+
$arrayExpected = ['5551234567', '443', ''];
1126+
$this->assertSame($arrayExpected, Str::numbers($arrayValue));
1127+
}
1128+
1129+
public function testFromBase64(): void
1130+
{
1131+
$this->assertSame('foo', Str::fromBase64(base64_encode('foo')));
1132+
$this->assertSame('foobar', Str::fromBase64(base64_encode('foobar'), true));
1133+
}
11171134
}

0 commit comments

Comments
 (0)