Completed
Pull Request — master (#30)
by
unknown
11:11
created

AbstractTranslatedEnum::getValues()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Yokai\EnumBundle\Enum;
4
5
use Symfony\Contracts\Translation\TranslatorInterface;
6
use Yokai\EnumBundle\Exception\InvalidTranslatePatternException;
7
8
/**
9
 * @author Yann Eugoné <[email protected]>
10
 */
11
abstract class AbstractTranslatedEnum implements EnumInterface
12
{
13
    /**
14
     * @var TranslatorInterface
15
     */
16
    private $translator;
17
18
    /**
19
     * @var string
20
     */
21
    private $transPattern;
22
23
    /**
24
     * @var string
25
     */
26
    private $transDomain = 'messages';
27
28
    /**
29
     * @param TranslatorInterface $translator
30
     * @param string              $transPattern
31
     */
32
    public function __construct(TranslatorInterface $translator, $transPattern)
33
    {
34
        if (false === strpos($transPattern, '%s')) {
35
            throw InvalidTranslatePatternException::placeholderRequired($transPattern);
36
        }
37
38
        $this->translator = $translator;
39
        $this->transPattern = $transPattern;
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function getChoices()
46
    {
47
        return array_combine(
48
            $this->getValues(),
49
            array_map(
50
                function ($value) {
51
                    return $this->translator->trans(
52
                        sprintf($this->transPattern, $value),
53
                        [],
54
                        $this->transDomain
55
                    );
56
                },
57
                $this->getValues()
58
            )
59
        );
60
    }
61
62
    /**
63
     * @param string $transDomain
64
     */
65
    public function setTransDomain($transDomain)
66
    {
67
        $this->transDomain = $transDomain;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    abstract protected function getValues();
74
}
75