/** * {@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; }
/** * {@inheritdoc} */ public function transform($object, ContextInterface $context) { if (!$object instanceof \Traversable) { throw UnsupportedClassException::create($object); } if (!$object instanceof \ArrayAccess) { throw new UnsupportedClassException(sprintf('The collection object for transform should implement \\ArrayAccess, %s given.', get_class($object))); } // Try crete a new collection try { $transformed = $this->createCollection($object); } catch (\Exception $e) { throw new UnsupportedClassException(sprintf('Could not create new collection instance with class "%s" (Use constructor).', get_class($object)), 0, $e); } foreach ($object as $key => $child) { $transformed[$key] = $this->manager->transform($child); } return $transformed; }
/** * Transform value * * @param object $object * @param mixed $value * @param PropertyMetadata $metadata * @param \ReflectionProperty $property * * @return mixed * * @throws TransformationFailedException */ protected function transformValue($object, $value, PropertyMetadata $metadata, \ReflectionProperty $property) { // Check, if should use expression language for get value if ($metadata->getExpressionValue()) { if (!$this->expressionLanguage) { throw new \LogicException('Can not evaluate expression language. Please inject ExpressionLanguage to transformer.'); } $attributes = ['object' => $object, 'value' => $value]; $value = $this->expressionLanguage->evaluate($metadata->getExpressionValue(), $attributes); } // Check, if should use transformer for this value (recursive) if ($metadata->isShouldTransform()) { if (!is_object($value)) { throw new TransformationFailedException(sprintf('Can not transform property "%s" in class "%s". The value must be a object, but "%s" given.', $property->getName(), $property->getDeclaringClass()->getName(), gettype($value))); } return $this->transformerManager->transform($value); } return $value; }