Completed
Push — master ( ca4c9c...b6ea60 )
by Artem
15s
created

ReadableEnumValueExtension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 74
rs 10

2 Methods

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