Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Form/DataTransformer/TagsTransformer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\TaggingBundle\Form\DataTransformer;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Kunstmaan\TaggingBundle\Entity\TagManager;
7
use Symfony\Component\Form\DataTransformerInterface;
8
9
class TagsTransformer implements DataTransformerInterface
10
{
11
    protected $tagManager;
12
13
    public function __construct(TagManager $tagManager)
14
    {
15
        $this->tagManager = $tagManager;
16
    }
17
18
    /**
19
     * Transforms a value from the original representation to a transformed representation.
20
     *
21
     * This method is called on two occasions inside a form field:
22
     *
23
     * 1. When the form field is initialized with the data attached from the datasource (object or array).
24
     * 2. When data from a request is bound using {@link Field::bind()} to transform the new input data
25
     *    back into the renderable format. For example if you have a date field and bind '2009-10-10' onto
26
     *    it you might accept this value because its easily parsed, but the transformer still writes back
27
     *    "2009/10/10" onto the form field (for further displaying or other purposes).
28
     *
29
     * This method must be able to deal with empty values. Usually this will
30
     * be NULL, but depending on your implementation other empty values are
31
     * possible as well (such as empty strings). The reasoning behind this is
32
     * that value transformers must be chainable. If the transform() method
33
     * of the first value transformer outputs NULL, the second value transformer
34
     * must be able to process that value.
35
     *
36
     * By convention, transform() should return an empty string if NULL is
37
     * passed.
38
     *
39
     * @param mixed $value The value in the original representation
40
     *
41
     * @return mixed The value in the transformed representation
42
     *
43
     * @throws UnexpectedTypeException       when the argument is not a string
44
     * @throws TransformationFailedException when the transformation fails
45
     */
46
    public function transform($value)
47
    {
48
        $result = [];
49
50
        if (!($value instanceof ArrayCollection)) {
51
            return $result;
52
        }
53
54
        foreach ($value as $tag) {
55
            $result[] = $tag->getId();
56
        }
57
58
        return $result;
59
    }
60
61
    /**
62
     * Transforms a value from the transformed representation to its originalœ
63
     * representation.
64
     *
65
     * This method is called when {@link Field::bind()} is called to transform the requests tainted data
66
     * into an acceptable format for your data processing/model layer.
67
68
     * This method must be able to deal with empty values. Usually this will
69
     * be an empty string, but depending on your implementation other empty
70
     * values are possible as well (such as empty strings). The reasoning behind
71
     * this is that value transformers must be chainable. If the
72
     * reverseTransform() method of the first value transformer outputs an
73
     * empty string, the second value transformer must be able to process that
74
     * value.
75
     *
76
     * By convention, reverseTransform() should return NULL if an empty string
77
     * is passed.
78
     *
79
     * @param mixed $value The value in the transformed representation
80
     *
81
     * @return mixed The value in the original representation
0 ignored issues
show
Consider making the return type a bit more specific; maybe use ArrayCollection.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
82
     *
83
     * @throws UnexpectedTypeException       when the argument is not of the expected type
84
     * @throws TransformationFailedException when the transformation fails
85
     */
86
    public function reverseTransform($value)
87
    {
88
        $result = new ArrayCollection();
89
        $manager = $this->tagManager;
90
91
        foreach ($value as $tagId) {
92
            $tag = $manager->findById((int) $tagId);
93
            $result->add($tag);
94
        }
95
96
        return $result;
97
    }
98
}
99