/** * {@inheritDoc} */ public function denormalize($data, ContextInterface $context) { if (!is_array($data)) { throw DenormalizationFailedException::unexpected($data, 'array'); } $collectionClass = $context->getAttribute('collection_class'); $class = $context->getAttribute('class'); if (!$class) { throw new DenormalizationFailedException('Undefined denormalizer class.'); } try { $this->normalizerManager->getNormalizerForClass($class); } catch (UnsupportedClassException $e) { throw new DenormalizationFailedException(sprintf('Not found normalizer for denormalize collection. Collection class "%s". Denormalizer class "%s".', $collectionClass, $class), 0, $e); } if ($collectionClass) { $reflection = Reflection::loadClassReflection($collectionClass); $collection = $reflection->newInstanceWithoutConstructor(); } else { $collection = []; } if (is_object($collection) && !$collection instanceof \ArrayAccess) { throw new DenormalizationFailedException(sprintf('The collection instance for denormalize data should implement "ArrayAccess", but "%s" given.', get_class($collection))); } foreach ($data as $key => $childData) { $denormalized = $this->normalizerManager->denormalize($class, $childData); $collection[$key] = $denormalized; } return $collection; }
/** * Denormalize value * * @param mixed $value * @param PropertyMetadata $metadata * @param \ReflectionProperty $property * * @return mixed * * @throws DenormalizationFailedException */ protected function denormalizeValue($value, PropertyMetadata $metadata, \ReflectionProperty $property) { if (null === $value) { return $value; } if ($metadata->isShouldNormalize() || $metadata->getDenormalizationClass()) { if (!$metadata->getDenormalizationClass()) { throw new DenormalizationFailedException(sprintf('Can not denormalize property "%s" in class "%s". Undefined denormalization class.', $property->getName(), $property->getDeclaringClass()->getName())); } return $this->normalizerManager->denormalize($metadata->getDenormalizationClass(), $value); } return $value; }