2018-08-29 06:15:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2024-02-23 18:36:02 +00:00
|
|
|
use App\Repositories\Contracts\RepositoryInterface;
|
2018-08-29 06:15:11 +00:00
|
|
|
use Illuminate\Contracts\Auth\Guard;
|
2024-10-15 06:29:31 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
2018-08-29 06:15:11 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-07-27 15:32:36 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2018-08-29 06:15:11 +00:00
|
|
|
|
2024-10-15 06:29:31 +00:00
|
|
|
/**
|
|
|
|
* @template T of Model
|
|
|
|
* @implements RepositoryInterface<T>
|
|
|
|
*/
|
2022-07-29 06:47:10 +00:00
|
|
|
abstract class Repository implements RepositoryInterface
|
2018-08-29 06:15:11 +00:00
|
|
|
{
|
2024-10-15 06:29:31 +00:00
|
|
|
/** @var class-string<T> $modelClass */
|
|
|
|
protected string $modelClass;
|
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
protected Guard $auth;
|
2018-08-29 06:15:11 +00:00
|
|
|
|
2024-10-15 06:29:31 +00:00
|
|
|
/** @param class-string<T> $modelClass */
|
2020-12-23 11:03:22 +00:00
|
|
|
public function __construct(?string $modelClass = null)
|
2018-08-29 06:15:11 +00:00
|
|
|
{
|
2020-12-23 11:03:22 +00:00
|
|
|
$this->modelClass = $modelClass ?: self::guessModelClass();
|
2019-01-01 11:53:20 +00:00
|
|
|
|
|
|
|
// This instantiation may fail during a console command if e.g. APP_KEY is empty,
|
|
|
|
// rendering the whole installation failing.
|
2024-09-30 22:15:38 +00:00
|
|
|
rescue(fn () => $this->auth = app(Guard::class));
|
2018-08-29 06:15:11 +00:00
|
|
|
}
|
|
|
|
|
2024-10-15 06:29:31 +00:00
|
|
|
/** @return class-string<T> */
|
2020-12-23 11:03:22 +00:00
|
|
|
private static function guessModelClass(): string
|
|
|
|
{
|
|
|
|
return preg_replace('/(.+)\\\\Repositories\\\\(.+)Repository$/m', '$1\Models\\\$2', static::class);
|
|
|
|
}
|
|
|
|
|
2024-10-15 06:29:31 +00:00
|
|
|
/** @inheritDoc */
|
2024-01-01 20:38:41 +00:00
|
|
|
public function getOne($id): Model
|
|
|
|
{
|
2024-10-15 06:29:31 +00:00
|
|
|
return $this->modelClass::query()->findOrFail($id);
|
2024-01-01 20:38:41 +00:00
|
|
|
}
|
|
|
|
|
2024-10-15 06:29:31 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
public function findOne($id): ?Model
|
2024-05-19 05:49:42 +00:00
|
|
|
{
|
2024-10-15 06:29:31 +00:00
|
|
|
return $this->modelClass::query()->find($id);
|
2024-05-19 05:49:42 +00:00
|
|
|
}
|
|
|
|
|
2024-10-15 06:29:31 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
public function getOneBy(array $params): Model
|
2018-08-29 06:15:11 +00:00
|
|
|
{
|
2024-10-15 06:29:31 +00:00
|
|
|
return $this->modelClass::query()->where($params)->firstOrFail();
|
2018-08-29 06:15:11 +00:00
|
|
|
}
|
|
|
|
|
2024-10-15 06:29:31 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
public function findOneBy(array $params): ?Model
|
2024-05-19 05:49:42 +00:00
|
|
|
{
|
2024-10-15 06:29:31 +00:00
|
|
|
return $this->modelClass::query()->where($params)->first();
|
2024-05-19 05:49:42 +00:00
|
|
|
}
|
|
|
|
|
2024-10-15 06:29:31 +00:00
|
|
|
/** @inheritDoc */
|
2024-05-31 05:40:34 +00:00
|
|
|
public function getMany(array $ids, bool $preserveOrder = false): Collection
|
2018-08-29 06:15:11 +00:00
|
|
|
{
|
2024-10-15 06:29:31 +00:00
|
|
|
$models = $this->modelClass::query()->find($ids);
|
2024-01-01 11:40:21 +00:00
|
|
|
|
2024-05-31 05:40:34 +00:00
|
|
|
return $preserveOrder ? $models->orderByArray($ids) : $models;
|
2018-08-29 06:15:11 +00:00
|
|
|
}
|
|
|
|
|
2024-10-15 06:29:31 +00:00
|
|
|
/** @inheritDoc */ // @phpcs:ignore
|
|
|
|
public function getAll(): EloquentCollection
|
2018-08-29 06:15:11 +00:00
|
|
|
{
|
2024-10-15 06:29:31 +00:00
|
|
|
return $this->modelClass::all();
|
2018-08-29 06:15:11 +00:00
|
|
|
}
|
2020-09-06 18:21:39 +00:00
|
|
|
|
2024-10-15 06:29:31 +00:00
|
|
|
/** @inheritDoc */
|
2024-06-04 13:35:00 +00:00
|
|
|
public function findFirstWhere(...$params): ?Model
|
2020-09-06 18:21:39 +00:00
|
|
|
{
|
2024-10-15 06:29:31 +00:00
|
|
|
return $this->modelClass::query()->firstWhere(...$params);
|
2020-12-24 12:41:18 +00:00
|
|
|
}
|
2018-08-29 06:15:11 +00:00
|
|
|
}
|