80 lines
2.6 KiB
PHP
80 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca\Catalog;
|
|
|
|
class LocalizedText
|
|
{
|
|
private const BASE_LANGUAGE = "ru";
|
|
|
|
public static function property(array $properties, string $code): string
|
|
{
|
|
if (LANGUAGE_ID !== self::BASE_LANGUAGE) {
|
|
$translated = self::value($properties[$code . "_" . strtoupper(LANGUAGE_ID)] ?? []);
|
|
if ($translated !== "") {
|
|
return $translated;
|
|
}
|
|
}
|
|
|
|
return self::value($properties[$code] ?? []);
|
|
}
|
|
|
|
public static function detailText(array $element): string
|
|
{
|
|
return self::field($element, "DETAIL_TEXT");
|
|
}
|
|
|
|
public static function field(array $element, string $field): string
|
|
{
|
|
if (LANGUAGE_ID !== self::BASE_LANGUAGE) {
|
|
$translated = self::value($element["PROPERTIES"][$field . "_" . strtoupper(LANGUAGE_ID)] ?? []);
|
|
if ($translated !== "") {
|
|
return $translated;
|
|
}
|
|
}
|
|
|
|
return (string)($element["~" . $field] ?? $element[$field] ?? "");
|
|
}
|
|
|
|
public static function localizeItem(array $item, array $fields, array $properties = []): array
|
|
{
|
|
if (LANGUAGE_ID === self::BASE_LANGUAGE) {
|
|
return $item;
|
|
}
|
|
$suffix = "_" . strtoupper(LANGUAGE_ID);
|
|
foreach ($fields as $field) {
|
|
$value = self::value($item["PROPERTIES"][$field . $suffix] ?? []);
|
|
if ($value !== "") {
|
|
$item["~" . $field] = $value;
|
|
$item[$field] = htmlspecialcharsbx($value);
|
|
}
|
|
}
|
|
foreach ($properties as $code) {
|
|
$value = self::value($item["PROPERTIES"][$code . $suffix] ?? []);
|
|
if ($value === "" || !isset($item["PROPERTIES"][$code])) {
|
|
continue;
|
|
}
|
|
$item["PROPERTIES"][$code]["~VALUE"] = $value;
|
|
$item["PROPERTIES"][$code]["VALUE"] = htmlspecialcharsbx($value);
|
|
if (isset($item["DISPLAY_PROPERTIES"][$code])) {
|
|
$item["DISPLAY_PROPERTIES"][$code]["~VALUE"] = $value;
|
|
$item["DISPLAY_PROPERTIES"][$code]["VALUE"] = htmlspecialcharsbx($value);
|
|
$item["DISPLAY_PROPERTIES"][$code]["DISPLAY_VALUE"] = htmlspecialcharsbx($value);
|
|
}
|
|
}
|
|
|
|
return $item;
|
|
}
|
|
|
|
private static function value(array $property): string
|
|
{
|
|
$value = $property["~VALUE"] ?? $property["VALUE"] ?? "";
|
|
if (is_array($value)) {
|
|
$value = $value["TEXT"] ?? implode(", ", array_filter($value, "is_scalar"));
|
|
}
|
|
|
|
return trim((string)$value);
|
|
}
|
|
}
|