diff --git a/src/Infra/Adapters/Parsers/IncoStringParser.php b/src/Infra/Adapters/Parsers/IncoStringParser.php index b501602..64aebf1 100644 --- a/src/Infra/Adapters/Parsers/IncoStringParser.php +++ b/src/Infra/Adapters/Parsers/IncoStringParser.php @@ -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)); } diff --git a/src/Shared/Helpers/StringHelper.php b/src/Shared/Helpers/StringHelper.php new file mode 100644 index 0000000..6c4fc30 --- /dev/null +++ b/src/Shared/Helpers/StringHelper.php @@ -0,0 +1,28 @@ + $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'); + }); +});