|
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
|
71 |
|
public function getClosure(string $name): Closure |
|
14
|
|
|
{ |
|
15
|
71 |
|
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
|
81 |
|
public function getNormalizer(string $type): AbstractNormalizer |
|
27
|
|
|
{ |
|
28
|
81 |
|
if (isset($this->loadedNormalizers[$type])) { |
|
29
|
46 |
|
return $this->loadedNormalizers[$type]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
79 |
|
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
|
79 |
|
} 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
|
79 |
|
} elseif (str_starts_with($type, 'string')) { |
|
39
|
|
|
// strings have length |
|
40
|
38 |
|
$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
|
65 |
|
$this->loadedNormalizers[$type] = $normalizer; |
|
52
|
65 |
|
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
|
79 |
|
protected function createNormalizer(string $type, ?int $length = null, bool $nullable = false): AbstractNormalizer |
|
66
|
|
|
{ |
|
67
|
79 |
|
return match ($type) { |
|
68
|
38 |
|
'str', 'string' => new Strings($length, $nullable), |
|
69
|
24 |
|
'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
|
3 |
|
'list_request_type' => new ListRequestType($length, $nullable), |
|
76
|
79 |
|
default => throw new DomainException('Not a valid normalizer type: ' . $type), |
|
77
|
79 |
|
}; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|