Fix bug in style changes for the Use cases on the live site.
[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\SerializerAwareInterface;
17 use Symfony\Component\Serializer\SerializerInterface;
18
19 /**
20  * Denormalizes arrays of objects.
21  *
22  * @author Alexander M. Turek <me@derrabus.de>
23  */
24 class ArrayDenormalizer implements DenormalizerInterface, SerializerAwareInterface
25 {
26     /**
27      * @var SerializerInterface|DenormalizerInterface
28      */
29     private $serializer;
30
31     /**
32      * {@inheritdoc}
33      */
34     public function denormalize($data, $class, $format = null, array $context = array())
35     {
36         if ($this->serializer === null) {
37             throw new BadMethodCallException('Please set a serializer before calling denormalize()!');
38         }
39         if (!is_array($data)) {
40             throw new InvalidArgumentException('Data expected to be an array, '.gettype($data).' given.');
41         }
42         if (substr($class, -2) !== '[]') {
43             throw new InvalidArgumentException('Unsupported class: '.$class);
44         }
45
46         $serializer = $this->serializer;
47         $class = substr($class, 0, -2);
48
49         return array_map(
50             function ($data) use ($serializer, $class, $format, $context) {
51                 return $serializer->denormalize($data, $class, $format, $context);
52             },
53             $data
54         );
55     }
56
57     /**
58      * {@inheritdoc}
59      */
60     public function supportsDenormalization($data, $type, $format = null)
61     {
62         return substr($type, -2) === '[]'
63             && $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format);
64     }
65
66     /**
67      * {@inheritdoc}
68      */
69     public function setSerializer(SerializerInterface $serializer)
70     {
71         if (!$serializer instanceof DenormalizerInterface) {
72             throw new InvalidArgumentException('Expected a serializer that also implements DenormalizerInterface.');
73         }
74
75         $this->serializer = $serializer;
76     }
77 }