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\Exception\TransformationFailedException
;
16 use Symfony\Component\Form\DataTransformerInterface
;
17 use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
;
20 * @author Bernhard Schussek <bschussek@gmail.com>
22 class ChoicesToValuesTransformer
implements DataTransformerInterface
29 * @param ChoiceListInterface $choiceList
31 public function __construct(ChoiceListInterface
$choiceList)
33 $this->choiceList
= $choiceList;
41 * @throws TransformationFailedException If the given value is not an array.
43 public function transform($array)
45 if (null === $array) {
49 if (!is_array($array)) {
50 throw new TransformationFailedException('Expected an array.');
53 return $this->choiceList
->getValuesForChoices($array);
61 * @throws TransformationFailedException If the given value is not an array
62 * or if no matching choice could be
63 * found for some given value.
65 public function reverseTransform($array)
67 if (null === $array) {
71 if (!is_array($array)) {
72 throw new TransformationFailedException('Expected an array.');
75 $choices = $this->choiceList
->getChoicesForValues($array);
77 if (count($choices) !== count($array)) {
78 throw new TransformationFailedException('Could not find all matching choices for the given values');