Example #1
0
 /**
  * {@inheritDoc}
  */
 public function resolve(ActionInterface $action)
 {
     if (!$action instanceof CallableAction) {
         throw UnexpectedTypeException::create($action, 'FiveLab\\Component\\Api\\SMD\\Action\\CallableAction');
     }
     $callable = $action->getCallable();
     $object = null;
     if ($callable instanceof \Closure) {
         $reflection = new \ReflectionFunction($action->getCallable());
     } else {
         if (is_array($callable)) {
             if (is_object($callable[0])) {
                 $object = $callable[0];
                 $reflection = new \ReflectionMethod(get_class($callable[0]), $callable[1]);
             } else {
                 $reflection = new \ReflectionMethod($callable[0], $callable[1]);
             }
         } else {
             if (function_exists($callable)) {
                 $reflection = new \ReflectionFunction($callable);
             } else {
                 throw new \RuntimeException(sprintf('Could not resolve reflection for callable "%s".', is_object($callable) ? get_class($callable) : gettype($callable)));
             }
         }
     }
     return new BaseCallable($reflection, $object);
 }
 /**
  * Construct
  *
  * @param object                      $object
  * @param TransformerContextInterface $transformerContext
  * @param NormalizerContextInterface  $normalizerContext
  *
  * @throws UnexpectedTypeException
  */
 public function __construct($object, TransformerContextInterface $transformerContext = null, NormalizerContextInterface $normalizerContext = null)
 {
     if (!is_object($object)) {
         throw UnexpectedTypeException::create($object, 'object');
     }
     $this->action = self::ACTION_TRANSFORM | self::ACTION_NORMALIZE;
     $this->normalizerContext = $normalizerContext;
     $this->transformerContext = $transformerContext;
     $this->object = $object;
 }
 /**
  * {@inheritDoc}
  */
 public function transform($object, ContextInterface $context = null)
 {
     if (!is_object($object)) {
         throw UnexpectedTypeException::create($object, 'object');
     }
     if (!$context) {
         $context = new Context();
     }
     $transformer = $this->getTransformerForClass($object);
     if ($transformer instanceof ModelTransformerManagerAwareInterface) {
         $transformer->setModelTransformerManager($this);
     }
     $transformed = $transformer->transform($object, $context);
     return $transformed;
 }
 /**
  * Process transform object response
  *
  * @param ObjectTransformableResponse $objectResponse
  *
  * @return Response
  */
 private function doTransformObjectResponse(ObjectTransformableResponse $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 ObjectTransformableResponse ? $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;
 }