for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Yokai\EnumBundle\Twig\Extension;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Yokai\EnumBundle\EnumRegistry;
/**
* @author Yann Eugoné <[email protected]>
*/
class EnumExtension extends AbstractExtension
{
* @var EnumRegistry
private $registry;
* @param EnumRegistry $registry
public function __construct(EnumRegistry $registry)
$this->registry = $registry;
}
* @inheritdoc
public function getFunctions(): array
return [
new TwigFunction('enum_label', [$this, 'getLabel']),
new TwigFunction('enum_choices', [$this, 'getChoices']),
];
public function getFilters(): array
new TwigFilter('enum_label', [$this, 'getLabel']),
* @param string $value
* @param string $enum
*
* @return string
public function getLabel(string $value, string $enum): string
return $this->getChoices($enum)[$value] ?? $value;
* @return array
public function getChoices(string $enum): array
return $this->registry->get($enum)->getChoices();