SimpleArrayTransformer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 10 2
A reverseTransform() 0 16 3
1
<?php
2
3
namespace EmanueleMinotto\SimpleArrayBundle\Form\DataTransformer;
4
5
use Symfony\Component\Form\DataTransformerInterface;
6
7
class SimpleArrayTransformer implements DataTransformerInterface
8
{
9
    /**
10
     * Transforms a value from the original representation to a transformed representation.
11
     *
12
     * This method is called on two occasions inside a form field:
13
     *
14
     * 1. When the form field is initialized with the data attached from the datasource (object or array).
15
     * 2. When data from a request is submitted using {@link Form::submit()} to transform the new input data
16
     *    back into the renderable format. For example if you have a date field and submit '2009-10-10'
17
     *    you might accept this value because its easily parsed, but the transformer still writes back
18
     *    "2009/10/10" onto the form field (for further displaying or other purposes).
19
     *
20
     * This method must be able to deal with empty values. Usually this will
21
     * be NULL, but depending on your implementation other empty values are
22
     * possible as well (such as empty strings). The reasoning behind this is
23
     * that value transformers must be chainable. If the transform() method
24
     * of the first value transformer outputs NULL, the second value transformer
25
     * must be able to process that value.
26
     *
27
     * By convention, transform() should return an empty string if NULL is
28
     * passed.
29
     *
30
     * @param mixed $value The value in the original representation.
31
     *
32
     * @throws TransformationFailedException When the transformation fails.
33
     *
34
     * @return mixed The value in the transformed representation.
35
     */
36
    public function transform($value)
37
    {
38
        if (!$value) {
39
            // default value
40
            $value = [];
41
        }
42
43
        // concatenate the values to one string
44
        return trim(implode(', ', $value));
45
    }
46
47
    /**
48
     * Transforms a value from the transformed representation to its original representation.
49
     *
50
     * This method is called when {@link Form::submit()} is called to transform the requests tainted data
51
     * into an acceptable format for your data processing/model layer.
52
     *
53
     * This method must be able to deal with empty values. Usually this will
54
     * be an empty string, but depending on your implementation other empty
55
     * values are possible as well (such as empty strings). The reasoning behind
56
     * this is that value transformers must be chainable. If the
57
     * reverseTransform() method of the first value transformer outputs an
58
     * empty string, the second value transformer must be able to process that
59
     * value.
60
     *
61
     * By convention, reverseTransform() should return NULL if an empty string
62
     * is passed.
63
     *
64
     * @param mixed $value The value in the transformed representation.
65
     *
66
     * @throws TransformationFailedException When the transformation fails.
67
     *
68
     * @return mixed The value in the original representation.
69
     */
70
    public function reverseTransform($value)
71
    {
72
        if (!$value) {
73
            // default
74
            $value = '';
75
        }
76
77
        if (!is_array($value)) {
78
            $value = explode(',', $value);
79
        }
80
81
        // 1. Split the string with commas
82
        // 2. Remove whitespaces around the values
83
        // 3. Remove empty elements (like in "tag1,tag2, ,,tag3,tag4")
84
        return array_filter(array_map('trim', $value));
85
    }
86
}
87