Completed
Pull Request — master (#90)
by Arnaud
02:00
created

Mapped   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 73
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

5 Methods

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