Skip to content

Commit cf121c9

Browse files
author
Yann Eugoné
committed
Added Twig extension that add ability to render enum value label
1 parent 2eb89a0 commit cf121c9

File tree

5 files changed

+169
-1
lines changed

5 files changed

+169
-1
lines changed

DependencyInjection/EnumExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ public function load(array $config, ContainerBuilder $container)
2121
$xmlLoader->load('services.xml');
2222
$xmlLoader->load('form_types.xml');
2323
$xmlLoader->load('validators.xml');
24+
$xmlLoader->load('twig.xml');
2425
}
2526
}

Resources/config/twig.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
9+
<service id="twig.extension.enum" class="EnumBundle\Twig\Extension\EnumExtension">
10+
<argument type="service" id="enum.registry"/>
11+
12+
<tag name="twig.extension" alias="enum"/>
13+
</service>
14+
15+
</services>
16+
17+
</container>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace EnumBundle\Tests\Twig\Extension;
4+
5+
use EnumBundle\Registry\EnumRegistryInterface;
6+
use EnumBundle\Twig\Extension\EnumExtension;
7+
8+
/**
9+
* @author Yann Eugoné <[email protected]>
10+
*/
11+
class EnumExtensionTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var EnumRegistryInterface|\Prophecy\Prophecy\ObjectProphecy
15+
*/
16+
private $registry;
17+
18+
protected function setUp()
19+
{
20+
$this->registry = $this->prophesize('EnumBundle\Registry\EnumRegistryInterface');
21+
}
22+
23+
protected function tearDown()
24+
{
25+
unset(
26+
$this->registry
27+
);
28+
}
29+
30+
public function testEnumLabel()
31+
{
32+
$enum = $this->prophesize('EnumBundle\Enum\EnumInterface');
33+
$enum->getChoices()
34+
->willReturn(['foo' => 'FOO', 'bar' => 'BAR']);
35+
36+
$this->registry->get('test')
37+
->willReturn($enum->reveal());
38+
39+
$twig = $this->createEnvironment();
40+
41+
$this->assertSame(
42+
'FOO',
43+
$twig->createTemplate("{{ 'foo'|enum_label('test') }}")->render([])
44+
);
45+
$this->assertSame(
46+
'BAR',
47+
$twig->createTemplate("{{ enum_label('bar', 'test') }}")->render([])
48+
);
49+
50+
$this->assertSame(
51+
'not_exist',
52+
$twig->createTemplate("{{ 'not_exist'|enum_label('test') }}")->render([])
53+
);
54+
$this->assertSame(
55+
'not_exist',
56+
$twig->createTemplate("{{ enum_label('not_exist', 'test') }}")->render([])
57+
);
58+
}
59+
60+
/**
61+
* @return \Twig_Environment
62+
*/
63+
protected function createEnvironment()
64+
{
65+
$loader = new \Twig_Loader_Array([]);
66+
$twig = new \Twig_Environment($loader, ['debug' => true, 'cache' => false, 'autoescape' => false]);
67+
$twig->addExtension($this->createExtension());
68+
69+
return $twig;
70+
}
71+
72+
/**
73+
* @return EnumExtension
74+
*/
75+
private function createExtension()
76+
{
77+
return new EnumExtension($this->registry->reveal());
78+
}
79+
}

Twig/Extension/EnumExtension.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace EnumBundle\Twig\Extension;
4+
5+
use EnumBundle\Registry\EnumRegistryInterface;
6+
use Twig_Extension;
7+
8+
/**
9+
* @author Yann Eugoné <[email protected]>
10+
*/
11+
class EnumExtension extends Twig_Extension
12+
{
13+
/**
14+
* @var EnumRegistryInterface
15+
*/
16+
private $registry;
17+
18+
/**
19+
* @param EnumRegistryInterface $registry
20+
*/
21+
public function __construct(EnumRegistryInterface $registry)
22+
{
23+
$this->registry = $registry;
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function getFunctions()
30+
{
31+
return [
32+
new \Twig_SimpleFunction('enum_label', [ $this, 'getLabel' ]),
33+
];
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function getFilters()
40+
{
41+
return [
42+
new \Twig_SimpleFilter('enum_label', [ $this, 'getLabel' ]),
43+
];
44+
}
45+
46+
/**
47+
* @param string $value
48+
* @param string $enum
49+
*
50+
* @return string
51+
*/
52+
public function getLabel($value, $enum)
53+
{
54+
$choices = $this->registry->get($enum)->getChoices();
55+
56+
if (isset($choices[$value])) {
57+
return $choices[$value];
58+
}
59+
60+
return $value;
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function getName()
67+
{
68+
return 'enum';
69+
}
70+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
],
1111
"require": {
1212
"php": ">=5.5",
13-
"symfony/framework-bundle": "~2.7"
13+
"symfony/framework-bundle": "~2.7",
14+
"twig/twig": "~1.20|~2.0"
1415
},
1516
"require-dev": {
1617
"symfony/form": "~2.7",

0 commit comments

Comments
 (0)