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

TimeZoneStrategy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 55
ccs 21
cts 24
cp 0.875
rs 10
c 1
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A applyTz() 0 15 3
A getTzValue() 0 8 2
A generateArguments() 0 10 1
A getAttributeClassName() 0 3 1
A getValidatorProperty() 0 3 1
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