diff --git a/src/Domain/Models/ValueObjects/Brand/BrandName.php b/src/Domain/Models/ValueObjects/Brand/BrandName.php index b6e251a..41913ca 100644 --- a/src/Domain/Models/ValueObjects/Brand/BrandName.php +++ b/src/Domain/Models/ValueObjects/Brand/BrandName.php @@ -5,15 +5,21 @@ declare(strict_types=1); namespace Domain\Models\ValueObjects\Brand; use Shared\DDD\Model\Equatable; +use Shared\Helpers\StringHelper; final readonly class BrandName implements Equatable { + public string $value; + public function __construct( - public string $value, + string $value, ) { - if (empty(trim($this->value))) { + $trimmed = trim($value); + if ($trimmed === '') { throw new \DomainException('Brand name cannot be empty.'); } + + $this->value = StringHelper::titleCase($trimmed); } #[\Override] diff --git a/src/Domain/Services/BrandCodeGenerator.php b/src/Domain/Services/BrandCodeGenerator.php index efed427..5c4d721 100644 --- a/src/Domain/Services/BrandCodeGenerator.php +++ b/src/Domain/Services/BrandCodeGenerator.php @@ -6,6 +6,7 @@ namespace Domain\Services; use Domain\Models\ValueObjects\Brand\BrandCode; use Domain\Models\ValueObjects\Brand\BrandName; +use Shared\Helpers\StringHelper; final readonly class BrandCodeGenerator { @@ -118,9 +119,7 @@ final readonly class BrandCodeGenerator private function normalizeString(string $value): string { - $ascii = transliterator_transliterate('Any-Latin; Latin-ASCII; Upper();', $value); - - return is_string($ascii) ? $ascii : mb_strtoupper($value); + return mb_strtoupper(StringHelper::transliterate($value)); } private function randomTwoDigits(): string diff --git a/src/Infra/Adapters/Mappers/IncoMapper.php b/src/Infra/Adapters/Mappers/IncoMapper.php index 875b247..9c7d40e 100644 --- a/src/Infra/Adapters/Mappers/IncoMapper.php +++ b/src/Infra/Adapters/Mappers/IncoMapper.php @@ -15,6 +15,7 @@ use Infra\Adapters\Parsers\IncoIngredientParser; use Infra\Adapters\Parsers\IncoParser; use Infra\Adapters\Parsers\IncoStringParser; use Shared\Helpers\DatesHelper; +use Shared\Helpers\StringHelper; final readonly class IncoMapper { @@ -72,7 +73,7 @@ final readonly class IncoMapper $name = IncoStringParser::beautifyText($rawDesc); if ($name === null || trim($name) === '') { - $name = $rawDenom !== '' ? IncoStringParser::words($rawDenom, 4, '') : "Produit {$incoId->value}"; + $name = $rawDenom !== '' ? StringHelper::words($rawDenom, 4, '') : "Produit {$incoId->value}"; } $label = IncoStringParser::beautifyName($rawDenom !== '' ? $rawDenom : ($name !== '' ? $name : "Produit {$incoId->value}")); diff --git a/src/Infra/Adapters/Mappers/IncoShortMapper.php b/src/Infra/Adapters/Mappers/IncoShortMapper.php index d9c3deb..3301ef7 100644 --- a/src/Infra/Adapters/Mappers/IncoShortMapper.php +++ b/src/Infra/Adapters/Mappers/IncoShortMapper.php @@ -8,6 +8,7 @@ use Domain\Models\Entities\Inco\IncoShortSheet; use Domain\Models\ValueObjects\Inco\IncoId; use Infra\Adapters\Parsers\IncoStringParser; use Shared\Helpers\DatesHelper; +use Shared\Helpers\StringHelper; final readonly class IncoShortMapper { @@ -40,7 +41,7 @@ final readonly class IncoShortMapper $name = IncoStringParser::beautifyText($rawDesc); if ($name === null || trim($name) === '') { - $name = $rawDenom !== '' ? IncoStringParser::words($rawDenom, 4, '') : "Produit {$incoId->value}"; + $name = $rawDenom !== '' ? StringHelper::words($rawDenom, 4, '') : "Produit {$incoId->value}"; } $label = IncoStringParser::beautifyName($rawDenom !== '' ? $rawDenom : ($name !== '' ? $name : "Produit {$incoId->value}")); diff --git a/src/Infra/Adapters/Parsers/IncoIngredientParser.php b/src/Infra/Adapters/Parsers/IncoIngredientParser.php index 492117a..4fa3acf 100644 --- a/src/Infra/Adapters/Parsers/IncoIngredientParser.php +++ b/src/Infra/Adapters/Parsers/IncoIngredientParser.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Infra\Adapters\Parsers; use Domain\Models\ValueObjects\Inco\Ingredients; +use Shared\Helpers\StringHelper; final readonly class IncoIngredientParser { @@ -174,7 +175,7 @@ final readonly class IncoIngredientParser $elements = self::tokenizeIngredientPart($tokenString, ':', 2); $labelRaw = $elements[0] ?? ''; - $labelSlug = IncoStringParser::slugify($labelRaw); + $labelSlug = StringHelper::slugify($labelRaw, ' '); $hasLabel = count($elements) > 1; $trimmedLabel = trim($labelRaw); @@ -182,7 +183,7 @@ final readonly class IncoIngredientParser $labelHasParen = (bool) preg_match('/[()]/', $trimmedLabel); $labelIsTooLong = mb_strlen($trimmedLabel, 'UTF-8') > 40; - $tokenSlug = IncoStringParser::slugify($tokenString); + $tokenSlug = StringHelper::slugify($tokenString, ' '); $isInfoLine = str_starts_with($tokenSlug, 'fabrique dans') || str_starts_with($tokenSlug, 'certifie') || str_starts_with($tokenSlug, 'a consommer') @@ -276,12 +277,12 @@ final readonly class IncoIngredientParser $ingredientPartString = implode('', $result); $delimiters = "\n\t{$charToEscape}"; - $tok = IncoStringParser::mbStrtok($delimiters, $ingredientPartString); + $tok = StringHelper::mbStrtok($delimiters, $ingredientPartString); $parts = []; while ($tok !== false) { $parts[] = $tok; - $tok = IncoStringParser::mbStrtok($delimiters); + $tok = StringHelper::mbStrtok($delimiters); } $limitChar = chr(27); diff --git a/src/Infra/Adapters/Parsers/IncoParser.php b/src/Infra/Adapters/Parsers/IncoParser.php index 3a2c0d1..9e769d0 100644 --- a/src/Infra/Adapters/Parsers/IncoParser.php +++ b/src/Infra/Adapters/Parsers/IncoParser.php @@ -12,6 +12,7 @@ use Domain\Models\ValueObjects\Inco\Nutriscore; use Domain\Models\ValueObjects\Inco\PortionAmount; use Domain\Models\ValueObjects\Inco\PortionType; use Domain\Models\ValueObjects\Inco\StorageCondition; +use Shared\Helpers\StringHelper; final readonly class IncoParser { @@ -41,12 +42,12 @@ final readonly class IncoParser } $delimiters = '–,'; - $tok = IncoStringParser::mbStrtok($delimiters, $rawText); + $tok = StringHelper::mbStrtok($delimiters, $rawText); $parts = []; while ($tok !== false) { $parts[] = $tok; - $tok = IncoStringParser::mbStrtok($delimiters); + $tok = StringHelper::mbStrtok($delimiters); } $parts = array_values(array_filter($parts, static function (string $item): bool { @@ -57,7 +58,7 @@ final readonly class IncoParser $parts = array_map(static function (string $item): string { $value = IncoStringParser::toPhrase($item); - $cleaned = IncoStringParser::protectiveDataFilter($value); + $cleaned = StringHelper::sanitize($value); return $cleaned ?? ''; }, $parts); @@ -203,7 +204,7 @@ final readonly class IncoParser return null; } - $cleaned = IncoStringParser::protectiveDataFilter($str); + $cleaned = StringHelper::sanitize($str); if ($cleaned === null || trim($cleaned) === '') { return null; } @@ -361,12 +362,12 @@ final readonly class IncoParser private static function tokenize(string $value): array { $delimiters = " \n\tà/\\-_de"; - $tok = IncoStringParser::mbStrtok($delimiters, $value); + $tok = StringHelper::mbStrtok($delimiters, $value); $parts = []; while ($tok !== false) { $parts[] = $tok; - $tok = IncoStringParser::mbStrtok($delimiters); + $tok = StringHelper::mbStrtok($delimiters); } return $parts; diff --git a/src/Infra/Adapters/Parsers/IncoStringParser.php b/src/Infra/Adapters/Parsers/IncoStringParser.php index 64aebf1..775982b 100644 --- a/src/Infra/Adapters/Parsers/IncoStringParser.php +++ b/src/Infra/Adapters/Parsers/IncoStringParser.php @@ -4,42 +4,8 @@ declare(strict_types=1); namespace Infra\Adapters\Parsers; -use Shared\Helpers\StringHelper; - final readonly class IncoStringParser { - /** - * Nettoie et normalise une ligne texte (pas de HTML, pas de caractères de contrôle, UTF-8 valide). - */ - public static function protectiveDataFilter(?string $line): ?string - { - if ($line === null || $line === '') { - return $line; - } - - // Decode HTML entities then strip tags - $line = html_entity_decode($line, ENT_QUOTES | ENT_HTML5, 'UTF-8'); - $line = strip_tags($line); - - // Normalize newlines, temporarily convert to
to preserve paragraph breaks - $line = str_replace(["\r\n", "\r"], "\n", $line); - $line = str_replace("\n", '
', $line); - - // Ensure valid UTF-8 (handling Windows-1252 / ISO-8859-1 legacy strings) - $line = self::ensureUtf8($line); - - // Remove astral symbols (4-byte UTF-8) that may break storage/processing - $line = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $line) ?? ''; - - // Remove control/non-printable characters but keep TAB(0x09), LF(0x0A) and CR(0x0D) - $line = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F\xA0\xAD]/u', '', $line) ?? ''; - - // Revert
back to newlines and final strip_tags as safety - $line = str_replace('
', "\n", $line); - - return strip_tags($line); - } - /** * Formate un libellé en phrase standard (1ère lettre en majuscule, sans point final trailing). */ @@ -178,81 +144,4 @@ final readonly class IncoStringParser return str_replace($floatingPointEsc, '.', $result); } - - /** - * Multibyte-safe tokenizer similar to strtok. - */ - public static function mbStrtok(string $delimiters, ?string $str = null): string | false - { - /** @var int $pos */ - static $pos = 0; - /** @var string $string */ - static $string = ''; - - if ($str !== null) { - $pos = 0; - $string = $str; - } - - $token = ''; - $len = mb_strlen($string, 'UTF-8'); - - while ($pos < $len) { - $char = mb_substr($string, $pos, 1, 'UTF-8'); - $pos++; - - if (mb_strpos($delimiters, $char, 0, 'UTF-8') === false) { - $token .= $char; - } else { - if ($token !== '') { - return $token; - } - } - } - - if ($token !== '') { - return $token; - } - - return false; - } - - /** - * Slugify a string for token comparisons. - */ - public static function slugify(string $value): string - { - $ascii = StringHelper::transliterate($value); - - return strtolower(trim($ascii)); - } - - /** - * Truncate a string to a max number of words. - */ - public static function words(string $value, int $words = 100, string $end = ' ...'): string - { - preg_match('/^\s*+(?:\S+\s*+){1,' . $words . '}/u', $value, $matches); - - if (! isset($matches[0]) || mb_strlen($value, 'UTF-8') === mb_strlen($matches[0], 'UTF-8')) { - return $value; - } - - return rtrim($matches[0]) . $end; - } - - private static function ensureUtf8(string $string): string - { - if ($string === '') { - return ''; - } - - if (mb_check_encoding($string, 'UTF-8')) { - return $string; - } - - $converted = mb_convert_encoding($string, 'UTF-8', 'Windows-1252, ISO-8859-15, ISO-8859-1'); - - return is_string($converted) ? $converted : $string; - } } diff --git a/src/Shared/Helpers/StringEncoder.php b/src/Shared/Helpers/StringEncoder.php new file mode 100644 index 0000000..aa1788e --- /dev/null +++ b/src/Shared/Helpers/StringEncoder.php @@ -0,0 +1,330 @@ + + */ + private const array CANONICAL_ENCODINGS = [ + 'UTF-8', + 'Windows-1252', + 'ISO-8859-15', + ]; + + /** + * Lowercase alias -> canonical name. + * + * Windows-1252 absorbs ISO-8859-1/Latin-1 because it is a strict superset: + * bytes 0xA0-0xFF are identical, and 0x80-0x9F are defined in Win-1252 + * but undefined (and useless) in ISO-8859-1. + * + * @var array + */ + private const array ENCODING_ALIASES = [ + // UTF-8 + 'utf8' => 'UTF-8', + 'utf-8' => 'UTF-8', + + // Windows-1252 (absorbs Latin-1 / ISO-8859-1) + 'win1252' => 'Windows-1252', + 'windows1252' => 'Windows-1252', + 'cp1252' => 'Windows-1252', + 'latin1' => 'Windows-1252', + 'latin-1' => 'Windows-1252', + 'iso-8859-1' => 'Windows-1252', + + // ISO-8859-15 (Latin-9) + 'latin9' => 'ISO-8859-15', + 'latin-9' => 'ISO-8859-15', + 'iso-8859-15' => 'ISO-8859-15', + ]; + + /** + * Windows-1252 specific codepoints (0x80-0x9F) absent from ISO-8859-1. + * + * Bytes 0x81, 0x8D, 0x8F, 0x90, 0x9D are officially undefined in Windows-1252. + * They are intentionally absent - encountering them triggers the + * Latin-1 fallback (Unicode codepoint = byte value). + * + * @var array Byte value -> UTF-8 character. + */ + private const array WIN1252_SPECIAL_BYTES = [ + 0x80 => "\u{20AC}", // € + 0x82 => "\u{201A}", // ‚ + 0x83 => "\u{0192}", // ƒ + 0x84 => "\u{201E}", // „ + 0x85 => "\u{2026}", // … + 0x86 => "\u{2020}", // † + 0x87 => "\u{2021}", // ‡ + 0x88 => "\u{02C6}", // ˆ + 0x89 => "\u{2030}", // ‰ + 0x8A => "\u{0160}", // Š + 0x8B => "\u{2039}", // ‹ + 0x8C => "\u{0152}", // Œ + 0x8E => "\u{017D}", // Ž + 0x91 => "\u{2018}", // ' + 0x92 => "\u{2019}", // ' + 0x93 => "\u{201C}", // " + 0x94 => "\u{201D}", // " + 0x95 => "\u{2022}", // • + 0x96 => "\u{2013}", // – + 0x97 => "\u{2014}", // — + 0x98 => "\u{02DC}", // ˜ + 0x99 => "\u{2122}", // ™ + 0x9A => "\u{0161}", // š + 0x9B => "\u{203A}", // › + 0x9C => "\u{0153}", // œ + 0x9E => "\u{017E}", // ž + 0x9F => "\u{0178}", // Ÿ + ]; + + /** + * ISO-8859-15 bytes that differ from ISO-8859-1. + * Only 8 codepoints differ between Latin-1 and Latin-9. + * + * @var array Byte value -> UTF-8 character. + */ + private const array LATIN9_OVERRIDES = [ + 0xA4 => "\u{20AC}", // € (replaces ¤) + 0xA6 => "\u{0160}", // Š (replaces ¦) + 0xA8 => "\u{0161}", // š (replaces ¨) + 0xB4 => "\u{017D}", // Ž (replaces ´) + 0xB8 => "\u{017E}", // ž (replaces ¸) + 0xBC => "\u{0152}", // Œ (replaces ¼) + 0xBD => "\u{0153}", // œ (replaces ½) + 0xBE => "\u{0178}", // Ÿ (replaces ¾) + ]; + + private function __construct() + { + // Static class + } + + /** + * Converts a string from a known encoding to UTF-8. + * + * @param bool $stripBom Remove UTF-8 BOM from the result if present. + * + * @throws \ValueError If encoding is unsupported or the string is invalid. + */ + public static function toUtf8( + string $string, + string $sourceEncoding, + bool $stripBom = true, + ): string { + if ($string === '') { + return ''; + } + + $canonical = self::resolveEncoding($sourceEncoding); + + $result = match ($canonical) { + 'UTF-8' => self::ensureValidUtf8($string), + 'Windows-1252' => self::fromSingleByte($string, self::WIN1252_SPECIAL_BYTES), + 'ISO-8859-15' => self::fromSingleByte($string, self::LATIN9_OVERRIDES), + default => throw new \ValueError(sprintf( + 'Unsupported encoding "%s". Supported: %s', + $canonical, + implode(', ', self::supportedEncodings()), + )) + }; + + return $stripBom ? self::stripUtf8Bom($result) : $result; + } + + /** + * Attempts to detect the encoding and convert to UTF-8. + * + * Detection strategy: + * 1. If the string is valid UTF-8, it is treated as UTF-8. + * 2. Otherwise, the configured fallback encoding is used. + * + * This two-step approach is more reliable than mb_detect_encoding + * for single-byte encodings (which accept any byte sequence). + * + * @param string $fallbackEncoding Encoding to assume when the string is not valid UTF-8. + * Defaults to 'Windows-1252' (covers most Western European legacy data). + * @param bool $stripBom Remove UTF-8 BOM from the result if present. + * + * @throws \ValueError If fallbackEncoding is unsupported or is 'UTF-8'. + */ + public static function detectAndConvertToUtf8( + string $string, + string $fallbackEncoding = 'Windows-1252', + bool $stripBom = true, + ): string { + if ($string === '') { + return ''; + } + + $canonical = self::resolveEncoding($fallbackEncoding); + + if ($canonical === 'UTF-8') { + throw new \ValueError( + 'Fallback encoding cannot be UTF-8. Use toUtf8() for explicit UTF-8 validation.', + ); + } + + // UTF-8 is the only encoding we can reliably detect: + // it has strict byte sequence rules that reject invalid sequences. + if (mb_check_encoding($string, 'UTF-8')) { + $result = $string; + } else { + $result = self::toUtf8($string, $canonical, stripBom: false); + } + + return $stripBom ? self::stripUtf8Bom($result) : $result; + } + + /** + * Converts an array of strings to UTF-8 from a known encoding. + * + * @param list $strings + * @return list + */ + public static function arrayToUtf8( + array $strings, + string $sourceEncoding, + bool $stripBom = true, + ): array { + return array_map( + static fn (string $s): string => self::toUtf8($s, $sourceEncoding, $stripBom), + $strings, + ); + } + + /** + * Returns all supported encoding names (canonical + aliases). + * + * @return list + */ + public static function supportedEncodings(): array + { + return array_values(array_unique([ + ...self::CANONICAL_ENCODINGS, + ...array_keys(self::ENCODING_ALIASES), + ])); + } + + /** + * Resolves a user-supplied encoding name to its canonical form. + * + * @throws \ValueError If encoding is unknown. + */ + private static function resolveEncoding(string $encoding): string + { + $normalized = strtolower(trim($encoding)); + + if (isset(self::ENCODING_ALIASES[$normalized])) { + return self::ENCODING_ALIASES[$normalized]; + } + + $match = array_find( + self::CANONICAL_ENCODINGS, + static fn (string $canonical): bool => strtolower($canonical) === $normalized, + ); + + if ($match !== null) { + return $match; + } + + throw new \ValueError(sprintf( + 'Unsupported encoding "%s". Supported: %s', + $encoding, + implode(', ', self::supportedEncodings()), + )); + } + + /** + * Validates that a string is well-formed UTF-8. + * + * @throws \ValueError If the string contains invalid UTF-8 sequences. + */ + private static function ensureValidUtf8(string $string): string + { + if (! mb_check_encoding($string, 'UTF-8')) { + throw new \ValueError('String is not valid UTF-8.'); + } + + return $string; + } + + /** + * Converts a single-byte encoded string to UTF-8. + * + * All supported single-byte encodings share the same base: + * - 0x00-0x7F: ASCII (identity) + * - 0x80-0xFF: Encoding-specific - use overrides map first, then Latin-1 identity. + * + * @param array $overrides Byte -> UTF-8 mapping for encoding-specific codepoints. + * For Windows-1252: the 0x80-0x9F special range. + * For ISO-8859-15: the 8 bytes that differ from Latin-1. + */ + private static function fromSingleByte(string $string, array $overrides): string + { + /** @var array $bytes keyed from 1 (unpack behavior) */ + $bytes = unpack('C*', $string); + $parts = []; + + foreach ($bytes as $byte) { + $parts[] = match (true) { + $byte >= 0 && $byte <= 0x7F => chr($byte), + isset($overrides[$byte]) => $overrides[$byte], + default => self::encodeLatin1ByteAsUtf8($byte), + }; + } + + return implode('', $parts); + } + + /** + * Encodes a single byte (0x80-0xFF) as a two-byte UTF-8 sequence. + * + * For Latin-1 / ISO-8859-1, the Unicode codepoint equals the byte value, + * so the UTF-8 encoding is: 110xxxxx 10xxxxxx. + * + * @throws \ValueError If the byte is outside the expected range. + */ + private static function encodeLatin1ByteAsUtf8(int $byte): string + { + if ($byte < 0x80 || $byte > 0xFF) { + throw new \ValueError( + sprintf('Byte must be in range 0x80-0xFF, got 0x%02X.', $byte), + ); + } + + return chr(0xC0 | ($byte >> 6)) . chr(0x80 | ($byte & 0x3F)); + } + + /** + * Strips the UTF-8 BOM (EF BB BF) if present at the start of the string. + */ + private static function stripUtf8Bom(string $string): string + { + if (str_starts_with($string, self::UTF8_BOM)) { + return substr($string, 3); + } + + return $string; + } +} diff --git a/src/Shared/Helpers/StringHelper.php b/src/Shared/Helpers/StringHelper.php index 6c4fc30..9f4f5fd 100644 --- a/src/Shared/Helpers/StringHelper.php +++ b/src/Shared/Helpers/StringHelper.php @@ -25,4 +25,172 @@ final readonly class StringHelper return (string) iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value); } + + public static function mbStrtok(string $delimiters, ?string $str = null): string | false + { + /** @var int $pos */ + static $pos = 0; + /** @var string $string */ + static $string = ''; + + if ($str !== null) { + $pos = 0; + $string = $str; + } + + $token = ''; + $len = mb_strlen($string, 'UTF-8'); + + while ($pos < $len) { + $char = mb_substr($string, $pos, 1, 'UTF-8'); + $pos++; + + if (mb_strpos($delimiters, $char, 0, 'UTF-8') === false) { + $token .= $char; + } else { + if ($token !== '') { + return $token; + } + } + } + + if ($token !== '') { + return $token; + } + + return false; + } + + /** + * Transliterates and formats a string into a URL-friendly slug. + */ + public static function slugify(string $value, string $separator = '-'): string + { + $transliterated = self::transliterate($value); + $cleaned = preg_replace('/[^a-zA-Z0-9]+/u', $separator, $transliterated) ?? ''; + + return strtolower(trim($cleaned, $separator)); + } + + /** + * Convert a string to snake_case. + */ + public static function snakeCase(string $value): string + { + return mb_strtolower(implode('_', self::splitWords($value))); + } + + /** + * Convert a string to kebab-case. + */ + public static function kebabCase(string $value): string + { + return mb_strtolower(implode('-', self::splitWords($value))); + } + + /** + * Convert a string to Title Case. + */ + public static function titleCase(string $value): string + { + $trimmed = trim($value); + if ($trimmed === '') { + return ''; + } + + return mb_convert_case( + mb_strtolower($trimmed, 'UTF-8'), + \MB_CASE_TITLE, + 'UTF-8' + ); + } + + /** + * Convert a string to PascalCase (StudlyCase). + */ + public static function pascalCase(string $value): string + { + $words = array_map( + static fn (string $word): string => mb_ucfirst(mb_strtolower($word)), + self::splitWords($value) + ); + + return implode('', $words); + } + + /** + * Convert a string to camelCase. + */ + public static function camelCase(string $value): string + { + return mb_lcfirst(self::pascalCase($value)); + } + + /** + * Split a string into words, respecting Unicode word boundaries. + */ + public static function words(string $value, int $words = 100, string $end = ' ...'): string + { + preg_match('/^\s*+(?:\S+\s*+){1,' . $words . '}/u', $value, $matches); + + if (! isset($matches[0]) || mb_strlen($value) === mb_strlen($matches[0])) { + return $value; + } + + return rtrim($matches[0]) . $end; + } + + /** + * Sanitizes and normalizes a text string (strips HTML, removes non-printable control characters, ensures valid UTF-8). + */ + public static function sanitize(?string $value): ?string + { + if ($value === null || $value === '') { + return $value; + } + + // Decode HTML entities then strip tags + $value = html_entity_decode($value, ENT_QUOTES | ENT_HTML5, 'UTF-8'); + $value = strip_tags($value); + + // Normalize newlines, temporarily convert to
to preserve paragraph breaks + $value = str_replace(["\r\n", "\r"], "\n", $value); + $value = str_replace("\n", '
', $value); + + // Ensure valid UTF-8 (handling Windows-1252 / ISO-8859-1 legacy strings) + $value = StringEncoder::detectAndConvertToUtf8($value); + + // Remove astral symbols (4-byte UTF-8) that may break storage/processing + $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $value) ?? ''; + + // Remove control/non-printable characters but keep TAB(0x09), LF(0x0A) and CR(0x0D) + $value = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F\xA0\xAD]/u', '', $value) ?? ''; + + // Revert
back to newlines and final strip_tags as safety + $value = str_replace('
', "\n", $value); + + return strip_tags($value); + } + + /** + * Split an arbitrary string into individual word tokens. + * + * @return list + */ + private static function splitWords(string $value): array + { + // 1. Separate camelCase transitions (e.g., "fooBar" -> "foo Bar") + $value = preg_replace('/(?<=[a-z0-9])([A-Z])/u', ' $1', $value) ?? $value; + + // 2. Separate acronyms followed by regular words (e.g., "HTTPResponse" -> "HTTP Response") + $value = preg_replace('/(?<=[A-Z])([A-Z][a-z])/u', ' $1', $value) ?? $value; + + // 3. Replace non-alphanumeric characters with whitespace + $value = preg_replace('/[^\p{L}\p{N}]+/u', ' ', $value) ?? $value; + + return array_values(array_filter( + explode(' ', trim($value)), + static fn (string $word): bool => $word !== '' + )); + } } diff --git a/tests/Feature/Api/V1/IncoSheets/IncoSheetsIndexTest.php b/tests/Feature/Api/V1/IncoSheets/IncoSheetsIndexTest.php index f279740..adc0f95 100644 --- a/tests/Feature/Api/V1/IncoSheets/IncoSheetsIndexTest.php +++ b/tests/Feature/Api/V1/IncoSheets/IncoSheetsIndexTest.php @@ -101,7 +101,7 @@ describe('IncoSheets Index Feature API', function (): void { $response->assertStatus(200); $response->assertJsonPath('incoSheets.0.id', '9999001') ->assertJsonPath('incoSheets.0.brand.code', $brand->code->value) - ->assertJsonPath('incoSheets.0.brand.label', 'LE GAULOIS PROFESSIONNEL'); + ->assertJsonPath('incoSheets.0.brand.label', 'Le Gaulois Professionnel'); }); it('returns 422 Unprocessable Entity when brand code format is invalid', function (): void { diff --git a/tests/Integration/Infra/Adapters/Repositories/EloquentBrandRepositoryTest.php b/tests/Integration/Infra/Adapters/Repositories/EloquentBrandRepositoryTest.php index 996d059..1d47943 100644 --- a/tests/Integration/Infra/Adapters/Repositories/EloquentBrandRepositoryTest.php +++ b/tests/Integration/Infra/Adapters/Repositories/EloquentBrandRepositoryTest.php @@ -63,7 +63,7 @@ describe('EloquentBrandRepository Integration Tests', function (): void { BrandModel::query()->create([ 'id' => $rawId, - 'name' => 'Maître CoQ', + 'name' => 'Maître Coq', 'arcadia_id' => 10, 'uwil_public_id' => 'MCQ01', ]); @@ -87,7 +87,7 @@ describe('EloquentBrandRepository Integration Tests', function (): void { ->and($secondResult)->not->toBeNull() ->and($secondResult->id()->value)->toBe($brandId->value) ->and($secondResult->code->value)->toBe('MCQ01') - ->and($secondResult->name->value)->toBe('Maître CoQ'); + ->and($secondResult->name->value)->toBe('Maître Coq'); }); it('finds a brand by BrandCode and serves subsequent calls from cache', function (): void { diff --git a/tests/Unit/Application/Arcadia/Usecase/Brands/FindAll/FindAllBrandsTest.php b/tests/Unit/Application/Arcadia/Usecase/Brands/FindAll/FindAllBrandsTest.php index 17db8cb..9f4c97e 100644 --- a/tests/Unit/Application/Arcadia/Usecase/Brands/FindAll/FindAllBrandsTest.php +++ b/tests/Unit/Application/Arcadia/Usecase/Brands/FindAll/FindAllBrandsTest.php @@ -82,7 +82,7 @@ describe('FindAllBrands Usecase Unit Tests', function (): void { // Second item was generated and persisted expect($result->brands[1]->arcadiaId->value)->toBe('2') - ->and($result->brands[1]->name->value)->toBe('Maître CoQ') + ->and($result->brands[1]->name->value)->toBe('Maître Coq') ->and(str_starts_with($result->brands[1]->code->value, 'MCQ'))->toBeTrue(); // Check that the second brand was persisted in repository @@ -92,7 +92,7 @@ describe('FindAllBrands Usecase Unit Tests', function (): void { it('slices brands according to pagination', function () use ($arcadiaDirectory, $brandRepository, $codeGenerator, $dispatcher): void { // GIVEN $arcadia1 = new ArcadiaBrand(new ArcadiaId('1'), new BrandName('Le Gaulois')); - $arcadia2 = new ArcadiaBrand(new ArcadiaId('2'), new BrandName('Maître CoQ')); + $arcadia2 = new ArcadiaBrand(new ArcadiaId('2'), new BrandName('Maître Coq')); $arcadia3 = new ArcadiaBrand(new ArcadiaId('3'), new BrandName('Loué')); $arcadiaDirectory->add($arcadia1, $arcadia2, $arcadia3); diff --git a/tests/Unit/Domain/Models/ValueObjects/Brand/BrandNameTest.php b/tests/Unit/Domain/Models/ValueObjects/Brand/BrandNameTest.php index 75abaad..af80030 100644 --- a/tests/Unit/Domain/Models/ValueObjects/Brand/BrandNameTest.php +++ b/tests/Unit/Domain/Models/ValueObjects/Brand/BrandNameTest.php @@ -9,15 +9,16 @@ use Domain\Models\ValueObjects\Brand\BrandName; covers(BrandName::class); describe('BrandName Value Object Instantiation', function (): void { - it('accepts valid brand names', function (): void { + it('accepts valid brand names and normalizes to Title Case', function (): void { // GIVEN - $value = 'Le Gaulois'; + $value = 'LE GAULOIS'; // WHEN $brandName = new BrandName($value); // THEN - expect($brandName->value)->toBe($value); + expect($brandName->value)->toBe('Le Gaulois') + ->and(new BrandName('maître coq')->value)->toBe('Maître Coq'); }); }); diff --git a/tests/Unit/Domain/Services/BrandResolverTest.php b/tests/Unit/Domain/Services/BrandResolverTest.php index dc04ffc..730e2b4 100644 --- a/tests/Unit/Domain/Services/BrandResolverTest.php +++ b/tests/Unit/Domain/Services/BrandResolverTest.php @@ -86,7 +86,7 @@ describe('BrandResolver Unit Tests', function (): void { // THEN expect($resolved->arcadiaId->value)->toBe('2') - ->and($resolved->name->value)->toBe('Maître CoQ') + ->and($resolved->name->value)->toBe('Maître Coq') ->and(str_starts_with($resolved->code->value, 'MCQ'))->toBeTrue(); expect($brandRepository->findByArcadiaId(new ArcadiaId('2')))->not->toBeNull(); @@ -97,7 +97,7 @@ describe('BrandResolver Unit Tests', function (): void { /** @var BrandCreated $event */ $event = $dispatcher->dispatched[0]; expect($event->arcadiaId->value)->toBe('2') - ->and($event->brandName->value)->toBe('Maître CoQ') + ->and($event->brandName->value)->toBe('Maître Coq') ->and($event->brandCode->value)->toBe($resolved->code->value) ->and($event->brandId->value)->toBe($resolved->id->value); }); diff --git a/tests/Unit/Infra/Adapters/Mappers/ArcadiaBrandMapperTest.php b/tests/Unit/Infra/Adapters/Mappers/ArcadiaBrandMapperTest.php index ed82965..6830213 100644 --- a/tests/Unit/Infra/Adapters/Mappers/ArcadiaBrandMapperTest.php +++ b/tests/Unit/Infra/Adapters/Mappers/ArcadiaBrandMapperTest.php @@ -23,7 +23,7 @@ describe('ArcadiaBrandMapper', function (): void { expect($domain)->toBeInstanceOf(ArcadiaBrand::class) ->and((string) $domain->id->value)->toBe('1') - ->and($domain->name->value)->toBe('LE GAULOIS'); + ->and($domain->name->value)->toBe('Le Gaulois'); }); it('maps an object row to ArcadiaBrand domain VO', function (): void { @@ -36,7 +36,7 @@ describe('ArcadiaBrandMapper', function (): void { expect($domain)->toBeInstanceOf(ArcadiaBrand::class) ->and((string) $domain->id->value)->toBe('2') - ->and($domain->name->value)->toBe('MAITRE COQ'); + ->and($domain->name->value)->toBe('Maitre Coq'); }); it('generates fallback name when libelle is empty', function (): void { @@ -59,6 +59,6 @@ describe('ArcadiaBrandMapper', function (): void { $domain = ArcadiaBrandMapper::toDomain($row); expect((string) $domain->id->value)->toBe('3') - ->and($domain->name->value)->toBe('LOUE'); + ->and($domain->name->value)->toBe('Loue'); }); }); diff --git a/tests/Unit/Infra/Adapters/Parsers/IncoStringParserTest.php b/tests/Unit/Infra/Adapters/Parsers/IncoStringParserTest.php index 46c52ec..327a1df 100644 --- a/tests/Unit/Infra/Adapters/Parsers/IncoStringParserTest.php +++ b/tests/Unit/Infra/Adapters/Parsers/IncoStringParserTest.php @@ -12,26 +12,6 @@ covers(IncoStringParser::class); uses(TestCase::class); describe('IncoStringParser', function (): void { - describe('protectiveDataFilter', function (): void { - it('returns null when input is null', function (): void { - expect(IncoStringParser::protectiveDataFilter(null))->toBeNull(); - }); - - it('returns empty string when input is empty', function (): void { - expect(IncoStringParser::protectiveDataFilter(''))->toBe(''); - }); - - it('strips HTML tags and decodes HTML entities', function (): void { - $input = '

Poulet & Dinde

'; - expect(IncoStringParser::protectiveDataFilter($input))->toBe('Poulet & Dinde'); - }); - - it('preserves newlines and removes non-printable control characters', function (): void { - $input = "Ligne 1\x00\x08\r\nLigne 2\x1F"; - expect(IncoStringParser::protectiveDataFilter($input))->toBe("Ligne 1\nLigne 2"); - }); - }); - describe('toPhrase', function (): void { it('formats string as phrase with capitalized first letter and no trailing period', function (): void { expect(IncoStringParser::toPhrase('VOLAILLE DE QUALITE.'))->toBe('Volaille de qualite') @@ -63,35 +43,4 @@ describe('IncoStringParser', function (): void { ->and($result)->toContain('Cuisson au four.'); }); }); - - describe('mbStrtok', function (): void { - it('tokenizes multibyte strings with delimiters', function (): void { - $str = 'Gluten;Lait,Œufs'; - $delim = ';,'; - $first = IncoStringParser::mbStrtok($delim, $str); - $second = IncoStringParser::mbStrtok($delim); - $third = IncoStringParser::mbStrtok($delim); - $fourth = IncoStringParser::mbStrtok($delim); - - expect($first)->toBe('Gluten') - ->and($second)->toBe('Lait') - ->and($third)->toBe('Œufs') - ->and($fourth)->toBeFalse(); - }); - }); - - describe('slugify', function (): void { - it('transliterates utf8 to ascii lowercase', function (): void { - expect(IncoStringParser::slugify('Émincés & Rôti'))->toBe('eminces & roti'); - }); - }); - - describe('words', function (): void { - it('truncates text to specified word count', function (): void { - $text = 'Un deux trois quatre cinq six sept'; - - expect(IncoStringParser::words($text, 3, '...'))->toBe('Un deux trois...') - ->and(IncoStringParser::words($text, 10))->toBe($text); - }); - }); }); diff --git a/tests/Unit/Infra/Cache/BrandCacheTest.php b/tests/Unit/Infra/Cache/BrandCacheTest.php index 080be7a..329aac3 100644 --- a/tests/Unit/Infra/Cache/BrandCacheTest.php +++ b/tests/Unit/Infra/Cache/BrandCacheTest.php @@ -95,7 +95,7 @@ describe('BrandCache Unit Tests', function (): void { $cacheData = [ 'id' => $uuid, - 'name' => 'Maître CoQ', + 'name' => 'Maître Coq', 'code' => 'MCQ01', 'arcadiaId' => '10', ]; @@ -119,7 +119,7 @@ describe('BrandCache Unit Tests', function (): void { assert($result !== null); expect($result)->not->toBeNull() ->and($result->arcadiaId->value)->toBe('10') - ->and($result->name->value)->toBe('Maître CoQ'); + ->and($result->name->value)->toBe('Maître Coq'); }); it('returns null and recovers gracefully when cache data is corrupted', function (): void { diff --git a/tests/Unit/Presentation/Api/V1/Dto/Brands/BrandDtoTest.php b/tests/Unit/Presentation/Api/V1/Dto/Brands/BrandDtoTest.php index 3ff3104..276f0d3 100644 --- a/tests/Unit/Presentation/Api/V1/Dto/Brands/BrandDtoTest.php +++ b/tests/Unit/Presentation/Api/V1/Dto/Brands/BrandDtoTest.php @@ -54,7 +54,7 @@ describe('BrandDto Unit Tests', function (): void { it('creates BrandDto from BrandItem', function (): void { // GIVEN $id = new BrandId(Uuid::uuid7()); - $name = new BrandName('Maître CoQ'); + $name = new BrandName('Maître Coq'); $code = new BrandCode('MCQ02'); $arcadiaId = new ArcadiaId('2'); $createdAt = new \DateTimeImmutable('2024-01-15T08:00:00Z'); @@ -72,7 +72,7 @@ describe('BrandDto Unit Tests', function (): void { // THEN expect($dto->id)->toBe((string) $id) - ->and($dto->name)->toBe('Maître CoQ') + ->and($dto->name)->toBe('Maître Coq') ->and($dto->code)->toBe('MCQ02') ->and($dto->arcadiaId)->toBe('2'); }); diff --git a/tests/Unit/Shared/Helpers/StringEncoderTest.php b/tests/Unit/Shared/Helpers/StringEncoderTest.php new file mode 100644 index 0000000..556bd4b --- /dev/null +++ b/tests/Unit/Shared/Helpers/StringEncoderTest.php @@ -0,0 +1,114 @@ + $reflection->newInstance()) + ->toThrow(\ReflectionException::class); + }); + + describe('toUtf8', function (): void { + it('returns empty string for empty input', function (): void { + expect(StringEncoder::toUtf8('', 'UTF-8'))->toBe('') + ->and(StringEncoder::toUtf8('', 'Windows-1252'))->toBe(''); + }); + + it('passes valid UTF-8 through unchanged', function (): void { + $utf8 = 'Émincés & Rôti de poulet fermier'; + expect(StringEncoder::toUtf8($utf8, 'UTF-8'))->toBe($utf8); + }); + + it('converts Windows-1252 special characters to UTF-8', function (): void { + // 0x80 (€), 0x8C (Œ), 0x9C (œ), 0x92 (’) + $win1252 = "\x80 50 \x8Cuf \x9Cuf l\x92arbre"; + $expected = '€ 50 Œuf œuf l’arbre'; + + expect(StringEncoder::toUtf8($win1252, 'Windows-1252'))->toBe($expected); + }); + + it('converts ISO-8859-15 specific characters to UTF-8', function (): void { + // 0xA4 (€ in Latin-9), 0xBC (Œ), 0xBD (œ) + $latin9 = "\xA4 50 \xBCuf \xBDuf"; + $expected = '€ 50 Œuf œuf'; + + expect(StringEncoder::toUtf8($latin9, 'ISO-8859-15'))->toBe($expected); + }); + + it('supports encoding aliases case-insensitively', function (string $alias): void { + $input = "\xE9"; // é in Latin-1/Win-1252 + expect(StringEncoder::toUtf8($input, $alias))->toBe('é'); + })->with(['windows-1252', 'WIN1252', 'cp1252', 'latin1', 'latin-1', 'iso-8859-1']); + + it('strips UTF-8 BOM when stripBom is true', function (): void { + $withBom = "\xEF\xBB\xBFHello World"; + expect(StringEncoder::toUtf8($withBom, 'UTF-8', stripBom: true))->toBe('Hello World') + ->and(StringEncoder::toUtf8($withBom, 'UTF-8', stripBom: false))->toBe($withBom); + }); + + it('throws ValueError for unsupported encoding', function (): void { + expect(fn () => StringEncoder::toUtf8('test', 'Shift-JIS')) + ->toThrow(\ValueError::class); + }); + + it('throws ValueError for invalid UTF-8 in toUtf8 with UTF-8 encoding', function (): void { + $invalidUtf8 = "\xC3\x28"; // Invalid UTF-8 sequence + expect(fn () => StringEncoder::toUtf8($invalidUtf8, 'UTF-8')) + ->toThrow(\ValueError::class); + }); + }); + + describe('detectAndConvertToUtf8', function (): void { + it('returns empty string for empty input', function (): void { + expect(StringEncoder::detectAndConvertToUtf8(''))->toBe(''); + }); + + it('preserves valid UTF-8 without re-encoding', function (): void { + $utf8 = 'Volaille rôti bio'; + expect(StringEncoder::detectAndConvertToUtf8($utf8))->toBe($utf8); + }); + + it('converts non-UTF8 strings using fallback encoding', function (): void { + // Non-UTF-8: "é" in Windows-1252 is 0xE9 + $win1252 = "R\xE9gal du Volailler"; + expect(StringEncoder::detectAndConvertToUtf8($win1252, 'Windows-1252'))->toBe('Régal du Volailler'); + }); + + it('throws ValueError when fallbackEncoding is UTF-8', function (): void { + expect(fn () => StringEncoder::detectAndConvertToUtf8('test', 'UTF-8')) + ->toThrow(\ValueError::class); + }); + }); + + describe('arrayToUtf8', function (): void { + it('converts an array of strings to UTF-8', function (): void { + $inputs = ["\xE9", "\xE0", "\xE8"]; + $expected = ['é', 'à', 'è']; + + expect(StringEncoder::arrayToUtf8($inputs, 'Windows-1252'))->toBe($expected); + }); + }); + + describe('supportedEncodings', function (): void { + it('returns list of all supported canonical and alias encodings', function (): void { + $encodings = StringEncoder::supportedEncodings(); + + expect($encodings)->toContain('UTF-8') + ->and($encodings)->toContain('Windows-1252') + ->and($encodings)->toContain('ISO-8859-15') + ->and($encodings)->toContain('latin1') + ->and($encodings)->toContain('cp1252'); + }); + }); +}); diff --git a/tests/Unit/Shared/Helpers/StringHelperTest.php b/tests/Unit/Shared/Helpers/StringHelperTest.php index 55aa64a..1f18f51 100644 --- a/tests/Unit/Shared/Helpers/StringHelperTest.php +++ b/tests/Unit/Shared/Helpers/StringHelperTest.php @@ -38,4 +38,73 @@ describe('StringHelper Unit Tests', function (): void { expect(StringHelper::transliterate($empty))->toBe('') ->and(StringHelper::transliterate($ascii))->toBe('hello world 123'); }); + + it('slugifies strings with custom separator', function (): void { + expect(StringHelper::slugify('Émincés & Rôti'))->toBe('eminces-roti') + ->and(StringHelper::slugify('Émincés & Rôti', '_'))->toBe('eminces_roti') + ->and(StringHelper::slugify('Émincés & Rôti', ' '))->toBe('eminces roti') + ->and(StringHelper::slugify(''))->toBe(''); + }); + + it('converts strings to snake_case', function (): void { + expect(StringHelper::snakeCase('fooBarBaz'))->toBe('foo_bar_baz') + ->and(StringHelper::snakeCase('HTTPResponseCode'))->toBe('http_response_code') + ->and(StringHelper::snakeCase('Le Gaulois Volaille'))->toBe('le_gaulois_volaille'); + }); + + it('converts strings to kebab-case', function (): void { + expect(StringHelper::kebabCase('fooBarBaz'))->toBe('foo-bar-baz') + ->and(StringHelper::kebabCase('HTTPResponseCode'))->toBe('http-response-code'); + }); + + it('converts strings to titleCase, pascalCase, and camelCase', function (): void { + expect(StringHelper::titleCase('LE GAULOIS'))->toBe('Le Gaulois') + ->and(StringHelper::titleCase('maître coq'))->toBe('Maître Coq') + ->and(StringHelper::titleCase(''))->toBe('') + ->and(StringHelper::pascalCase('foo_bar_baz'))->toBe('FooBarBaz') + ->and(StringHelper::camelCase('foo_bar_baz'))->toBe('fooBarBaz') + ->and(StringHelper::camelCase('HTTP_RESPONSE'))->toBe('httpResponse'); + }); + + it('truncates strings to word limits with custom ending', function (): void { + $text = 'Un deux trois quatre cinq six'; + + expect(StringHelper::words($text, 3, '...'))->toBe('Un deux trois...') + ->and(StringHelper::words($text, 10))->toBe($text) + ->and(StringHelper::words(''))->toBe(''); + }); + + it('tokenizes multibyte strings with delimiters using mbStrtok', function (): void { + $str = 'Gluten;Lait,Œufs'; + $delim = ';,'; + $first = StringHelper::mbStrtok($delim, $str); + $second = StringHelper::mbStrtok($delim); + $third = StringHelper::mbStrtok($delim); + $fourth = StringHelper::mbStrtok($delim); + + expect($first)->toBe('Gluten') + ->and($second)->toBe('Lait') + ->and($third)->toBe('Œufs') + ->and($fourth)->toBeFalse(); + }); + + describe('sanitize', function (): void { + it('returns null when input is null', function (): void { + expect(StringHelper::sanitize(null))->toBeNull(); + }); + + it('returns empty string when input is empty', function (): void { + expect(StringHelper::sanitize(''))->toBe(''); + }); + + it('strips HTML tags and decodes HTML entities', function (): void { + $input = '

Poulet & Dinde

'; + expect(StringHelper::sanitize($input))->toBe('Poulet & Dinde'); + }); + + it('preserves newlines and removes non-printable control characters', function (): void { + $input = "Ligne 1\x00\x08\r\nLigne 2\x1F"; + expect(StringHelper::sanitize($input))->toBe("Ligne 1\nLigne 2"); + }); + }); });