for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace EnumBundle\Twig\Extension;
use EnumBundle\Registry\EnumRegistryInterface;
use Twig_Extension;
/**
* @author Yann Eugoné <[email protected]>
*/
class EnumExtension extends Twig_Extension
{
* @var EnumRegistryInterface
private $registry;
* @param EnumRegistryInterface $registry
public function __construct(EnumRegistryInterface $registry)
$this->registry = $registry;
}
* {@inheritdoc}
public function getFunctions()
return [
new \Twig_SimpleFunction('enum_label', [ $this, 'getLabel' ]),
new \Twig_SimpleFunction('enum_choices', [ $this, 'getChoices' ]),
];
public function getFilters()
new \Twig_SimpleFilter('enum_label', [ $this, 'getLabel' ]),
* @param string $value
* @param string $enum
*
* @return string
public function getLabel($value, $enum)
$choices = $this->getChoices($enum);
if (isset($choices[$value])) {
return $choices[$value];
return $value;
* @return array
public function getChoices($enum)
return $this->registry->get($enum)->getChoices();
public function getName()
return 'enum';