Completed
Pull Request — master (#90)
by Arnaud
03:24
created

Mapped::setTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace LAG\AdminBundle\Field\Field;
4
5
use LAG\AdminBundle\Field\AbstractField;
6
use LAG\AdminBundle\Field\TranslatorAwareInterface;
7
use LogicException;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
use Symfony\Component\Translation\TranslatorInterface;
10
11
class Mapped extends AbstractField implements TranslatorAwareInterface
12
{
13
    /**
14
     * @var TranslatorInterface
15
     */
16
    protected $translator;
17
    
18
    /**
19
     * Render value of the field.
20
     *
21
     * @param mixed $value Value to render
22
     *
23
     * @return mixed
24
     */
25
    public function render($value)
26
    {
27
        $mapping = $this
28
            ->options
29
            ->get('mapping')
30
        ;
31
    
32
        if (!array_key_exists($value, $mapping)) {
33
            throw new LogicException('Value "'.$value.' " not found in mapping '.implode(',', $mapping));
34
        }
35
    
36
        return $this
37
            ->translator
38
            ->trans($mapping[$value])
39
        ;
40
    }
41
    
42
    /**
43
     * Configure options resolver.
44
     *
45
     * @param OptionsResolver $resolver
46
     */
47
    public function configureOptions(OptionsResolver $resolver)
48
    {
49
        $resolver
50
            ->setRequired('mapping')
51
            ->setAllowedTypes('mapping', 'array')
52
        ;
53
    }
54
    
55
    /**
56
     * Return field type.
57
     *
58
     * @return string
59
     */
60
    public function getType()
61
    {
62
        return AbstractField::TYPE_MAPPED;
63
    }
64
    
65
    /**
66
     * Defines translator.
67
     *
68
     * @param TranslatorInterface $translator
69
     *
70
     * @return void
71
     */
72
    public function setTranslator(TranslatorInterface $translator)
73
    {
74
        $this->translator = $translator;
75
    }}
76