Beispiel #1
0
 /**
  * @param ResourceDefinitionInterface $resourceDefinition
  * @param array                       $parameters
  * @return string
  * @throws \Exception
  */
 public function generate(ResourceDefinitionInterface $resourceDefinition, array $parameters = array())
 {
     $definedParams = $resourceDefinition->getParameters();
     if (count($definedParams) < count($parameters)) {
         throw new \RuntimeException();
     }
     preg_match('/\\{([a-zA-Z.]+)\\}+/', $resourceDefinition->getPath(), $paramsInPath);
     $path = preg_replace_callback('/\\{([a-zA-Z.]+)\\}+/', function ($matches) use($parameters, $resourceDefinition, $paramsInPath) {
         if (strpos($matches[1], '.')) {
             $accessor = PropertyAccess::createPropertyAccessor();
             $path = explode('.', $matches[1]);
             $root = array_shift($path);
             $result = $accessor->getValue($parameters[$root], implode('.', $path));
             $paramsInPath[] = $root;
         } else {
             if (!array_key_exists($matches[1], $parameters)) {
                 throw new \RuntimeException(sprintf('Parameter `%s` is required in route `%s`, but was not passed', $matches[1], $resourceDefinition->getPath()));
             }
             $result = $parameters[$matches[1]];
             $paramsInPath[] = $matches[1];
         }
         return $result;
     }, $resourceDefinition->getPath());
     $leftovers = array_diff_key($definedParams, array_flip($paramsInPath));
     $leftoverValues = array_intersect_key($parameters, $leftovers);
     if ($leftoverValues) {
         $path .= '?' . http_build_query($leftoverValues);
     }
     return $path;
 }
Beispiel #2
0
 /**
  * Register a resource definition for this endpoint.
  *
  * @throws LogicException when a resource name was already used.
  * @param ResourceDefinitionInterface $resource
  */
 public function addResourceDefinition(ResourceDefinitionInterface $resource)
 {
     if (array_key_exists($resource->getName(), $this->resources)) {
         throw new LogicException(sprintf('Cannot register a resource named `%s` twice.', $resource->getName()));
     }
     $this->resources[$resource->getName()] = $resource;
 }
 public function process(ResponseInterface $response, ResourceDefinitionInterface $resource)
 {
     if (!$response instanceof ApiResponse) {
         throw new InvalidArgumentException(sprintf('Expected an `ApiResponse`, got `%s` instead', get_class($response)));
     }
     $returnType = $resource->getReturnType();
     $config = new Configuration($returnType);
     $hydratorClass = $config->createFactory()->getHydratorClass();
     $hydrator = new $hydratorClass();
     $object = new $returnType();
     if ($resource->getMethod() !== ResourceDefinitionInterface::METHOD_CGET) {
         return $hydrator->hydrate($response->getData(), $object);
     }
     $result = [];
     foreach ($response->getData() as $item) {
         $current = clone $object;
         $result[] = $hydrator->hydrate($item, $current);
     }
     return $result;
 }
 /**
  * @param ResponseInterface           $response
  * @param ResourceDefinitionInterface $resource
  * @return array
  */
 public function process(ResponseInterface $response, ResourceDefinitionInterface $resource)
 {
     if (!$response instanceof ApiResponse) {
         throw new \InvalidArgumentException(sprintf('Expected an ApiResponse, got %s', get_class($response)));
     }
     $dataClass = $resource->getReturnType();
     if (!$dataClass) {
         throw new RuntimeException(sprintf('Return type is not defined for resource `%s` (%s), cannot hydrate', $resource->getName(), $resource->getPath()));
     }
     if (!class_exists($dataClass)) {
         throw new RuntimeException(sprintf('Cannot hydrate an instance of class `%s` as the class does not exist', $dataClass));
     }
     $reflection = new \ReflectionClass($dataClass);
     $this->reflObject = new \ReflectionObject($reflection->newInstance());
     if ($resource->getMethod() === ResourceDefinitionInterface::METHOD_CGET) {
         $result = [];
         foreach ($response->getData() as $row) {
             $result[] = $this->hydrateRow($reflection->newInstance(), $row);
         }
     } else {
         $result = $this->hydrateRow($reflection->newInstance(), $response->getData());
     }
     return $result;
 }