EnumExtension   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 57
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFunctions() 0 7 1
A getFilters() 0 6 1
A getLabel() 0 4 1
A getChoices() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yokai\EnumBundle\Twig\Extension;
6
7
use Twig\Extension\AbstractExtension;
8
use Twig\TwigFilter;
9
use Twig\TwigFunction;
10
use Yokai\EnumBundle\EnumRegistry;
11
12
/**
13
 * @author Yann Eugoné <[email protected]>
14
 */
15
class EnumExtension extends AbstractExtension
16
{
17
    /**
18
     * @var EnumRegistry
19
     */
20
    private $registry;
21
22
    /**
23
     * @param EnumRegistry $registry
24
     */
25
    public function __construct(EnumRegistry $registry)
26
    {
27
        $this->registry = $registry;
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function getFunctions(): array
34
    {
35
        return [
36
            new TwigFunction('enum_label', [$this, 'getLabel']),
37
            new TwigFunction('enum_choices', [$this, 'getChoices']),
38
        ];
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function getFilters(): array
45
    {
46
        return [
47
            new TwigFilter('enum_label', [$this, 'getLabel']),
48
        ];
49
    }
50
51
    /**
52
     * @param string $value
53
     * @param string $enum
54
     *
55
     * @return string
56
     */
57
    public function getLabel(string $value, string $enum): string
58
    {
59
        return $this->getChoices($enum)[$value] ?? $value;
60
    }
61
62
    /**
63
     * @param string $enum
64
     *
65
     * @return array
66
     */
67
    public function getChoices(string $enum): array
68
    {
69
        return $this->registry->get($enum)->getChoices();
70
    }
71
}
72