Completed
Push — master ( d5349d...2edaf3 )
by Yann
9s
created

EnumExtension::getLabel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
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 2
    public function __construct(EnumRegistryInterface $registry)
24
    {
25 2
        $this->registry = $registry;
26 2
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31 2
    public function getFunctions()
32
    {
33
        return [
34 2
            new Twig_SimpleFunction('enum_label', [ $this, 'getLabel' ]),
35 2
            new Twig_SimpleFunction('enum_choices', [ $this, 'getChoices' ]),
36 2
        ];
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 2
    public function getFilters()
43
    {
44
        return [
45 2
            new Twig_SimpleFilter('enum_label', [ $this, 'getLabel' ]),
46 2
        ];
47
    }
48
49
    /**
50
     * @param string $value
51
     * @param string $enum
52
     *
53
     * @return string
54
     */
55 1
    public function getLabel($value, $enum)
56
    {
57 1
        $choices = $this->getChoices($enum);
58
59 1
        if (isset($choices[$value])) {
60 1
            return $choices[$value];
61
        }
62
63 1
        return $value;
64
    }
65
66
    /**
67
     * @param string $enum
68
     *
69
     * @return array
70
     */
71 2
    public function getChoices($enum)
72
    {
73 2
        return $this->registry->get($enum)->getChoices();
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79 2
    public function getName()
80
    {
81 2
        return 'enum';
82
    }
83
}
84