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 * @author Bernhard Schussek <bschussek@gmail.com>
20 class ValueToDuplicatesTransformer
implements DataTransformerInterface
24 public function __construct(array $keys)
30 * Duplicates the given value through the array.
32 * @param mixed $value The value
34 * @return array The array
36 public function transform($value)
40 foreach ($this->keys
as $key) {
41 $result[$key] = $value;
48 * Extracts the duplicated value from an array.
52 * @return mixed The value
54 * @throws TransformationFailedException If the given value is not an array or
55 * if the given array can not be transformed.
57 public function reverseTransform($array)
59 if (!is_array($array)) {
60 throw new TransformationFailedException('Expected an array.');
63 $result = current($array);
66 foreach ($this->keys
as $key) {
67 if (!empty($array[$key])) {
68 if ($array[$key] !== $result) {
69 throw new TransformationFailedException(
70 'All values in the array should be the same'
78 if (count($emptyKeys) > 0) {
79 if (count($emptyKeys) == count($this->keys
)) {
84 throw new TransformationFailedException(
85 sprintf('The keys "%s" should not be empty', implode('", "', $emptyKeys)