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 ArrayToPartsTransformer
implements DataTransformerInterface
24 public function __construct(array $partMapping)
26 $this->partMapping
= $partMapping;
29 public function transform($array)
31 if (null === $array) {
35 if (!is_array($array) ) {
36 throw new TransformationFailedException('Expected an array.');
41 foreach ($this->partMapping
as $partKey => $originalKeys) {
43 $result[$partKey] = null;
45 $result[$partKey] = array_intersect_key($array, array_flip($originalKeys));
52 public function reverseTransform($array)
54 if (!is_array($array) ) {
55 throw new TransformationFailedException('Expected an array.');
61 foreach ($this->partMapping
as $partKey => $originalKeys) {
62 if (!empty($array[$partKey])) {
63 foreach ($originalKeys as $originalKey) {
64 if (isset($array[$partKey][$originalKey])) {
65 $result[$originalKey] = $array[$partKey][$originalKey];
69 $emptyKeys[] = $partKey;
73 if (count($emptyKeys) > 0) {
74 if (count($emptyKeys) === count($this->partMapping
)) {
79 throw new TransformationFailedException(
80 sprintf('The keys "%s" should not be empty', implode('", "', $emptyKeys)