Exemplo n.º 1
0
 /**
  * Call the given endpoint, transparently passing the call parameters to it.
  *
  * @param string $name
  * @param array $arguments
  * @throws UndefinedResourceException when the method called is not represented by a resource
  *
  * @return array|object
  */
 public function __call($name, $arguments)
 {
     if (!array_key_exists($name, $this->resources)) {
         throw UndefinedResourceException::create($name, array_keys($this->resources));
     }
     $resource = $this->resources[$name];
     $uri = $this->baseUrl . $this->router->generate($resource, ParameterHydrator::hydrateParameters($resource->getParameters(), $arguments));
     $request = new Request($resource->getMethod(), $uri);
     foreach ($this->listeners[self::LISTENER_BEFORE_REQUEST] as $listener) {
         /** @var callable $listener */
         if (is_callable($listener)) {
             $request = $listener($request);
         }
     }
     $response = $this->httpAdapter->send($request);
     foreach ($this->listeners[self::LISTENER_AFTER_RESPONSE] as $listener) {
         if (is_callable($listener)) {
             $result = $listener($request, $response);
         } elseif ($listener instanceof AfterResponseListenerInterface) {
             $result = $listener->process($request, $response);
         }
         if ($result) {
             $response = $result;
         }
     }
     foreach ($this->listeners[self::LISTENER_AFTER_DATA] as $listener) {
         if (is_callable($listener)) {
             return $listener($response, $resource);
         } elseif ($listener instanceof AfterDataListenerInterface) {
             return $listener->process($response, $resource);
         }
     }
     throw new \RuntimeException('No hydrators :(');
 }
Exemplo n.º 2
0
 /**
  * @test
  * @dataProvider provideUserPropIdUrls
  */
 public function it_populates_multiple_parameters($userId, $propId, $expectedUrl)
 {
     $this->markTestIncomplete();
     $resourceDefn = new ResourceDefinition($name = 'getUserProp', $method = 'GET', $path = '/users/{id}/props/{propId}', $returnType = null, $parameterDefinitions = [new ParameterDefinition('id', 'string', true), new ParameterDefinition('propId', 'string', true)]);
     $router = new Router();
     static::assertEquals($expectedUrl, $router->generate($resourceDefn, ['id' => $userId, 'propId' => $propId]));
 }