| 123456789101112131415161718192021222324252627282930313233 |
- <?php
- namespace App\Support;
- /** Text variants share one campaign identity and one set of financial rules. */
- final class PromotionTranslations
- {
- public const LANGUAGES = ['zh', 'en', 'vi'];
- public const FIELDS = ['title', 'content'];
- public static function all(array $activity): array
- {
- $stored = $activity['translations'] ?? null;
- if (is_string($stored)) $stored = json_decode($stored, true);
- if (!is_array($stored) || !$stored) {
- $lang = in_array($activity['lang'] ?? '', self::LANGUAGES, true) ? $activity['lang'] : 'zh';
- $stored = [$lang => array_intersect_key($activity, array_flip(self::FIELDS))];
- }
- $result = [];
- foreach (self::LANGUAGES as $lang) {
- foreach (self::FIELDS as $field) $result[$lang][$field] = (string) ($stored[$lang][$field] ?? '');
- }
- return $result;
- }
- public static function select(array $activity, string $lang): ?array
- {
- $text = self::all($activity)[$lang] ?? null;
- if (!$text || trim($text['title']) === '') return null;
- return ['lang' => $lang, 'title' => $text['title'],
- 'content' => $text['content'] === '' ? null : $text['content']];
- }
- }
|