You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.1 KiB
83 lines
2.1 KiB
|
1 month ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Support;
|
||
|
|
|
||
|
|
final class LocalizedSchema
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* @param array<string, mixed>|null $i18n
|
||
|
|
*/
|
||
|
|
public static function pick(string $locale, string $fallback, ?array $i18n): string
|
||
|
|
{
|
||
|
|
if ($locale === PortalLocale::EN) {
|
||
|
|
$en = trim((string) ($i18n['en'] ?? ''));
|
||
|
|
if ($en !== '') {
|
||
|
|
return $en;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $fallback;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array<string, mixed> $field
|
||
|
|
*/
|
||
|
|
public static function fieldText(array $field, string $locale, string $key): string
|
||
|
|
{
|
||
|
|
$fallback = trim((string) ($field[$key] ?? ''));
|
||
|
|
$i18n = $field[$key.'_i18n'] ?? null;
|
||
|
|
|
||
|
|
return self::pick($locale, $fallback, is_array($i18n) ? $i18n : null);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array<string, mixed> $option
|
||
|
|
*/
|
||
|
|
public static function optionLabel(array $option, string $locale): string
|
||
|
|
{
|
||
|
|
$fallback = trim((string) ($option['label'] ?? $option['value'] ?? ''));
|
||
|
|
$i18n = $option['label_i18n'] ?? null;
|
||
|
|
|
||
|
|
return self::pick($locale, $fallback, is_array($i18n) ? $i18n : null);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param list<mixed>|null $schemaJson
|
||
|
|
* @return array<string, mixed>|null
|
||
|
|
*/
|
||
|
|
public static function fieldByKey(?array $schemaJson, string $key): ?array
|
||
|
|
{
|
||
|
|
if (! is_array($schemaJson)) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
foreach ($schemaJson as $row) {
|
||
|
|
if (is_array($row) && trim((string) ($row['key'] ?? '')) === $key) {
|
||
|
|
return $row;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array<string, mixed> $field
|
||
|
|
*/
|
||
|
|
public static function optionLabelForValue(array $field, string $value, string $locale): string
|
||
|
|
{
|
||
|
|
$opts = $field['options'] ?? [];
|
||
|
|
if (! is_array($opts)) {
|
||
|
|
return $value;
|
||
|
|
}
|
||
|
|
foreach ($opts as $opt) {
|
||
|
|
if (! is_array($opt)) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if (trim((string) ($opt['value'] ?? '')) === $value) {
|
||
|
|
return self::optionLabel($opt, $locale);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $value;
|
||
|
|
}
|
||
|
|
}
|