Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support throwable parameter for findOrFail #104

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading