refactor(string): extract transliterate to Shared\Helpers\StringHelper
This commit is contained in:
parent
0805017a70
commit
54a199d4e0
3 changed files with 72 additions and 8 deletions
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Infra\Adapters\Parsers;
|
||||
|
||||
use Shared\Helpers\StringHelper;
|
||||
|
||||
final readonly class IncoStringParser
|
||||
{
|
||||
/**
|
||||
|
|
@ -220,14 +222,7 @@ final readonly class IncoStringParser
|
|||
*/
|
||||
public static function slugify(string $value): string
|
||||
{
|
||||
if (\function_exists('transliterator_transliterate')) {
|
||||
$transliterated = \transliterator_transliterate('Any-Latin; Latin-ASCII', $value);
|
||||
if ($transliterated !== false) {
|
||||
return strtolower(trim($transliterated));
|
||||
}
|
||||
}
|
||||
|
||||
$ascii = (string) iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
|
||||
$ascii = StringHelper::transliterate($value);
|
||||
|
||||
return strtolower(trim($ascii));
|
||||
}
|
||||
|
|
|
|||
28
src/Shared/Helpers/StringHelper.php
Normal file
28
src/Shared/Helpers/StringHelper.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shared\Helpers;
|
||||
|
||||
final readonly class StringHelper
|
||||
{
|
||||
private function __construct()
|
||||
{
|
||||
// Static class
|
||||
}
|
||||
|
||||
/**
|
||||
* Transliterates UTF-8 text to ASCII using ICU Transliterator with iconv fallback.
|
||||
*/
|
||||
public static function transliterate(string $value): string
|
||||
{
|
||||
if (\function_exists('transliterator_transliterate')) {
|
||||
$transliterated = \transliterator_transliterate('Any-Latin; Latin-ASCII', $value);
|
||||
if ($transliterated !== false) {
|
||||
return $transliterated;
|
||||
}
|
||||
}
|
||||
|
||||
return (string) iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
|
||||
}
|
||||
}
|
||||
41
tests/Unit/Shared/Helpers/StringHelperTest.php
Normal file
41
tests/Unit/Shared/Helpers/StringHelperTest.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Shared\Helpers;
|
||||
|
||||
use Shared\Helpers\StringHelper;
|
||||
|
||||
covers(StringHelper::class);
|
||||
|
||||
describe('StringHelper Unit Tests', function (): void {
|
||||
it('prevents direct instantiation', function (): void {
|
||||
// GIVEN
|
||||
$reflection = new \ReflectionClass(StringHelper::class);
|
||||
|
||||
// WHEN & THEN
|
||||
expect(fn () => $reflection->newInstance())
|
||||
->toThrow(\ReflectionException::class);
|
||||
});
|
||||
|
||||
it('transliterates accented and special characters to ascii', function (): void {
|
||||
// GIVEN
|
||||
$input = 'Émincés & Rôti, crème brûlée, ça va ?';
|
||||
|
||||
// WHEN
|
||||
$result = StringHelper::transliterate($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe('Eminces & Roti, creme brulee, ca va ?');
|
||||
});
|
||||
|
||||
it('handles empty strings and plain ascii strings without modification', function (): void {
|
||||
// GIVEN
|
||||
$empty = '';
|
||||
$ascii = 'hello world 123';
|
||||
|
||||
// WHEN & THEN
|
||||
expect(StringHelper::transliterate($empty))->toBe('')
|
||||
->and(StringHelper::transliterate($ascii))->toBe('hello world 123');
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue