1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the FreshDoctrineEnumBundle |
4
|
|
|
* |
5
|
|
|
* (c) Artem Genvald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Fresh\DoctrineEnumBundle\Twig\Extension; |
14
|
|
|
|
15
|
|
|
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsNotRegisteredException; |
16
|
|
|
use Fresh\DoctrineEnumBundle\Exception\EnumType\NoRegisteredEnumTypesException; |
17
|
|
|
use Fresh\DoctrineEnumBundle\Exception\EnumValue\ValueIsFoundInFewRegisteredEnumTypesException; |
18
|
|
|
use Fresh\DoctrineEnumBundle\Exception\EnumValue\ValueIsNotFoundInAnyRegisteredEnumTypeException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ReadableEnumValueExtension returns the readable variant of ENUM value. |
22
|
|
|
* |
23
|
|
|
* @author Artem Genvald <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ReadableEnumValueExtension extends AbstractEnumExtension |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function getFilters(): array |
31
|
|
|
{ |
32
|
|
|
return [new \Twig_Filter('readable_enum', [$this, 'getReadableEnumValue'])]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string|null $enumValue |
37
|
|
|
* @param string|null $enumType |
38
|
|
|
* |
39
|
|
|
* @return string|null |
40
|
|
|
* |
41
|
|
|
* @throws EnumTypeIsNotRegisteredException |
42
|
|
|
* @throws NoRegisteredEnumTypesException |
43
|
|
|
* @throws ValueIsFoundInFewRegisteredEnumTypesException |
44
|
|
|
* @throws ValueIsNotFoundInAnyRegisteredEnumTypeException |
45
|
|
|
* @throws \InvalidArgumentException |
46
|
|
|
*/ |
47
|
|
|
public function getReadableEnumValue(?string $enumValue, ?string $enumType = null): ?string |
48
|
|
|
{ |
49
|
|
|
if ($this->hasRegisteredEnumTypes()) { |
50
|
|
|
if (null === $enumValue) { |
51
|
|
|
return $enumValue; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// If ENUM type was set, e.g. {{ player.position|readable_enum('BasketballPositionType') }} |
55
|
|
|
if (null !== $enumType) { |
56
|
|
|
$this->throwExceptionIfEnumTypeIsNotRegistered($enumType); |
57
|
|
|
|
58
|
|
|
return $this->registeredEnumTypes[$enumType]::getReadableValue($enumValue); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// If ENUM type wasn't set, e.g. {{ player.position|readable_enum }} |
62
|
|
|
$this->findOccurrences($enumValue); |
63
|
|
|
|
64
|
|
|
if ($this->onlyOneOccurrenceFound()) { |
65
|
|
|
return \array_pop($this->occurrences)::getReadableValue($enumValue); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($this->moreThanOneOccurrenceFound()) { |
69
|
|
|
throw new ValueIsFoundInFewRegisteredEnumTypesException( |
70
|
|
|
\sprintf( |
71
|
|
|
'Value "%s" is found in few registered ENUM types. You should manually set the appropriate one', |
72
|
|
|
$enumValue |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
throw new ValueIsNotFoundInAnyRegisteredEnumTypeException( |
78
|
|
|
\sprintf( |
79
|
|
|
'Value "%s" was not found in any registered ENUM type.', |
80
|
|
|
$enumValue |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
throw $this->createNoRegisteredEnumTypesException(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $enumValue |
90
|
|
|
*/ |
91
|
|
|
private function findOccurrences(string $enumValue): void |
92
|
|
|
{ |
93
|
|
|
foreach ($this->registeredEnumTypes as $registeredEnumType) { |
94
|
|
|
if ($registeredEnumType::isValueExist($enumValue)) { |
95
|
|
|
$this->occurrences[] = $registeredEnumType; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|