Skip to content

Commit

Permalink
feat(AbstractEloquentQuery): support throwable parameter for findOrFail
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna authored and pionl committed Nov 25, 2024
1 parent 4b03e75 commit 37e88ad
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/Database/Queries/AbstractEloquentQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace LaraStrict\Database\Queries;

use Closure;
use Exception;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
Expand All @@ -14,6 +13,7 @@
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Support\Arr;
use LaraStrict\Database\Scopes\OrderScope;
use Throwable;

/**
* You must implement abstract public function execute.
Expand Down Expand Up @@ -45,7 +45,7 @@ public function make(array $attributes = []): Model
abstract protected function getModelClass(): string;

/**
* @param array<int, Scope|null> $scopes
* @param array<int, Scope|null> $scopes
* @param array<OrderScope> $orderBy
*
* @return ChunkedModelQueryResult<TModel>
Expand Down Expand Up @@ -150,13 +150,13 @@ protected function find(string|int $id, array $scopes = []): ?Model
}

/**
* @param array<int, Scope|null> $scopes
* @param Closure(int|string):Exception|null $customException Creates a custom exceptions if model does not exists.
* Receives $id argument.
*
* @template TException of Throwable
* @param array<int, Scope|null> $scopes
* @param Closure(int|string):TException|TException|null $customException Creates a custom exceptions if model does
* not exists. Receives $id argument.
* @return TModel
*/
protected function findOrFail(string|int $key, array $scopes = [], Closure $customException = null): Model
protected function findOrFail(string|int $key, array $scopes = [], Closure|Throwable $customException = null): Model
{
try {
/** @var TModel $model */
Expand All @@ -167,8 +167,9 @@ protected function findOrFail(string|int $key, array $scopes = [], Closure $cust
} catch (ModelNotFoundException $modelNotFoundException) {
if ($customException === null) {
throw $modelNotFoundException;
} elseif ($customException instanceof Throwable) {
throw $customException;
}

throw $customException($key);
}
}
Expand Down Expand Up @@ -213,6 +214,7 @@ protected function getQuery(array $scopes = []): Builder
$class = $this->getModelClass();
/** @var Builder<TModel> $query */
$query = $class::query();

return $this->getScopedQuery($query, $scopes);
}
}

0 comments on commit 37e88ad

Please sign in to comment.