Passed
Push — master ( 221c35...fdb910 )
by Petr
02:59
created

NormalizerFactory::fillAllowedValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 6
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace kalanis\Pohoda\Common\OptionsResolver\Normalizers;
4
5
use Closure;
6
use DomainException;
7
use kalanis\Pohoda\Common;
8
9
class NormalizerFactory
10
{
11 167
    public static function loadNormalizersFromDto(
12
        Common\OptionsResolver $resolver,
13
        Common\Dtos\AbstractDto $dto,
14
        bool $useOneDirectionalVariables,
15
    ): void {
16 167
        $propertyOptions = Common\Dtos\Processing::getOptions($dto, $useOneDirectionalVariables);
17 167
        foreach ($propertyOptions as $propertyName => $allOptions) {
18 165
            foreach ($allOptions as $option) {
19 165
                if (is_a($option, Common\Attributes\Options\AbstractOption::class)) {
20 165
                    switch ($option->getAction()) {
21 165
                        case Common\OptionsResolver\ActionsEnum::DEFAULT_VALUES:
22 94
                            static::fillDefaultValues($resolver, $option, $propertyName);
23 94
                            break;
24 165
                        case Common\OptionsResolver\ActionsEnum::IS_REQUIRED:
25 88
                            static::fillAsRequired($resolver, $propertyName);
26 88
                            break;
27 162
                        case Common\OptionsResolver\ActionsEnum::NORMALIZER:
28 158
                            static::fillNormalizers($resolver, $option, $propertyName, $dto);
29 158
                            break;
30 137
                        case Common\OptionsResolver\ActionsEnum::ALLOWED_VALUES:
31 137
                            static::fillAllowedValues($resolver, $option, $propertyName);
32 137
                            break;
33
                            // @codeCoverageIgnoreStart
34
                            // okay, I do not know what might happen when I will want to use check by types
35
                        case Common\OptionsResolver\ActionsEnum::ALLOWED_TYPES:
36
                            static::fillAllowedTypes($resolver, $option, $propertyName);
37
                            break;
38
                            // @codeCoverageIgnoreEnd
39
                    };
40
                }
41
            }
42
        }
43
    }
44
45 94
    protected static function fillDefaultValues(
46
        Common\OptionsResolver $resolver,
47
        Common\Attributes\Options\AbstractOption $option,
48
        string $propertyName,
49
    ): void {
50 94
        if (\is_callable($option->value)) {
51 35
            $resolver->setDefault($propertyName, Closure::fromCallable($option->value));
52
        } else {
53 61
            $resolver->setDefault($propertyName, $option->value);
54
        }
55
    }
56
57 88
    protected static function fillAsRequired(
58
        Common\OptionsResolver $resolver,
59
        string $propertyName,
60
    ): void {
61 88
        $resolver->setRequired($propertyName);
62
    }
63
64 158
    protected static function fillNormalizers(
65
        Common\OptionsResolver $resolver,
66
        Common\Attributes\Options\AbstractOption $option,
67
        string $propertyName,
68
        Common\Dtos\AbstractDto $dto,
69
    ): void {
70 158
        $reflect = new \ReflectionClass($option->getNormalizer());
71 158
        $instance = $reflect->newInstance();
72 158
        if (is_a($instance, AbstractNormalizer::class)) {
73 158
            $instance->setParams(
74 158
                \is_null($option->value) ? null : (\is_numeric($option->value) || \is_string($option->value) ? \intval($option->value) : null),
75 158
                $option->isNullable,
76 158
                $option->value,
77 158
                $dto,
78 158
            );
79 158
            $resolver->setNormalizer($propertyName, $instance->normalize(...));
80
        }
81
    }
82
83 137
    protected static function fillAllowedValues(
84
        Common\OptionsResolver $resolver,
85
        Common\Attributes\Options\AbstractOption $option,
86
        string $propertyName,
87
    ): void {
88 137
        $resolver->setAllowedValues($propertyName, $option->value);
89
    }
90
91
    // @codeCoverageIgnoreStart
92
    // okay, I do not know what might happen when I will want to use check by types
93
    protected static function fillAllowedTypes(
94
        Common\OptionsResolver $resolver,
95
        Common\Attributes\Options\AbstractOption $option,
96
        string $propertyName,
97
    ): void {
98
        $values = array_map(fn($v) => \strval($v), (array) $option->value);
99
        $resolver->setAllowedTypes($propertyName, $values);
100
    }
101
    // @codeCoverageIgnoreEnd
102
103
    /**
104
     * Create normalizer.
105
     *
106
     * @param string   $type
107
     * @throws DomainException
108
     * @return AbstractNormalizer
109
     * @see vendor/symfony/options-resolver/OptionsResolver.php:1128
110
     */
111 20
    public static function createNormalizer(string $type): AbstractNormalizer
112
    {
113 20
        return match ($type) {
114 2
            'str', 'string' => new Strings(),
115 13
            'float', 'number' => new Numbers(),
116 2
            'int', 'integer' => new Integers(),
117 14
            'bool', 'boolean' => new Booleans(),
118 3
            'date' => new Dates(),
119
            'datetime' => new DateTimes(),
120
            'time' => new Times(),
121
            'list_request_type' => new ListRequestType(),
122 20
            default => throw new DomainException('Not a valid normalizer type: ' . $type),
123 20
        };
124
    }
125
}
126