Security update for Core, with self-updated composer
[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\UnexpectedValueException;
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 class ArrayDenormalizer implements DenormalizerInterface, SerializerAwareInterface
26 {
27     /**
28      * @var SerializerInterface|DenormalizerInterface
29      */
30     private $serializer;
31
32     /**
33      * {@inheritdoc}
34      */
35     public function denormalize($data, $class, $format = null, array $context = array())
36     {
37         if ($this->serializer === null) {
38             throw new BadMethodCallException('Please set a serializer before calling denormalize()!');
39         }
40         if (!is_array($data)) {
41             throw new InvalidArgumentException('Data expected to be an array, '.gettype($data).' given.');
42         }
43         if (substr($class, -2) !== '[]') {
44             throw new InvalidArgumentException('Unsupported class: '.$class);
45         }
46
47         $serializer = $this->serializer;
48         $class = substr($class, 0, -2);
49
50         $builtinType = isset($context['key_type']) ? $context['key_type']->getBuiltinType() : null;
51         foreach ($data as $key => $value) {
52             if (null !== $builtinType && !call_user_func('is_'.$builtinType, $key)) {
53                 throw new UnexpectedValueException(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, $builtinType, gettype($key)));
54             }
55
56             $data[$key] = $serializer->denormalize($value, $class, $format, $context);
57         }
58
59         return $data;
60     }
61
62     /**
63      * {@inheritdoc}
64      */
65     public function supportsDenormalization($data, $type, $format = null)
66     {
67         return substr($type, -2) === '[]'
68             && $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format);
69     }
70
71     /**
72      * {@inheritdoc}
73      */
74     public function setSerializer(SerializerInterface $serializer)
75     {
76         if (!$serializer instanceof DenormalizerInterface) {
77             throw new InvalidArgumentException('Expected a serializer that also implements DenormalizerInterface.');
78         }
79
80         $this->serializer = $serializer;
81     }
82 }