ReadableEnumValueTwigExtension::findOccurrences()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
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\DBAL\Types\AbstractEnumType;
16
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsNotRegisteredException;
17
use Fresh\DoctrineEnumBundle\Exception\EnumType\NoRegisteredEnumTypesException;
18
use Fresh\DoctrineEnumBundle\Exception\EnumValue\ValueIsFoundInFewRegisteredEnumTypesException;
19
use Fresh\DoctrineEnumBundle\Exception\EnumValue\ValueIsNotFoundInAnyRegisteredEnumTypeException;
20
use Twig\TwigFilter;
21
22
/**
23
 * ReadableEnumValueExtension returns the readable variant of ENUM value.
24
 *
25
 * @author Artem Henvald <[email protected]>
26
 */
27
class ReadableEnumValueTwigExtension extends AbstractEnumTwigExtension
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getFilters(): array
33
    {
34
        return [new TwigFilter('readable_enum', [$this, 'getReadableEnumValue'])];
35
    }
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)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Fresh\DoctrineEnumBundl...AbstractEnumType::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
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
97
    {
98
        foreach ($this->registeredEnumTypes as $registeredEnumType) {
99
            if ($registeredEnumType::isValueExist($enumValue)) {
100
                $this->occurrences[] = $registeredEnumType;
101
            }
102
        }
103
    }
104
}
105