Completed
Pull Request — master (#139)
by Artem
01:36
created

Twig/Extension/ReadableEnumValueTwigExtension.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\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 Henvald <[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);
0 ignored issues
show
The method getReadableValue cannot be called on $this->registeredEnumTypes[$enumType] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
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