Completed
Push — sensio-insight-fixes ( 787c3e )
by Yann
07:09
created

EnumExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Yokai\EnumBundle\Twig\Extension;
4
5
use Twig_Extension;
6
use Twig_SimpleFilter;
7
use Twig_SimpleFunction;
8
use Yokai\EnumBundle\Registry\EnumRegistryInterface;
9
10
/**
11
 * @author Yann Eugoné <[email protected]>
12
 */
13
class EnumExtension extends Twig_Extension
14
{
15
    /**
16
     * @var EnumRegistryInterface
17
     */
18
    private $registry;
19
20
    /**
21
     * @param EnumRegistryInterface $registry
22
     */
23
    public function __construct(EnumRegistryInterface $registry)
24
    {
25
        $this->registry = $registry;
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function getFunctions()
32
    {
33
        return [
34
            new Twig_SimpleFunction('enum_label', [ $this, 'getLabel' ]),
35
            new Twig_SimpleFunction('enum_choices', [ $this, 'getChoices' ]),
36
        ];
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function getFilters()
43
    {
44
        return [
45
            new Twig_SimpleFilter('enum_label', [ $this, 'getLabel' ]),
46
        ];
47
    }
48
49
    /**
50
     * @param string $value
51
     * @param string $enum
52
     *
53
     * @return string
54
     */
55
    public function getLabel($value, $enum)
56
    {
57
        $choices = $this->getChoices($enum);
58
59
        if (isset($choices[$value])) {
60
            return $choices[$value];
61
        }
62
63
        return $value;
64
    }
65
66
    /**
67
     * @param string $enum
68
     *
69
     * @return array
70
     */
71
    public function getChoices($enum)
72
    {
73
        return $this->registry->get($enum)->getChoices();
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function getName()
80
    {
81
        return 'enum';
82
    }
83
}
84