ValuetoChoiceOrTextTransformer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 41
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A reverseTransform() 0 8 2
A transform() 0 8 2
1
<?php
2
3
namespace Black\Common\Application\Form\Transformer;
4
5
use Symfony\Component\Form\DataTransformerInterface;
6
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
7
8
/**
9
 * Class ValuetoChoiceOrTextTransformer
10
 *
11
 * ValueToChoiceOrTextTransformer save the content of TextType value if "other" is selected.
12
 * Otherwise, this transformer save the value of the ChoiceType
13
 */
14
class ValuetoChoiceOrTextTransformer implements DataTransformerInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $choices;
20
21
    /**
22
     * Construct the transformer
23
     *
24
     * @param ChoiceListInterface $choices
25
     */
26
    public function __construct(ChoiceListInterface $choices)
27
    {
28
        $this->choices = $choices->getChoices();
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function reverseTransform($data)
35
    {
36
        if ('other' === $data['choice']) {
37
            return $data['text'];
38
        }
39
40
        return $data['choice'];
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function transform($data)
47
    {
48
        if (in_array($data, $this->choices, true)) {
49
            return ['choice' => $data, 'text' => null];
50
        }
51
52
        return ['choice' => 'other', 'text' => $data];
53
    }
54
}
55