Passed
Push — master ( 8dac2e...23d755 )
by Stanislau
03:22
created

TimeZoneStrategy::applyTz()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 15
ccs 7
cts 9
cp 0.7778
crap 3.0987
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Library\DTO\Preparation\Processor\Property\Assert;
15
16
use Symfony\Component\Validator\Constraints\Timezone;
17
18
class TimeZoneStrategy extends AbstractConstraintStrategy
19
{
20 1
    protected function generateArguments(array $config): array
21
    {
22 1
        $parent = parent::generateArguments($config);
23
24 1
        $parent['countryCode'] = $config['country_code'] ?? null;
25 1
        $parent['intlCompatible'] = $this->stringToBool($config['intl_compatible'] ?? 'false');
26
27 1
        $this->applyTz($parent, $config);
28
29 1
        return array_filter($parent);
30
    }
31
32
    /**
33
     * @param array<string, mixed> $args
34
     * @param array<string, mixed> $config
35
     *
36
     * @return void
37
     */
38 1
    protected function applyTz(array &$args, array $config): void
39
    {
40 1
        $zone = 0;
41 1
        if (empty($config['zone'])) {
42
            $args['zone'] = \DateTimeZone::ALL;
43
44
            return;
45
        }
46
47 1
        $zones = $config['zone'];
48 1
        foreach ($zones as $z) {
49 1
            $zone |= $this->getTzValue($z['value']);
50
        }
51
52 1
        $args['zone'] = $zone;
53
    }
54
55 1
    protected function getTzValue(string $zoneName): int
56
    {
57 1
        $constName = sprintf('%s::%s', \DateTimeZone::class, $zoneName);
58 1
        if (!\defined($constName)) {
59
            throw new \InvalidArgumentException(sprintf('Time zone `%s` is not defined in class %s', $zoneName, \DateTimeZone::class));
60
        }
61
62 1
        return \constant($constName);
63
    }
64
65 1
    protected function getValidatorProperty(): string
66
    {
67 1
        return 'time_zone';
68
    }
69
70 1
    protected function getAttributeClassName(): string
71
    {
72 1
        return Timezone::class;
73
    }
74
}
75