Completed
Pull Request — master (#116)
by Artem
01:18
created

ReadableEnumValueTwigExtension::findOccurrences()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
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
declare(strict_types=1);
12
13
namespace Fresh\DoctrineEnumBundle\Twig\Extension;
14
15
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsNotRegisteredException;
16
use Fresh\DoctrineEnumBundle\Exception\EnumType\NoRegisteredEnumTypesException;
17
use Fresh\DoctrineEnumBundle\Exception\EnumValue\ValueIsFoundInFewRegisteredEnumTypesException;
18
use Fresh\DoctrineEnumBundle\Exception\EnumValue\ValueIsNotFoundInAnyRegisteredEnumTypeException;
19
use Twig\TwigFilter;
20
21
/**
22
 * ReadableEnumValueExtension returns the readable variant of ENUM value.
23
 *
24
 * @author Artem Genvald <[email protected]>
25
 */
26
class ReadableEnumValueTwigExtension extends AbstractEnumTwigExtension
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getFilters(): array
32
    {
33
        return [new TwigFilter('readable_enum', [$this, 'getReadableEnumValue'])];
34
    }
35
36
    /**
37
     * @param string|null $enumValue
38
     * @param string|null $enumType
39
     *
40
     * @return string|null
41
     *
42
     * @throws EnumTypeIsNotRegisteredException
43
     * @throws NoRegisteredEnumTypesException
44
     * @throws ValueIsFoundInFewRegisteredEnumTypesException
45
     * @throws ValueIsNotFoundInAnyRegisteredEnumTypeException
46
     * @throws \InvalidArgumentException
47
     */
48
    public function getReadableEnumValue(?string $enumValue, ?string $enumType = null): ?string
49
    {
50
        if ($this->hasRegisteredEnumTypes()) {
51
            if (null === $enumValue) {
52
                return $enumValue;
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
                return \array_pop($this->occurrences)::getReadableValue($enumValue);
67
            }
68
69
            if ($this->moreThanOneOccurrenceFound()) {
70
                throw new ValueIsFoundInFewRegisteredEnumTypesException(
71
                    \sprintf(
72
                        'Value "%s" is found in few registered ENUM types. You should manually set the appropriate one',
73
                        $enumValue
74
                    )
75
                );
76
            }
77
78
            throw new ValueIsNotFoundInAnyRegisteredEnumTypeException(
79
                \sprintf(
80
                    'Value "%s" was not found in any registered ENUM type.',
81
                    $enumValue
82
                )
83
            );
84
        }
85
86
        throw $this->createNoRegisteredEnumTypesException();
87
    }
88
89
    /**
90
     * @param string $enumValue
91
     */
92
    private function findOccurrences(string $enumValue): void
93
    {
94
        foreach ($this->registeredEnumTypes as $registeredEnumType) {
95
            if ($registeredEnumType::isValueExist($enumValue)) {
96
                $this->occurrences[] = $registeredEnumType;
97
            }
98
        }
99
    }
100
}
101