Completed
Push — master ( d5349d...2edaf3 )
by Yann
9s
created

AbstractTranslatedEnum::getChoices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 14
cts 14
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Yokai\EnumBundle\Enum;
4
5
use Symfony\Component\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 4
    public function __construct(TranslatorInterface $translator, $transPattern)
33
    {
34 4
        if (false === strpos($transPattern, '%s')) {
35 1
            throw InvalidTranslatePatternException::placeholderRequired($transPattern);
36
        }
37
38 3
        $this->translator = $translator;
39 3
        $this->transPattern = $transPattern;
40 3
    }
41
42
    /**
43
     * @return array
44
     */
45 2
    public function getChoices()
46
    {
47 2
        return array_combine(
48 2
            $this->getValues(),
49 2
            array_map(
50 2
                function ($value) {
51 2
                    return $this->translator->trans(
52 2
                        sprintf($this->transPattern, $value),
53 2
                        [],
54 2
                        $this->transDomain
55 2
                    );
56 2
                },
57 2
                $this->getValues()
58 2
            )
59 2
        );
60
    }
61
62
    /**
63
     * @param string $transDomain
64
     */
65 1
    public function setTransDomain($transDomain)
66
    {
67 1
        $this->transDomain = $transDomain;
68 1
    }
69
70
    /**
71
     * @return array
72
     */
73
    abstract protected function getValues();
74
}
75