4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Extension\Core\DataTransformer
;
14 use Symfony\Component\Form\DataTransformerInterface
;
15 use Symfony\Component\Form\Exception\TransformationFailedException
;
18 * Passes a value through multiple value transformers
20 * @author Bernhard Schussek <bschussek@gmail.com>
22 class DataTransformerChain
implements DataTransformerInterface
25 * The value transformers
26 * @var DataTransformerInterface[]
28 protected $transformers;
31 * Uses the given value transformers to transform values
33 * @param array $transformers
35 public function __construct(array $transformers)
37 $this->transformers
= $transformers;
41 * Passes the value through the transform() method of all nested transformers
43 * The transformers receive the value in the same order as they were passed
44 * to the constructor. Each transformer receives the result of the previous
45 * transformer as input. The output of the last transformer is returned
48 * @param mixed $value The original value
50 * @return mixed The transformed value
52 * @throws TransformationFailedException
54 public function transform($value)
56 foreach ($this->transformers
as $transformer) {
57 $value = $transformer->transform($value);
64 * Passes the value through the reverseTransform() method of all nested
67 * The transformers receive the value in the reverse order as they were passed
68 * to the constructor. Each transformer receives the result of the previous
69 * transformer as input. The output of the last transformer is returned
72 * @param mixed $value The transformed value
74 * @return mixed The reverse-transformed value
76 * @throws TransformationFailedException
78 public function reverseTransform($value)
80 for ($i = count($this->transformers
) - 1; $i >= 0; --$i) {
81 $value = $this->transformers
[$i]->reverseTransform($value);