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
|
|
|
namespace Fresh\DoctrineEnumBundle\Twig\Extension; |
12
|
|
|
|
13
|
|
|
use Fresh\DoctrineEnumBundle\Exception\EnumTypeIsNotRegisteredException; |
14
|
|
|
use Fresh\DoctrineEnumBundle\Exception\NoRegisteredEnumTypesException; |
15
|
|
|
use Fresh\DoctrineEnumBundle\Exception\ValueIsFoundInFewRegisteredEnumTypesException; |
16
|
|
|
use Fresh\DoctrineEnumBundle\Exception\ValueIsNotFoundInAnyRegisteredEnumTypeException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ReadableEnumValueExtension returns the readable variant of ENUM value. |
20
|
|
|
* |
21
|
|
|
* @author Artem Genvald <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ReadableEnumValueExtension extends AbstractEnumExtension |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function getFilters() |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
new \Twig_SimpleFilter( |
32
|
|
|
'readable_enum', |
33
|
|
|
[$this, 'getReadableEnumValue'] |
34
|
|
|
), |
35
|
|
|
new \Twig_SimpleFilter( |
36
|
|
|
'readable', |
37
|
|
|
[$this, 'getReadableEnumValue'], |
38
|
|
|
['deprecated' => true, 'alternative' => 'readable_enum'] |
39
|
|
|
), |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get readable variant of the ENUM value. |
45
|
|
|
* |
46
|
|
|
* @param string $enumValue ENUM value |
47
|
|
|
* @param string|null $enumType ENUM type |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
* |
51
|
|
|
* @throws EnumTypeIsNotRegisteredException |
52
|
|
|
* @throws NoRegisteredEnumTypesException |
53
|
|
|
* @throws ValueIsFoundInFewRegisteredEnumTypesException |
54
|
|
|
* @throws ValueIsNotFoundInAnyRegisteredEnumTypeException |
55
|
|
|
*/ |
56
|
|
|
public function getReadableEnumValue($enumValue, $enumType = null) |
57
|
|
|
{ |
58
|
|
|
if (!empty($this->registeredEnumTypes) && is_array($this->registeredEnumTypes)) { |
59
|
|
|
// If ENUM type was set, e.g. {{ player.position|readable_enum('BasketballPositionType') }} |
60
|
|
|
if (!empty($enumType)) { |
61
|
|
|
if (!isset($this->registeredEnumTypes[$enumType])) { |
62
|
|
|
throw new EnumTypeIsNotRegisteredException(sprintf('ENUM type "%s" is not registered.', $enumType)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @var $enumTypeClass \Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType */ |
66
|
|
|
$enumTypeClass = $this->registeredEnumTypes[$enumType]; |
67
|
|
|
|
68
|
|
|
return $enumTypeClass::getReadableValue($enumValue); |
69
|
|
|
} else { |
70
|
|
|
// If ENUM type wasn't set, e.g. {{ player.position|readable_enum }} |
71
|
|
|
$occurrences = []; |
72
|
|
|
// Check if value exists in registered ENUM types |
73
|
|
|
foreach ($this->registeredEnumTypes as $registeredEnumType) { |
74
|
|
|
if ($registeredEnumType::isValueExist($enumValue)) { |
75
|
|
|
$occurrences[] = $registeredEnumType; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// If found only one occurrence, then we know exactly which ENUM type |
80
|
|
|
if (count($occurrences) == 1) { |
81
|
|
|
$enumTypeClass = array_pop($occurrences); |
82
|
|
|
|
83
|
|
|
return $enumTypeClass::getReadableValue($enumValue); |
84
|
|
|
} elseif (count($occurrences) > 1) { |
85
|
|
|
throw new ValueIsFoundInFewRegisteredEnumTypesException(sprintf( |
86
|
|
|
'Value "%s" is found in few registered ENUM types. You should manually set the appropriate one', |
87
|
|
|
$enumValue |
88
|
|
|
)); |
89
|
|
|
} else { |
90
|
|
|
throw new ValueIsNotFoundInAnyRegisteredEnumTypeException(sprintf( |
91
|
|
|
'Value "%s" wasn\'t found in any registered ENUM type.', |
92
|
|
|
$enumValue |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} else { |
97
|
|
|
throw new NoRegisteredEnumTypesException('There are no registered ENUM types.'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|