AbstractTranslatedEnum::getValues()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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