Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
Add ability to pass sort options into page and cursor methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene committed Jun 19, 2019
1 parent 231b5c8 commit 6a589ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/Contracts/IRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,35 @@ public function getWhere(array $fieldValues): Collection;
*
* @param PagingInfo $paging Paging information
* @param array $fieldValues Filters collection
* @param SortOptions|null $sortOptions How list of items should be sorted
*
* @return LengthAwarePaginator
*
* @throws BadCriteriaException
* @throws InvalidArgumentException
*/
public function getPage(PagingInfo $paging, array $fieldValues = []): LengthAwarePaginator;
public function getPage(
PagingInfo $paging,
array $fieldValues = [],
?SortOptions $sortOptions = null
): LengthAwarePaginator;

/**
* Get models collection as cursor.
*
* @param CursorRequest $cursor Request with cursor data
* @param array $fieldValues Filters collection
* @param SortOptions|null $sortOptions How list of items should be sorted
*
* @return CursorResult
*
* @throws BadCriteriaException
*/
public function getCursorPage(CursorRequest $cursor, array $fieldValues = []): CursorResult;
public function getCursorPage(
CursorRequest $cursor,
array $fieldValues = [],
?SortOptions $sortOptions = null
): CursorResult;

/**
* Retrieve list of entities that satisfied requested conditions.
Expand Down
26 changes: 20 additions & 6 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,32 @@ public function get(): Collection
}

/** {@inheritdoc} */
public function getPage(PagingInfo $paging, array $fieldValues = []): LengthAwarePaginator
{
$builder = $this->query();
public function getPage(
PagingInfo $paging,
array $fieldValues = [],
?SortOptions $sortOptions = null
): LengthAwarePaginator {
$builder = $this
->query()
->when($sortOptions, function (Builder $query) use ($sortOptions) {
return $query->orderBy($sortOptions->orderBy, $sortOptions->sortOrder);
});
$builder->addNestedWhereQuery($this->getNestedWhereConditions($builder->getQuery(), $fieldValues));

return $builder->paginate($paging->pageSize, ['*'], 'page', $paging->page);
}

/** {@inheritdoc} */
public function getCursorPage(CursorRequest $cursor, array $fieldValues = []): CursorResult
{
$builder = $this->query();
public function getCursorPage(
CursorRequest $cursor,
array $fieldValues = [],
?SortOptions $sortOptions = null
): CursorResult {
$builder = $this
->query()
->when($sortOptions, function (Builder $query) use ($sortOptions) {
return $query->orderBy($sortOptions->orderBy, $sortOptions->sortOrder);
});
$builder->addNestedWhereQuery($this->getNestedWhereConditions($builder->getQuery(), $fieldValues));

return $this->toCursorResult($cursor, $builder);
Expand Down

0 comments on commit 6a589ca

Please sign in to comment.