/** * {@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; }
/** * {@inheritDoc} */ public function denormalize($data, ContextInterface $context) { $class = $context->getAttribute('_class'); if (!$class) { throw new DenormalizationFailedException('Undefined class for denormalization'); } $metadata = $this->metadataFactory->loadMetadata($class); if ($context->getGroups()) { $denormalizeProperties = $metadata->getPropertiesForGroups($context->getGroups()); } else { $denormalizeProperties = $metadata->getProperties(); } $classReflection = Reflection::loadClassReflection($class); if ($object = $context->getAttribute('_object')) { if (!is_object($object)) { throw UnexpectedTypeException::create($object, 'object'); } if (get_class($object) != $classReflection && !is_a($object, $class)) { throw UnexpectedTypeException::create($object, $class); } } else { $object = $classReflection->newInstanceWithoutConstructor(); } foreach ($denormalizeProperties as $denormalizePropertyName => $propertyMetadata) { $fieldName = $propertyMetadata->getFieldName(); if (!isset($data[$fieldName])) { continue; } $objectPropertyReflection = $classReflection->getProperty($denormalizePropertyName); if (!$objectPropertyReflection->isPublic()) { $objectPropertyReflection->setAccessible(true); } $denormalizedValue = $this->denormalizeValue($data[$fieldName], $propertyMetadata, $objectPropertyReflection); $objectPropertyReflection->setValue($object, $denormalizedValue); } return $object; }