Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Serializer\Normalizer; Chris@0: Chris@0: use Symfony\Component\Serializer\Exception\BadMethodCallException; Chris@0: use Symfony\Component\Serializer\Exception\InvalidArgumentException; Chris@14: use Symfony\Component\Serializer\Exception\NotNormalizableValueException; Chris@0: use Symfony\Component\Serializer\SerializerAwareInterface; Chris@0: use Symfony\Component\Serializer\SerializerInterface; Chris@0: Chris@0: /** Chris@0: * Denormalizes arrays of objects. Chris@0: * Chris@0: * @author Alexander M. Turek Chris@14: * Chris@14: * @final since version 3.3. Chris@0: */ Chris@0: class ArrayDenormalizer implements DenormalizerInterface, SerializerAwareInterface Chris@0: { Chris@0: /** Chris@0: * @var SerializerInterface|DenormalizerInterface Chris@0: */ Chris@0: private $serializer; Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@14: * Chris@14: * @throws NotNormalizableValueException Chris@0: */ Chris@17: public function denormalize($data, $class, $format = null, array $context = []) Chris@0: { Chris@14: if (null === $this->serializer) { Chris@0: throw new BadMethodCallException('Please set a serializer before calling denormalize()!'); Chris@0: } Chris@17: if (!\is_array($data)) { Chris@17: throw new InvalidArgumentException('Data expected to be an array, '.\gettype($data).' given.'); Chris@0: } Chris@14: if ('[]' !== substr($class, -2)) { Chris@0: throw new InvalidArgumentException('Unsupported class: '.$class); Chris@0: } Chris@0: Chris@0: $serializer = $this->serializer; Chris@0: $class = substr($class, 0, -2); Chris@0: Chris@0: $builtinType = isset($context['key_type']) ? $context['key_type']->getBuiltinType() : null; Chris@0: foreach ($data as $key => $value) { Chris@17: if (null !== $builtinType && !\call_user_func('is_'.$builtinType, $key)) { Chris@17: throw new NotNormalizableValueException(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, $builtinType, \gettype($key))); Chris@0: } Chris@0: Chris@0: $data[$key] = $serializer->denormalize($value, $class, $format, $context); Chris@0: } Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: public function supportsDenormalization($data, $type, $format = null/*, array $context = []*/) Chris@0: { Chris@17: $context = \func_num_args() > 3 ? func_get_arg(3) : []; Chris@14: Chris@14: return '[]' === substr($type, -2) Chris@14: && $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setSerializer(SerializerInterface $serializer) Chris@0: { Chris@0: if (!$serializer instanceof DenormalizerInterface) { Chris@0: throw new InvalidArgumentException('Expected a serializer that also implements DenormalizerInterface.'); Chris@0: } Chris@0: Chris@0: $this->serializer = $serializer; Chris@0: } Chris@0: }