NormalizerFactory::getNormalizer()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 8.8333
cc 7
nc 7
nop 1
crap 7
1
<?php
2
3
namespace Riesenia\Pohoda\Common\OptionsResolver\Normalizers;
4
5
use Closure;
6
use DomainException;
7
8
class NormalizerFactory
9
{
10
    /** @var array<string, AbstractNormalizer> */
11
    protected array $loadedNormalizers = [];
12
13 70
    public function getClosure(string $name): Closure
14
    {
15 70
        return $this->getNormalizer($name)->normalize(...);
16
    }
17
18
    /**
19
     * Get normalizer.
20
     *
21
     * @param string $type
22
     *
23
     * @throws DomainException
24
     * @return AbstractNormalizer
25
     */
26 80
    public function getNormalizer(string $type): AbstractNormalizer
27
    {
28 80
        if (isset($this->loadedNormalizers[$type])) {
29 45
            return $this->loadedNormalizers[$type];
30
        }
31
32 78
        if (str_starts_with($type, '?string')) {
33
            // strings can be nullable and have length
34 1
            $normalizer = $this->createNormalizer('string', \intval(\substr($type, 7)), true);
35 78
        } elseif (str_starts_with($type, '?str')) {
36
            // short strings can be nullable and have length
37 1
            $normalizer = $this->createNormalizer('string', \intval(\substr($type, 4)), true);
38 78
        } elseif (str_starts_with($type, 'string')) {
39
            // strings have length
40 36
            $normalizer = $this->createNormalizer('string', \intval(\substr($type, 6)));
41 76
        } elseif (str_starts_with($type, 'str')) {
42
            // short strings have length
43 3
            $normalizer = $this->createNormalizer('string', \intval(\substr($type, 3)));
44 73
        } elseif (str_starts_with($type, '?')) {
45
            // types can be nullable
46 7
            $normalizer = $this->createNormalizer(\substr($type, 1), null, true);
47
        } else {
48 70
            $normalizer = $this->createNormalizer($type);
49
        }
50
51 64
        $this->loadedNormalizers[$type] = $normalizer;
52 64
        return $this->loadedNormalizers[$type];
53
    }
54
55
    /**
56
     * Create normalizer.
57
     *
58
     * @param string   $type
59
     * @param int|null $length
60
     * @param bool     $nullable
61
     * @throws DomainException
62
     * @return AbstractNormalizer
63
     * @see vendor/symfony/options-resolver/OptionsResolver.php:1128
64
     */
65 78
    protected function createNormalizer(string $type, ?int $length = null, bool $nullable = false): AbstractNormalizer
66
    {
67 78
        return match ($type) {
68 36
            'str', 'string' => new Strings($length, $nullable),
69 23
            'float', 'number' => new Numbers($length, $nullable),
70 20
            'int', 'integer' => new Integers($length, $nullable),
71 31
            'bool', 'boolean' => new Booleans($length, $nullable),
72 18
            'date' => new Dates($length, $nullable),
73 3
            'datetime' => new DateTimes($length, $nullable),
74 3
            'time' => new Times($length, $nullable),
75 4
            'list_request_type' => new ListRequestType($length, $nullable),
76 78
            default => throw new DomainException('Not a valid normalizer type: ' . $type),
77 78
        };
78
    }
79
}
80