chore: better Repository docs (#1852)

This commit is contained in:
Phan An 2024-10-15 13:29:31 +07:00 committed by GitHub
parent 32b848b4b5
commit d041a0386f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 47 additions and 28 deletions

View file

@ -2,18 +2,31 @@
namespace App\Repositories\Contracts; namespace App\Repositories\Contracts;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
/** @template T of Model */
interface RepositoryInterface interface RepositoryInterface
{ {
/** @return T */
public function getOne($id): Model; public function getOne($id): Model;
/** @return T */
public function getOneBy(array $params): Model;
/** @return T|null */
public function findOne($id): ?Model; public function findOne($id): ?Model;
/** @return Collection<Model> */ /** @return T|null */
public function findOneBy(array $params): ?Model;
/** @return Collection<array-key, T> */
public function getMany(array $ids, bool $preserveOrder = false): Collection; public function getMany(array $ids, bool $preserveOrder = false): Collection;
/** @return Collection<Model> */ /** @return Collection<int, T> */
public function getAll(): Collection; public function getAll(): EloquentCollection;
/** @return T|null */
public function findFirstWhere(...$params): ?Model;
} }

View file

@ -4,72 +4,78 @@ namespace App\Repositories;
use App\Repositories\Contracts\RepositoryInterface; use App\Repositories\Contracts\RepositoryInterface;
use Illuminate\Contracts\Auth\Guard; use Illuminate\Contracts\Auth\Guard;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
/** @template T of Model */ /**
* @template T of Model
* @implements RepositoryInterface<T>
*/
abstract class Repository implements RepositoryInterface abstract class Repository implements RepositoryInterface
{ {
private string $modelClass; /** @var class-string<T> $modelClass */
protected Guard $auth; protected string $modelClass;
public Model $model;
protected Guard $auth;
/** @param class-string<T> $modelClass */
public function __construct(?string $modelClass = null) public function __construct(?string $modelClass = null)
{ {
$this->modelClass = $modelClass ?: self::guessModelClass(); $this->modelClass = $modelClass ?: self::guessModelClass();
$this->model = app($this->modelClass);
// This instantiation may fail during a console command if e.g. APP_KEY is empty, // This instantiation may fail during a console command if e.g. APP_KEY is empty,
// rendering the whole installation failing. // rendering the whole installation failing.
rescue(fn () => $this->auth = app(Guard::class)); rescue(fn () => $this->auth = app(Guard::class));
} }
/** @return class-string<T> */
private static function guessModelClass(): string private static function guessModelClass(): string
{ {
return preg_replace('/(.+)\\\\Repositories\\\\(.+)Repository$/m', '$1\Models\\\$2', static::class); return preg_replace('/(.+)\\\\Repositories\\\\(.+)Repository$/m', '$1\Models\\\$2', static::class);
} }
/** @return T */ /** @inheritDoc */
public function getOne($id): Model public function getOne($id): Model
{ {
return $this->model::query()->findOrFail($id); return $this->modelClass::query()->findOrFail($id);
} }
/** @return T|null */ /** @inheritDoc */
public function findOneBy(array $params): ?Model
{
return $this->model::query()->where($params)->first();
}
/** @return T|null */
public function findOne($id): ?Model public function findOne($id): ?Model
{ {
return $this->model::query()->find($id); return $this->modelClass::query()->find($id);
} }
/** @return T */ /** @inheritDoc */
public function getOneBy(array $params): Model public function getOneBy(array $params): Model
{ {
return $this->model::query()->where($params)->firstOrFail(); return $this->modelClass::query()->where($params)->firstOrFail();
} }
/** @return array<array-key, T>|Collection<array-key, T> */ /** @inheritDoc */
public function findOneBy(array $params): ?Model
{
return $this->modelClass::query()->where($params)->first();
}
/** @inheritDoc */
public function getMany(array $ids, bool $preserveOrder = false): Collection public function getMany(array $ids, bool $preserveOrder = false): Collection
{ {
$models = $this->model::query()->find($ids); $models = $this->modelClass::query()->find($ids);
return $preserveOrder ? $models->orderByArray($ids) : $models; return $preserveOrder ? $models->orderByArray($ids) : $models;
} }
/** @return array<array-key, T>|Collection<array-key, T> */ /** @inheritDoc */ // @phpcs:ignore
public function getAll(): Collection public function getAll(): EloquentCollection
{ {
return $this->model::all(); return $this->modelClass::all();
} }
/** @return T|null */ /** @inheritDoc */
public function findFirstWhere(...$params): ?Model public function findFirstWhere(...$params): ?Model
{ {
return $this->model::query()->firstWhere(...$params); return $this->modelClass::query()->firstWhere(...$params);
} }
} }

View file

@ -10,7 +10,7 @@ class SettingRepository extends Repository
/** @return array<mixed> */ /** @return array<mixed> */
public function getAllAsKeyValueArray(): array public function getAllAsKeyValueArray(): array
{ {
return $this->model->pluck('value', 'key')->toArray(); return $this->modelClass::query()->pluck('value', 'key')->toArray();
} }
public function getByKey(string $key): mixed public function getByKey(string $key): mixed