Skip to content

Commit

Permalink
add exception handling for invalid page size
Browse files Browse the repository at this point in the history
  • Loading branch information
imahmood committed Dec 5, 2023
1 parent 00705ce commit b618f7f
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/BaseEloquentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use Salehhashemi\Repository\Contracts\CriteriaInterface;
use Salehhashemi\Repository\Contracts\RepositoryInterface;

Expand Down Expand Up @@ -139,17 +140,31 @@ public function findList(string $key = null, string $value = null): Collection
*/
public function paginate(int $perPage = null): Paginator
{
$this->perPage = $perPage ?? $this->perPage;
$perPage = $this->preparePageSize($perPage);

$this->applyCriteria();
$this->applyRelations();
$this->applyOrder();
$results = $this->getQuery()->paginate($this->perPage);
$results = $this->getQuery()->paginate($perPage);
$this->resetQuery();

return $results;
}

/**
* Prepare the page size for pagination.
*
* @throws \InvalidArgumentException
*/
protected function preparePageSize(int $perPage = null): int
{
if ($perPage <= 0) {
throw new InvalidArgumentException('Invalid page size');
}

return min($perPage, $this->perPage);
}

/**
* Apply conditions to the query based on an array of conditions.
*/
Expand Down

0 comments on commit b618f7f

Please sign in to comment.