Get the position within a discussion where a post with a certain number
is. If the post with that number does not exist, the index of the
closest post to it will be returned.
/** * {@inheritdoc} */ protected function extractOffset(ServerRequestInterface $request) { $actor = $request->getAttribute('actor'); $queryParams = $request->getQueryParams(); $sort = $this->extractSort($request); $limit = $this->extractLimit($request); $filter = $this->extractFilter($request); if (($near = array_get($queryParams, 'page.near')) > 1) { if (count($filter) > 1 || !isset($filter['discussion']) || $sort) { throw new InvalidParameterException('You can only use page[near] with filter[discussion] and the default sort order'); } $offset = $this->posts->getIndexForNumber($filter['discussion'], $near, $actor); return max(0, $offset - $limit / 2); } return parent::extractOffset($request); }
/** * @param ServerRequestInterface $request * @param array $where * @return \Illuminate\Database\Eloquent\Collection * @throws InvalidParameterException */ private function getPosts(ServerRequestInterface $request, array $where) { $queryParams = $request->getQueryParams(); $actor = $request->getAttribute('actor'); $sort = $this->extractSort($request); $limit = $this->extractLimit($request); if (($near = array_get($queryParams, 'page.near')) > 1) { if (count($where) > 1 || !isset($where['discussion_id']) || $sort) { throw new InvalidParameterException('You can only use page[near] with ' . 'filter[discussion] and the default sort order'); } $offset = $this->posts->getIndexForNumber($where['discussion_id'], $near, $actor); $offset = max(0, $offset - $limit / 2); } else { $offset = $this->extractOffset($request); } return $this->posts->findWhere($where, $actor, $sort, $limit, $offset); }
/** * @param ServerRequestInterface $request * @param Discussion$discussion * @param int $limit * @return int */ private function getPostsOffset(ServerRequestInterface $request, Discussion $discussion, $limit) { $queryParams = $request->getQueryParams(); $actor = $request->getAttribute('actor'); if (($near = array_get($queryParams, 'page.near')) > 1) { $offset = $this->posts->getIndexForNumber($discussion->id, $near, $actor); $offset = max(0, $offset - $limit / 2); } else { $offset = $this->extractOffset($request); } return $offset; }