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