for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Yokai\EnumBundle;
use Symfony\Contracts\Translation\TranslatorInterface;
use Yokai\EnumBundle\Exception\InvalidTranslatePatternException;
/**
* @author Yann Eugoné <[email protected]>
*/
abstract class AbstractTranslatedEnum implements EnumInterface
{
* @var TranslatorInterface
private $translator;
* @var string
private $transPattern;
private $transDomain = 'messages';
* @param TranslatorInterface $translator
* @param string $transPattern
public function __construct(TranslatorInterface $translator, string $transPattern)
if (false === strpos($transPattern, '%s')) {
throw InvalidTranslatePatternException::placeholderRequired($transPattern);
}
$this->translator = $translator;
$this->transPattern = $transPattern;
* @return array
public function getChoices(): array
return array_combine(
$this->getValues(),
array_map(
function (string $value): string {
return $this->translator->trans(
sprintf($this->transPattern, $value),
[],
$this->transDomain
);
},
$this->getValues()
)
* @param string $transDomain
public function setTransDomain(string $transDomain): void
$this->transDomain = $transDomain;
abstract protected function getValues(): array;