4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Normalizer;
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;
20 * Denormalizes arrays of objects.
22 * @author Alexander M. Turek <me@derrabus.de>
24 class ArrayDenormalizer implements DenormalizerInterface, SerializerAwareInterface
27 * @var SerializerInterface|DenormalizerInterface
34 public function denormalize($data, $class, $format = null, array $context = array())
36 if ($this->serializer === null) {
37 throw new BadMethodCallException('Please set a serializer before calling denormalize()!');
39 if (!is_array($data)) {
40 throw new InvalidArgumentException('Data expected to be an array, '.gettype($data).' given.');
42 if (substr($class, -2) !== '[]') {
43 throw new InvalidArgumentException('Unsupported class: '.$class);
46 $serializer = $this->serializer;
47 $class = substr($class, 0, -2);
50 function ($data) use ($serializer, $class, $format, $context) {
51 return $serializer->denormalize($data, $class, $format, $context);
60 public function supportsDenormalization($data, $type, $format = null)
62 return substr($type, -2) === '[]'
63 && $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format);
69 public function setSerializer(SerializerInterface $serializer)
71 if (!$serializer instanceof DenormalizerInterface) {
72 throw new InvalidArgumentException('Expected a serializer that also implements DenormalizerInterface.');
75 $this->serializer = $serializer;