EnumValuesAsArrayTwigExtension::getFunctions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\LogicException;
18
use Twig\TwigFunction;
19
20
/**
21
 * EnumValuesAsArrayTwigExtension.
22
 *
23
 * @author Artem Henvald <[email protected]>
24
 */
25
class EnumValuesAsArrayTwigExtension extends AbstractEnumTwigExtension
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getFunctions(): array
31
    {
32
        return [
33
            new TwigFunction('enum_values', [$this, 'getEnumValuesAsArray']),
34
            new TwigFunction('enum_readable_values', [$this, 'getReadableEnumValuesAsArray']),
35
        ];
36
    }
37
38
    /**
39
     * @param string $enumType
40
     *
41
     * @throws EnumTypeIsNotRegisteredException
42
     * @throws NoRegisteredEnumTypesException
43
     *
44
     * @return string[]
45
     */
46
    public function getEnumValuesAsArray(string $enumType): array
47
    {
48
        return $this->callEnumTypeStaticMethod($enumType, 'getValues');
49
    }
50
51
    /**
52
     * @param string $enumType
53
     *
54
     * @throws EnumTypeIsNotRegisteredException
55
     * @throws NoRegisteredEnumTypesException
56
     *
57
     * @return string[]
58
     */
59
    public function getReadableEnumValuesAsArray(string $enumType): array
60
    {
61
        return $this->callEnumTypeStaticMethod($enumType, 'getReadableValues');
62
    }
63
64
    /**
65
     * @param string $enumType
66
     * @param string $staticMethodName
67
     *
68
     * @throws LogicException
69
     * @throws EnumTypeIsNotRegisteredException
70
     * @throws NoRegisteredEnumTypesException
71
     *
72
     * @return string[]
73
     */
74
    private function callEnumTypeStaticMethod(string $enumType, string $staticMethodName): array
75
    {
76
        if ($this->hasRegisteredEnumTypes()) {
77
            $this->throwExceptionIfEnumTypeIsNotRegistered($enumType);
78
79
            $function = [$this->registeredEnumTypes[$enumType], $staticMethodName];
80
81
            if (!\is_callable($function)) {
82
                throw new LogicException(\sprintf('%s::%s is not a valid exception', $this->registeredEnumTypes[$enumType], $staticMethodName));
83
            }
84
85
            return $function();
86
        }
87
88
        throw $this->createNoRegisteredEnumTypesException();
89
    }
90
}
91