Skip to content

Commit f89fb45

Browse files
liamh101Liam Hacketttaylorotwell
authored
[8.x] Add Transliterate shortcut to the Str helper (#40681)
* Add Transliterate shortcut to the Str helper * Remove whitespace * Fix variable typo * formatting Co-authored-by: Liam Hackett <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent eb59122 commit f89fb45

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/Illuminate/Support/Str.php

+13
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ public static function ascii($value, $language = 'en')
9999
return ASCII::to_ascii((string) $value, $language);
100100
}
101101

102+
/**
103+
* Transliterate a string to its closest ASCII representation.
104+
*
105+
* @param string $string
106+
* @param string|null $unknown
107+
* @param bool|null $strict
108+
* @return string
109+
*/
110+
public static function transliterate($string, $unknown = '?', $strict = false)
111+
{
112+
return ASCII::to_transliterate($string, $unknown, $strict);
113+
}
114+
102115
/**
103116
* Get the portion of a string before the first occurrence of a given value.
104117
*

tests/Support/SupportStrTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,42 @@ public function testRepeat()
672672
$this->assertSame('aaaaa', Str::repeat('a', 5));
673673
$this->assertSame('', Str::repeat('', 5));
674674
}
675+
676+
/**
677+
* @dataProvider specialCharacterProvider
678+
*/
679+
public function testTransliterate(string $value, string $expected): void
680+
{
681+
$this->assertSame($expected, Str::transliterate($value));
682+
}
683+
684+
public function specialCharacterProvider(): array
685+
{
686+
return [
687+
['ⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ', 'abcdefghijklmnopqrstuvwxyz'],
688+
['⓪①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳', '01234567891011121314151617181920'],
689+
['⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾', '12345678910'],
690+
['⓿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴', '011121314151617181920'],
691+
['ⓣⓔⓢⓣ@ⓛⓐⓡⓐⓥⓔⓛ.ⓒⓞⓜ', '[email protected]'],
692+
['🎂', '?'],
693+
['abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz'],
694+
['0123456789', '0123456789'],
695+
];
696+
}
697+
698+
public function testTransliterateOverrideUnknown(): void
699+
{
700+
$this->assertSame('HHH', Str::transliterate('🎂🚧🏆', 'H'));
701+
$this->assertSame('Hello', Str::transliterate('🎂', 'Hello'));
702+
}
703+
704+
/**
705+
* @dataProvider specialCharacterProvider
706+
*/
707+
public function testTransliterateStrict(string $value, string $expected): void
708+
{
709+
$this->assertSame($expected, Str::transliterate($value, '?', true));
710+
}
675711
}
676712

677713
class StringableObjectStub

0 commit comments

Comments
 (0)