23b3a0e95b023ebdeefd3339ebf1c0a17114a779
[yaffs-website] / vendor / symfony / serializer / Normalizer / ArrayDenormalizer.php
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\Serializer\Normalizer;
13
14 use Symfony\Component\Serializer\Exception\BadMethodCallException;
15 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16 use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
17 use Symfony\Component\Serializer\SerializerAwareInterface;
18 use Symfony\Component\Serializer\SerializerInterface;
19
20 /**
21  * Denormalizes arrays of objects.
22  *
23  * @author Alexander M. Turek <me@derrabus.de>
24  *
25  * @final since version 3.3.
26  */
27 class ArrayDenormalizer implements DenormalizerInterface, SerializerAwareInterface
28 {
29     /**
30      * @var SerializerInterface|DenormalizerInterface
31      */
32     private $serializer;
33
34     /**
35      * {@inheritdoc}
36      *
37      * @throws NotNormalizableValueException
38      */
39     public function denormalize($data, $class, $format = null, array $context = array())
40     {
41         if (null === $this->serializer) {
42             throw new BadMethodCallException('Please set a serializer before calling denormalize()!');
43         }
44         if (!\is_array($data)) {
45             throw new InvalidArgumentException('Data expected to be an array, '.\gettype($data).' given.');
46         }
47         if ('[]' !== substr($class, -2)) {
48             throw new InvalidArgumentException('Unsupported class: '.$class);
49         }
50
51         $serializer = $this->serializer;
52         $class = substr($class, 0, -2);
53
54         $builtinType = isset($context['key_type']) ? $context['key_type']->getBuiltinType() : null;
55         foreach ($data as $key => $value) {
56             if (null !== $builtinType && !\call_user_func('is_'.$builtinType, $key)) {
57                 throw new NotNormalizableValueException(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, $builtinType, \gettype($key)));
58             }
59
60             $data[$key] = $serializer->denormalize($value, $class, $format, $context);
61         }
62
63         return $data;
64     }
65
66     /**
67      * {@inheritdoc}
68      */
69     public function supportsDenormalization($data, $type, $format = null/*, array $context = array()*/)
70     {
71         $context = \func_num_args() > 3 ? func_get_arg(3) : array();
72
73         return '[]' === substr($type, -2)
74             && $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
75     }
76
77     /**
78      * {@inheritdoc}
79      */
80     public function setSerializer(SerializerInterface $serializer)
81     {
82         if (!$serializer instanceof DenormalizerInterface) {
83             throw new InvalidArgumentException('Expected a serializer that also implements DenormalizerInterface.');
84         }
85
86         $this->serializer = $serializer;
87     }
88 }