Completed
Pull Request — master (#90)
by Arnaud
05:12 queued 02:13
created

Mapped   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 65
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 16 2
A configureOptions() 0 7 1
A getType() 0 4 1
A setTranslator() 0 4 1
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