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