Author: Hassan Khan (contact@hassankhan.me)
示例#1
0
 /**
  * {@inheritDoc}
  */
 public function paginate(Response $response, $limit = null)
 {
     // If there's nothing to paginate, return response as-is
     if (!$response->hasPages() || $limit === 0) {
         return $response;
     }
     $next = $this->request('GET', $response->nextUrl());
     $merged = $response->merge($next);
     // If ``$limit`` is not set then call itself indefinitely
     if ($limit === null) {
         return $this->paginate($merged);
     }
     // If ``$limit`` is set, call itself while decrementing it each time
     $limit--;
     return $this->paginate($merged, $limit);
 }
示例#2
0
 /**
  * {@inheritDoc}
  */
 public function request($method, $uri, array $parameters = [])
 {
     try {
         $response = $this->guzzle->request($method, $uri, $parameters);
     } catch (ClientException $e) {
         if (!$e->hasResponse()) {
             throw $e;
         }
         throw $this->resolveExceptionClass($e);
     } catch (Exception $e) {
         throw $e;
     }
     return Response::createFromJson(json_decode($response->getBody()->getContents(), true));
 }
示例#3
0
 /**
  * Merges the contents of this response with ``$response`` and returns a new
  * :php:class:`Response` instance.
  *
  * @param Response $response
  *
  * @return Response
  *
  * @throws IncompatibleResponseException
  */
 public function merge(Response $response)
 {
     $meta = $response->getRaw('meta');
     $data = $response->get();
     $pagination = $response->hasPages() ? $response->getRaw('pagination') : [];
     $old = $this->get();
     if ($this->isCollection($old) && $this->isCollection($data)) {
         $old = !$old instanceof Collection ?: $old->toArray();
         $new = !$data instanceof Collection ?: $data->toArray();
         return new Response($meta, array_merge($old, $new), $pagination);
     }
     if ($this->isRecord($old) && $this->isRecord($data)) {
         return new Response($meta, [$old, $data], $pagination);
     }
     throw new IncompatibleResponseException('The response contents cannot be merged');
 }
示例#4
0
 /**
  * {@inheritDoc}
  */
 public function request($method, $uri, array $parameters = [])
 {
     $file = file_get_contents($this->mapRequestToFile($method, $uri, $parameters));
     return Response::createFromJson(json_decode($file, true));
 }