1 | <?php |
||
27 | class ReadableEnumValueTwigExtension extends AbstractEnumTwigExtension |
||
28 | { |
||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | public function getFilters(): array |
||
36 | |||
37 | /** |
||
38 | * @param string|null $enumValue |
||
39 | * @param string|null $enumType |
||
40 | * |
||
41 | * @throws EnumTypeIsNotRegisteredException |
||
42 | * @throws NoRegisteredEnumTypesException |
||
43 | * @throws ValueIsFoundInFewRegisteredEnumTypesException |
||
44 | * @throws ValueIsNotFoundInAnyRegisteredEnumTypeException |
||
45 | * |
||
46 | * @return string|null |
||
47 | */ |
||
48 | public function getReadableEnumValue(?string $enumValue, ?string $enumType = null): ?string |
||
49 | { |
||
50 | if ($this->hasRegisteredEnumTypes()) { |
||
51 | if (null === $enumValue) { |
||
52 | return null; |
||
53 | } |
||
54 | |||
55 | // If ENUM type was set, e.g. {{ player.position|readable_enum('BasketballPositionType') }} |
||
56 | if (null !== $enumType) { |
||
57 | $this->throwExceptionIfEnumTypeIsNotRegistered($enumType); |
||
58 | |||
59 | return $this->registeredEnumTypes[$enumType]::getReadableValue($enumValue); |
||
60 | } |
||
61 | |||
62 | // If ENUM type wasn't set, e.g. {{ player.position|readable_enum }} |
||
63 | $this->findOccurrences($enumValue); |
||
64 | |||
65 | if ($this->onlyOneOccurrenceFound()) { |
||
66 | $occurrence = \array_pop($this->occurrences); |
||
67 | |||
68 | if (null !== $occurrence && \is_subclass_of($occurrence, AbstractEnumType::class)) { |
||
|
|||
69 | return $occurrence::getReadableValue($enumValue); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | if ($this->moreThanOneOccurrenceFound()) { |
||
74 | $exceptionMessage = \sprintf( |
||
75 | 'Value "%s" is found in few registered ENUM types. You should manually set the appropriate one', |
||
76 | $enumValue |
||
77 | ); |
||
78 | |||
79 | throw new ValueIsFoundInFewRegisteredEnumTypesException($exceptionMessage); |
||
80 | } |
||
81 | |||
82 | $exceptionMessage = \sprintf( |
||
83 | 'Value "%s" was not found in any registered ENUM type.', |
||
84 | $enumValue |
||
85 | ); |
||
86 | |||
87 | throw new ValueIsNotFoundInAnyRegisteredEnumTypeException($exceptionMessage); |
||
88 | } |
||
89 | |||
90 | throw $this->createNoRegisteredEnumTypesException(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param string $enumValue |
||
95 | */ |
||
96 | private function findOccurrences(string $enumValue): void |
||
104 | } |
||
105 |