|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Copyright 2026 The Horde Project (http://www.horde.org/) |
| 7 | + * |
| 8 | + * See the enclosed file LICENSE for license information (LGPL). If you |
| 9 | + * did not receive this file, see http://www.horde.org/licenses/lgpl21. |
| 10 | + * |
| 11 | + * @category Horde |
| 12 | + * @copyright 2026 The Horde Project |
| 13 | + * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 |
| 14 | + * @package Core |
| 15 | + */ |
| 16 | + |
| 17 | +namespace Horde\Core\Prefs; |
| 18 | + |
| 19 | +use Horde\Date\Format; |
| 20 | +use Horde_Prefs; |
| 21 | +use Psr\Log\LoggerInterface; |
| 22 | +use Throwable; |
| 23 | + |
| 24 | +/** |
| 25 | + * Date format preferences decorator |
| 26 | + * |
| 27 | + * Wraps date format preference access with automatic strftime-to-ICU |
| 28 | + * conversion. On first access of a legacy strftime value: |
| 29 | + * 1. Detects strftime format |
| 30 | + * 2. Logs the conversion |
| 31 | + * 3. Converts to ICU pattern |
| 32 | + * 4. Writes back the converted value (failsafe, noop if impossible) |
| 33 | + * 5. Returns the ICU pattern |
| 34 | + * |
| 35 | + * @category Horde |
| 36 | + * @copyright 2026 The Horde Project |
| 37 | + * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 |
| 38 | + * @package Core |
| 39 | + */ |
| 40 | +class DateFormatPrefs |
| 41 | +{ |
| 42 | + private const DATE_FORMAT_KEYS = [ |
| 43 | + 'date_format', |
| 44 | + 'date_format_mini', |
| 45 | + 'time_format', |
| 46 | + 'time_format_mini', |
| 47 | + ]; |
| 48 | + |
| 49 | + public function __construct( |
| 50 | + private Horde_Prefs $prefs, |
| 51 | + private string $locale = 'en_US', |
| 52 | + private ?LoggerInterface $logger = null, |
| 53 | + ) {} |
| 54 | + |
| 55 | + /** |
| 56 | + * Get a date format preference as an ICU pattern. |
| 57 | + * |
| 58 | + * If the stored value is strftime, converts it to ICU, writes back |
| 59 | + * the converted value (failsafe), logs the conversion, and returns |
| 60 | + * the ICU pattern. If already ICU, returns as-is. |
| 61 | + */ |
| 62 | + public function getDateFormat(string $key): string |
| 63 | + { |
| 64 | + $value = $this->prefs->getValue($key); |
| 65 | + if ($value === null) { |
| 66 | + return ''; |
| 67 | + } |
| 68 | + |
| 69 | + if (!Format::isStrftimeFormat($value)) { |
| 70 | + return $value; |
| 71 | + } |
| 72 | + |
| 73 | + $icu = Format::strftimeToIcu($value, $this->locale); |
| 74 | + |
| 75 | + $this->logger?->notice( |
| 76 | + "DateFormatPrefs: converting legacy strftime pref '{$key}': '{$value}' → '{$icu}'" |
| 77 | + ); |
| 78 | + |
| 79 | + if (!$this->prefs->isLocked($key)) { |
| 80 | + try { |
| 81 | + $this->prefs->setValue($key, $icu); |
| 82 | + } catch (Throwable) { |
| 83 | + // Noop — read-only backend or other error |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return $icu; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get the short time format ICU pattern based on twentyFour pref. |
| 92 | + * |
| 93 | + * Replaces the common pattern: |
| 94 | + * $prefs->getValue('twentyFour') ? '%R' : '%I:%M%p' |
| 95 | + */ |
| 96 | + public function getTimeFormatShort(): string |
| 97 | + { |
| 98 | + return $this->prefs->getValue('twentyFour') ? 'HH:mm' : 'h:mm a'; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get the time format with seconds based on twentyFour pref. |
| 103 | + * |
| 104 | + * Replaces the common pattern: |
| 105 | + * $prefs->getValue('twentyFour') ? '%H:%M:%S' : '%I:%M:%S %p' |
| 106 | + */ |
| 107 | + public function getTimeFormatFull(): string |
| 108 | + { |
| 109 | + return $this->prefs->getValue('twentyFour') ? 'HH:mm:ss' : 'h:mm:ss a'; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Check if a given pref key is a date format key handled by this class. |
| 114 | + */ |
| 115 | + public function isDateFormatKey(string $key): bool |
| 116 | + { |
| 117 | + return in_array($key, self::DATE_FORMAT_KEYS, true); |
| 118 | + } |
| 119 | +} |
0 commit comments