Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/serializer/Normalizer/ArrayDenormalizer.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 * @throws UnexpectedValueException | |
36 */ | |
37 public function denormalize($data, $class, $format = null, array $context = array()) | |
38 { | |
39 if ($this->serializer === null) { | |
40 throw new BadMethodCallException('Please set a serializer before calling denormalize()!'); | |
41 } | |
42 if (!is_array($data)) { | |
43 throw new InvalidArgumentException('Data expected to be an array, '.gettype($data).' given.'); | |
44 } | |
45 if (substr($class, -2) !== '[]') { | |
46 throw new InvalidArgumentException('Unsupported class: '.$class); | |
47 } | |
48 | |
49 $serializer = $this->serializer; | |
50 $class = substr($class, 0, -2); | |
51 | |
52 $builtinType = isset($context['key_type']) ? $context['key_type']->getBuiltinType() : null; | |
53 foreach ($data as $key => $value) { | |
54 if (null !== $builtinType && !call_user_func('is_'.$builtinType, $key)) { | |
55 throw new UnexpectedValueException(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, $builtinType, gettype($key))); | |
56 } | |
57 | |
58 $data[$key] = $serializer->denormalize($value, $class, $format, $context); | |
59 } | |
60 | |
61 return $data; | |
62 } | |
63 | |
64 /** | |
65 * {@inheritdoc} | |
66 */ | |
67 public function supportsDenormalization($data, $type, $format = null) | |
68 { | |
69 return substr($type, -2) === '[]' | |
70 && $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format); | |
71 } | |
72 | |
73 /** | |
74 * {@inheritdoc} | |
75 */ | |
76 public function setSerializer(SerializerInterface $serializer) | |
77 { | |
78 if (!$serializer instanceof DenormalizerInterface) { | |
79 throw new InvalidArgumentException('Expected a serializer that also implements DenormalizerInterface.'); | |
80 } | |
81 | |
82 $this->serializer = $serializer; | |
83 } | |
84 } |