/** * {@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 extract($object) { if (!$this->transformerManager->supports($object)) { throw new ExtractorNotSupportedException(sprintf('Can not extract data from object via class "%s" (Transformer).', get_class($object))); } $transformedObject = $this->transformerManager->transform($object); if (!$this->normalizerManager->supports($transformedObject)) { throw new ExtractorNotSupportedException(sprintf('Can not extract data from object via class "%s" (Normalizer).', get_class($transformedObject))); } $normalizedData = $this->normalizerManager->normalize($transformedObject); return $normalizedData; }
/** * Process transform object response * * @param ObjectResponse $objectResponse * * @return Response */ private function doTransformObjectResponse(ObjectResponse $objectResponse) { $responseData = $objectResponse; if ($objectResponse->isActionTransform()) { try { $responseData = $this->transformerManager->transform($responseData->getObject(), $objectResponse->getTransformerContext()); if (!is_object($responseData)) { throw UnexpectedTypeException::create($responseData, 'object'); } } catch (TransformerUnsupportedObjectException $e) { throw new \RuntimeException(sprintf('Can not transform object with class "%s".', get_class($objectResponse)), 0, $e); } } try { $responseData = $this->normalizerManager->normalize($responseData instanceof ObjectResponse ? $responseData->getObject() : $responseData, $objectResponse->getNormalizerContext()); if (!is_array($responseData)) { throw UnexpectedTypeException::create($responseData, 'array'); } } catch (NormalizerUnsupportedObjectException $e) { throw new \RuntimeException(sprintf('Can not normalize object with class "%s".', get_class($responseData)), 0, $e); } if ($objectResponse->isEmptyResponse()) { $response = new EmptyResponse($responseData, $objectResponse->getHttpStatusCode()); } else { $response = new Response($responseData, $objectResponse->getHttpStatusCode()); } return $response; }
/** * 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; }