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