Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
TimezoneTransformer
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 supports
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 normalizeTimezone
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Engine\Application\Transformer;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use DateTimeZone;
12
13/**
14 * Timezone UiType Transformer.
15 *
16 * Handles UiType: timezone.
17 * Normalizes, validates and formats IANA timezone identifier strings.
18 *
19 * @package App\Core\Engine\Application\Transformer
20 */
21final class TimezoneTransformer extends AbstractStringTransformer
22{
23    /** @var string[] Supported UiType names. */
24    private const array SUPPORTED = ['timezone'];
25
26    /** Default fallback timezone. */
27    public const string DEFAULT_TIMEZONE = 'Europe/Warsaw';
28
29    /** {@inheritdoc} */
30    public function supports(string $uitypeName): bool
31    {
32        return in_array($uitypeName, self::SUPPORTED, true);
33    }
34
35    /**
36     * Validates and normalizes IANA timezone string.
37     *
38     * @param mixed $value Raw timezone identifier.
39     * @return string Valid IANA timezone or default fallback.
40     */
41    public static function normalizeTimezone(mixed $value): string
42    {
43        if ($value === null || $value === '') {
44            return self::DEFAULT_TIMEZONE;
45        }
46
47        $tz = trim((string) $value);
48        if (in_array($tz, DateTimeZone::listIdentifiers(), true)) {
49            return $tz;
50        }
51
52        return self::DEFAULT_TIMEZONE;
53    }
54}