Exemplo n.º 1
0
 /**
  * @param Payload $payload
  *
  * @throws \League\Route\Http\Exception\BadRequestException
  *
  * @return Request
  */
 public function process($payload)
 {
     /** @var Request $request */
     $request = $payload->getRequest();
     if (!isset($request->getQueryParams()['sort'])) {
         return $payload;
     }
     $this->ensureGetOnlyRequest($request);
     $providedSorts = explode(',', $request->getQueryParams()['sort']);
     if (empty($providedSorts)) {
         return $payload;
     }
     $sorts = [];
     foreach ($providedSorts as $sort) {
         if (strlen($sort) <= 0 || $sort === '-') {
             throw new BadRequestException('Sort parameter cannot be empty.');
         }
         if ($sort[0] === '-') {
             $sorts[substr($sort, 1)] = self::SORT_DESCENDING;
             continue;
         }
         $sorts[$sort] = self::SORT_ASCENDING;
     }
     $request->setSorts($sorts);
     return $payload;
 }
Exemplo n.º 2
0
 /**
  * @param Payload $payload
  *
  * @throws BadRequestException
  *
  * @return Payload
  */
 public function process($payload)
 {
     /** @var Request $request */
     $request = $payload->getRequest();
     $queryParams = $request->getQueryParams();
     $page = isset($queryParams['page']) ? $this->coerceToInteger($queryParams['page'], 'page') : null;
     $perPage = isset($queryParams['per-page']) ? $this->coerceToInteger($queryParams['per-page'], 'per-page') : null;
     if ($page || $perPage) {
         $this->ensureNotPreviouslyPaginated($request);
         $page = $page ?: $this->defaultPage;
         $perPage = $perPage ?: $this->defaultPerPage;
         return $payload->withRequest($request->withPageAndPerPage($page, $perPage));
     }
     return $payload;
 }
Exemplo n.º 3
0
 /**
  * @param Payload $payload
  *
  * @throws BadRequestException
  *
  * @return Payload
  */
 public function process($payload)
 {
     /** @var Request $request */
     $request = $payload->getRequest();
     $queryParams = $request->getQueryParams();
     $offset = isset($queryParams['offset']) ? $this->coerceToInteger($queryParams['offset'], 'offset') : null;
     $limit = isset($queryParams['limit']) ? $this->coerceToInteger($queryParams['limit'], 'limit') : null;
     if ($offset || $limit) {
         $this->ensureNotPreviouslyPaginated($request);
         $this->ensureGetOnlyRequest($request);
         $offset = $offset ?: $this->defaultOffset;
         $limit = $limit ?: $this->defaultLimit;
         return $payload->withRequest($request->withOffsetLimit($offset, $limit));
     }
     return $payload;
 }
Exemplo n.º 4
0
 /**
  * @param Payload $payload
  *
  * @return Payload
  */
 public function process($payload)
 {
     /* @var Request $request */
     $request = $payload->getRequest();
     if (!isset($request->getQueryParams()['fields'])) {
         return $payload;
     }
     $fields = $request->getQueryParams()['fields'];
     if ($fields) {
         $this->ensureGetOnlyRequest($request);
         $fields = explode(',', $fields);
     }
     if (!empty($fields)) {
         return $payload->withRequest($request->withRequestedFields((array) $fields));
     }
     return $payload;
 }
Exemplo n.º 5
0
 /**
  * @param Payload $payload
  *
  * @throws \League\Route\Http\Exception\BadRequestException
  *
  * @return Payload
  */
 public function process($payload)
 {
     /** @var Request $request */
     $request = $payload->getRequest();
     if (!isset($request->getQueryParams()['include'])) {
         return $payload;
     }
     $include = explode(',', $request->getQueryParams()['include']);
     if (!empty($include)) {
         foreach ((array) $include as $k => $resource) {
             if (strpos($resource, '.') !== false) {
                 $resource = explode('.', $resource);
                 $include[$k] = $resource;
             }
         }
         return $payload->withRequest($request->withIncludedResources($include));
     }
     return $payload;
 }
Exemplo n.º 6
0
 /**
  * @param Payload $payload
  *
  * @throws BadRequestException
  *
  * @return Payload
  */
 public function process($payload)
 {
     /* @var Request $request */
     $request = $payload->getRequest();
     $queryParams = $request->getQueryParams();
     $before = isset($queryParams['before']) ? $queryParams['before'] : null;
     $after = isset($queryParams['after']) ? $queryParams['after'] : null;
     if ($before && $after) {
         throw new BadRequestException('You may not specify both before and after');
     }
     if ($before || $after) {
         $this->ensureNotPreviouslyPaginated($request);
         $this->ensureGetOnlyRequest($request);
         if ($before) {
             return $payload->withRequest($request->withBeforeCursor($before));
         }
         if ($after) {
             return $payload->withRequest($request->withAfterCursor($after));
         }
     }
     return $payload;
 }