diff options
Diffstat (limited to 'vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToValuesTransformer.php')
-rw-r--r-- | vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToValuesTransformer.php | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToValuesTransformer.php b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToValuesTransformer.php new file mode 100644 index 00000000..4492865e --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToValuesTransformer.php | |||
@@ -0,0 +1,83 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\Form\Extension\Core\DataTransformer; | ||
13 | |||
14 | use Symfony\Component\Form\Exception\TransformationFailedException; | ||
15 | |||
16 | use Symfony\Component\Form\DataTransformerInterface; | ||
17 | use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface; | ||
18 | |||
19 | /** | ||
20 | * @author Bernhard Schussek <bschussek@gmail.com> | ||
21 | */ | ||
22 | class ChoicesToValuesTransformer implements DataTransformerInterface | ||
23 | { | ||
24 | private $choiceList; | ||
25 | |||
26 | /** | ||
27 | * Constructor. | ||
28 | * | ||
29 | * @param ChoiceListInterface $choiceList | ||
30 | */ | ||
31 | public function __construct(ChoiceListInterface $choiceList) | ||
32 | { | ||
33 | $this->choiceList = $choiceList; | ||
34 | } | ||
35 | |||
36 | /** | ||
37 | * @param array $array | ||
38 | * | ||
39 | * @return array | ||
40 | * | ||
41 | * @throws TransformationFailedException If the given value is not an array. | ||
42 | */ | ||
43 | public function transform($array) | ||
44 | { | ||
45 | if (null === $array) { | ||
46 | return array(); | ||
47 | } | ||
48 | |||
49 | if (!is_array($array)) { | ||
50 | throw new TransformationFailedException('Expected an array.'); | ||
51 | } | ||
52 | |||
53 | return $this->choiceList->getValuesForChoices($array); | ||
54 | } | ||
55 | |||
56 | /** | ||
57 | * @param array $array | ||
58 | * | ||
59 | * @return array | ||
60 | * | ||
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. | ||
64 | */ | ||
65 | public function reverseTransform($array) | ||
66 | { | ||
67 | if (null === $array) { | ||
68 | return array(); | ||
69 | } | ||
70 | |||
71 | if (!is_array($array)) { | ||
72 | throw new TransformationFailedException('Expected an array.'); | ||
73 | } | ||
74 | |||
75 | $choices = $this->choiceList->getChoicesForValues($array); | ||
76 | |||
77 | if (count($choices) !== count($array)) { | ||
78 | throw new TransformationFailedException('Could not find all matching choices for the given values'); | ||
79 | } | ||
80 | |||
81 | return $choices; | ||
82 | } | ||
83 | } | ||