<?php
declare(strict_types=1); namespace App\Domain\Tags; class TagNormalizer { public function normalize(array $tags): array { $normalized = []; foreach ($tags as $tag) { $tag = $this->convertEncoding($tag); $tag = $this->toLower($tag); $tag = $this->replaceSpacesWithHyphen($tag); $tag = $this->removeInvalidChars($tag); $tag = $this->trimTag($tag); if ($tag !== '') { $normalized[] = $tag; } } return array_values(array_unique($normalized)); } private function convertEncoding(string $tag): string { return mb_convert_kana($tag, 'KVas'); } private function toLower(string $tag): string { return mb_strtolower($tag); } private function removeInvalidChars(string $tag): string { return preg_replace('/[^\p{L}\p{N}\-_ ]/u', '', $tag); } private function replaceSpacesWithHyphen(string $tag): string { return preg_replace('/\s+/', '-', $tag); } private function trimTag(string $tag): string { return trim($tag, ' -'); } } 