1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the FreshDoctrineEnumBundle. |
4
|
|
|
* |
5
|
|
|
* (c) Artem Henvald <[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\Constant\ConstantIsFoundInFewRegisteredEnumTypesException; |
16
|
|
|
use Fresh\DoctrineEnumBundle\Exception\Constant\ConstantIsNotFoundInAnyRegisteredEnumTypeException; |
17
|
|
|
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsNotRegisteredException; |
18
|
|
|
use Fresh\DoctrineEnumBundle\Exception\EnumType\NoRegisteredEnumTypesException; |
19
|
|
|
use Twig\TwigFilter; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* EnumConstantExtension. |
23
|
|
|
* |
24
|
|
|
* @author Artem Henvald <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class EnumConstantTwigExtension extends AbstractEnumTwigExtension |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function getFilters(): array |
32
|
|
|
{ |
33
|
|
|
return [new TwigFilter('enum_constant', [$this, 'getEnumConstant'])]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $enumConstant |
38
|
|
|
* @param string|null $enumType |
39
|
|
|
* |
40
|
|
|
* @throws EnumTypeIsNotRegisteredException |
41
|
|
|
* @throws NoRegisteredEnumTypesException |
42
|
|
|
* @throws ConstantIsFoundInFewRegisteredEnumTypesException |
43
|
|
|
* @throws ConstantIsNotFoundInAnyRegisteredEnumTypeException |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getEnumConstant(string $enumConstant, ?string $enumType = null): string |
48
|
|
|
{ |
49
|
|
|
if ($this->hasRegisteredEnumTypes()) { |
50
|
|
|
// If ENUM type was set, e.g. {{ 'CENTER'|enum_constant('BasketballPositionType') }} |
51
|
|
|
if (null !== $enumType) { |
52
|
|
|
$this->throwExceptionIfEnumTypeIsNotRegistered($enumType); |
53
|
|
|
|
54
|
|
|
return (string) \constant($this->registeredEnumTypes[$enumType].'::'.$enumConstant); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// If ENUM type wasn't set, e.g. {{ 'CENTER'|enum_constant }} |
58
|
|
|
$this->findOccurrences($enumConstant); |
59
|
|
|
|
60
|
|
|
if ($this->onlyOneOccurrenceFound()) { |
61
|
|
|
return (string) \constant(\array_pop($this->occurrences).'::'.$enumConstant); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($this->moreThanOneOccurrenceFound()) { |
65
|
|
|
$exceptionMessage = \sprintf( |
66
|
|
|
'Constant "%s" is found in few registered ENUM types. You should manually set the appropriate one.', |
67
|
|
|
$enumConstant |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
throw new ConstantIsFoundInFewRegisteredEnumTypesException($exceptionMessage); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$exceptionMessage = \sprintf( |
74
|
|
|
'Constant "%s" was not found in any registered ENUM type.', |
75
|
|
|
$enumConstant |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
throw new ConstantIsNotFoundInAnyRegisteredEnumTypeException($exceptionMessage); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
throw $this->createNoRegisteredEnumTypesException(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $enumConstant |
86
|
|
|
* |
87
|
|
|
* @throws \ReflectionException |
88
|
|
|
*/ |
89
|
|
|
private function findOccurrences(string $enumConstant): void |
90
|
|
|
{ |
91
|
|
|
/** @var class-string<\Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType> $registeredEnumType */ |
|
|
|
|
92
|
|
|
foreach ($this->registeredEnumTypes as $registeredEnumType) { |
93
|
|
|
$reflection = new \ReflectionClass($registeredEnumType); |
94
|
|
|
|
95
|
|
|
if ($reflection->hasConstant($enumConstant)) { |
96
|
|
|
$this->occurrences[] = $registeredEnumType; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.