-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdcede9
commit 85d4322
Showing
10 changed files
with
2,003 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Data\Filters\Contracts; | ||
|
||
/** | ||
* This interface is a contract for implementing classes to define | ||
* filtering functionality for the application (models) data. | ||
*/ | ||
interface FiltersData {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Data\Filters\Models; | ||
|
||
use App\Data\Filters\Contracts\FiltersData; | ||
use Spatie\LaravelData\Data; | ||
|
||
final class UserFilters extends Data implements FiltersData | ||
{ | ||
/** | ||
* @param array<int> $ids | ||
*/ | ||
public function __construct( | ||
public ?array $ids = null, | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Repositories\Contracts; | ||
|
||
use App\Data\Filters\Contracts\FiltersData; | ||
use Illuminate\Contracts\Pagination\LengthAwarePaginator; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
/** | ||
* @template TModel of Model | ||
* | ||
* @implements BaseRepositoryInterface<TModel> | ||
*/ | ||
abstract readonly class BaseRepository implements BaseRepositoryInterface | ||
{ | ||
public function __construct( | ||
protected Model $model, | ||
) {} | ||
|
||
public function query(): Builder | ||
{ | ||
return $this->model->newQuery(); | ||
} | ||
|
||
public function find(int|string $id): ?Model | ||
{ | ||
return $this->query()->findOrFail($id); | ||
} | ||
|
||
public function getAll(int $perPage = -1, int $page = 1): Collection|LengthAwarePaginator | ||
{ | ||
return $perPage === -1 | ||
? $this->query()->get() | ||
: $this->query()->paginate(perPage: $perPage, page: $page); | ||
} | ||
|
||
public function getFiltered(FiltersData $filters, array $order = [], int $perPage = -1, int $page = 1): Collection|LengthAwarePaginator | ||
{ | ||
$query = $this->getFilteredQuery($filters, $order); | ||
|
||
return $perPage === -1 | ||
? $query->get() | ||
: $query->paginate(perPage: $perPage, page: $page); | ||
} | ||
|
||
/** | ||
* @param Builder<TModel|Model> $query | ||
* @param array<int|string, string> $order | ||
* @return Builder<TModel|Model> | ||
*/ | ||
protected function addQueryOrder(Builder $query, array $order): Builder | ||
{ | ||
foreach ($order as $column => $direction) { | ||
is_int($column) | ||
? $query->orderBy($direction) | ||
: $query->orderBy($column, $direction); | ||
} | ||
|
||
return $query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.