p35
This commit is contained in:
@@ -44,6 +44,27 @@ final class LanguageCleanupConfig
|
||||
return in_array($term, $this->getProtectedTerms(), true);
|
||||
}
|
||||
|
||||
/** @return array<string, string> */
|
||||
public function getAsciiTransliterationMap(): array
|
||||
{
|
||||
$normalization = $this->requiredMap('normalization');
|
||||
if (!array_key_exists('ascii_transliteration', $normalization)) {
|
||||
throw $this->invalid('normalization.ascii_transliteration', 'is missing');
|
||||
}
|
||||
|
||||
return $this->stringMapFromValue($normalization['ascii_transliteration'], 'normalization.ascii_transliteration', true);
|
||||
}
|
||||
|
||||
public function transliterateToAscii(string $value): string
|
||||
{
|
||||
$map = $this->getAsciiTransliterationMap();
|
||||
if ($map === []) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return strtr($value, $map);
|
||||
}
|
||||
|
||||
/** @return string[] */
|
||||
public function getCleanupProfileNames(): array
|
||||
{
|
||||
@@ -235,6 +256,35 @@ final class LanguageCleanupConfig
|
||||
return $out;
|
||||
}
|
||||
|
||||
/** @return array<string, string> */
|
||||
private function stringMapFromValue(mixed $value, string $path, bool $required): array
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
throw $this->invalid($path, 'must be a map of non-empty strings');
|
||||
}
|
||||
|
||||
$out = [];
|
||||
foreach ($value as $key => $item) {
|
||||
if (!is_scalar($key) || !is_scalar($item)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$key = trim((string) $key);
|
||||
$item = trim((string) $item);
|
||||
if ($key === '' || $item === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$out[$key] = $item;
|
||||
}
|
||||
|
||||
if ($required && $out === []) {
|
||||
throw $this->invalid($path, 'must contain at least one non-empty map entry');
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/** @param string[] $terms */
|
||||
private function removeProtectedTerms(array $terms): array
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user