Skip to content

Commit

Permalink
Merge 9f230a8 into b3ab206
Browse files Browse the repository at this point in the history
  • Loading branch information
wimski committed Mar 30, 2023
2 parents b3ab206 + 9f230a8 commit 4e9650d
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 189 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ This changelog is initialized in release 1.0.0

## [Unreleased]

## [v3.0.0] - 2023-03-30

### Changed
* Update builder method

## [v2.8.0] - 2023-03-29

### Added
Expand Down Expand Up @@ -67,7 +72,8 @@ This changelog is initialized in release 1.0.0
* Columns argument to ModelRepositoryInterface::all to be compatible with the underlying Eloquent model
* FQN options for contract and repository to ModelRepositoryMakeCommand

[Unreleased]: https://github.com/wimski/laravel-model-repositories/compare/v2.8.0...master
[Unreleased]: https://github.com/wimski/laravel-model-repositories/compare/v3.0.0...master
[v3.0.0]: https://github.com/wimski/laravel-model-repositories/compare/v2.8.0...v3.0.0
[v2.8.0]: https://github.com/wimski/laravel-model-repositories/compare/v2.7.0...v2.8.0
[v2.7.0]: https://github.com/wimski/laravel-model-repositories/compare/v2.6.0...v2.7.0
[v2.6.0]: https://github.com/wimski/laravel-model-repositories/compare/v2.5.0...v2.6.0
Expand Down
41 changes: 21 additions & 20 deletions src/Contracts/Repositories/ModelRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,52 @@
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\LazyCollection;

/**
* @template T of \Illuminate\Database\Eloquent\Model
* @template TModel of Model
*/
interface ModelRepositoryInterface
{
/**
* @return Builder<T>
* @return Builder<TModel>|TModel
*/
public function builder(): Builder;
public function builder(bool $withGlobalScopes = true);

/**
* @param int|string $key
* @param string ...$column
* @return T|null
* @return TModel|null
*/
public function find($key, string ...$column);

/**
* @param int|string $key
* @param string ...$column
* @return T
* @return TModel
* @throws ModelNotFoundException
*/
public function findOrFail($key, string ...$column);

/**
* @param int[]|string[]|Arrayable<int|string, mixed> $keys
* @param string ...$column
* @return Collection<int, T>
* @return Collection<int, TModel>
*/
public function findMany($keys, string ...$column): Collection;

/**
* @param string ...$column
* @return T|null
* @return TModel|null
*/
public function first(string ...$column);

/**
* @param string ...$column
* @return T
* @return TModel
* @throws ModelNotFoundException
*/
public function firstOrFail(string ...$column);
Expand All @@ -62,7 +63,7 @@ public function firstOrFail(string ...$column);
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return T|null
* @return TModel|null
*/
public function firstWhere($column, $operator = null, $value = null, string $boolean = 'and');

Expand All @@ -71,7 +72,7 @@ public function firstWhere($column, $operator = null, $value = null, string $boo
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return T
* @return TModel
* @throws ModelNotFoundException
*/
public function firstWhereOrFail($column, $operator = null, $value = null, string $boolean = 'and');
Expand All @@ -81,66 +82,66 @@ public function firstWhereOrFail($column, $operator = null, $value = null, strin
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return Collection<int, T>
* @return Collection<int, TModel>
*/
public function where($column, $operator = null, $value = null, string $boolean = 'and'): Collection;

/**
* @param string $column
* @param mixed[] $values
* @return Collection<int, T>
* @return Collection<int, TModel>
*/
public function whereIn(string $column, array $values): Collection;

/**
* @param string $column
* @param mixed[] $values
* @return Collection<int, T>
* @return Collection<int, TModel>
*/
public function whereNotIn(string $column, array $values): Collection;

/**
* @return LazyCollection<int, T>
* @return LazyCollection<int, TModel>
*/
public function cursor(): LazyCollection;

/**
*
* @param string ...$column
* @return Collection<int, T>
* @return Collection<int, TModel>
*/
public function all(string ...$column): Collection;

/**
* @param array<string, mixed> $attributes
* @return T
* @return TModel
*/
public function make(array $attributes);

/**
* @param int|string $key
* @param string ...$column
* @return T
* @return TModel
*/
public function findOrMake($key, string ...$column);

/**
* @param array<string, mixed> $attributes
* @param array<string, mixed> $values
* @return T
* @return TModel
*/
public function firstWhereOrMake(array $attributes, array $values = []);

/**
* @param array<string, mixed> $attributes
* @return T
* @return TModel
*/
public function create(array $attributes);

/**
* @param array<string, mixed> $attributes
* @param array<string, mixed> $values
* @return T
* @return TModel
*/
public function firstWhereOrCreate(array $attributes, array $values = []);
}
88 changes: 47 additions & 41 deletions src/Repositories/AbstractModelRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,79 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\LazyCollection;
use Wimski\ModelRepositories\Contracts\Repositories\ModelRepositoryInterface;

/**
* @template T of \Illuminate\Database\Eloquent\Model
* @implements ModelRepositoryInterface<T>
* @template TModel of Model
* @implements ModelRepositoryInterface<TModel>
*/
abstract class AbstractModelRepository implements ModelRepositoryInterface
{
/**
* @var T
* @var TModel
*/
protected $model;
protected Model $model;

public function builder(): Builder
public function builder(bool $withGlobalScopes = true)
{
/** @var Builder<T> $builder */
$builder = $this->model->newQuery();
/** @var Builder<TModel> $builder */
$builder = $this->model->newQueryWithoutScopes();

if ($withGlobalScopes) {
$this->model->registerGlobalScopes($builder);
}

/** @var Builder<TModel>|TModel $builder */
return $builder;
}

public function find($key, string ...$column)
{
/** @var T|null $model */
$model = $this->model->find($key, $this->parseColumns(...$column));
/** @var TModel|null $model */
$model = $this->builder()->find($key, $this->parseColumns(...$column));

return $model;
}

public function findOrFail($key, string ...$column)
{
/** @var T $model */
$model = $this->model->findOrFail($key, $this->parseColumns(...$column));
/** @var TModel $model */
$model = $this->builder()->findOrFail($key, $this->parseColumns(...$column));

return $model;
}

public function findMany($keys, string ...$column): Collection
{
/** @var Collection<int, T> $models */
$models = $this->model->findMany($keys, $this->parseColumns(...$column));
/** @var Collection<int, TModel> $models */
$models = $this->builder()->findMany($keys, $this->parseColumns(...$column));

return $models;
}

public function first(string ...$column)
{
/** @var T|null $model */
$model = $this->model->first($this->parseColumns(...$column));
/** @var TModel|null $model */
$model = $this->builder()->first($this->parseColumns(...$column));

return $model;
}

public function firstOrFail(string ...$column)
{
/** @var T $model */
$model = $this->model->firstOrFail($this->parseColumns(...$column));
/** @var TModel $model */
$model = $this->builder()->firstOrFail($this->parseColumns(...$column));

return $model;
}

public function firstWhere($column, $operator = null, $value = null, string $boolean = 'and')
{
/** @var T|null $model */
$model = $this->model->firstWhere($column, $operator, $value, $boolean);
/** @var TModel|null $model */
$model = $this->builder()->firstWhere($column, $operator, $value, $boolean);

return $model;
}
Expand All @@ -85,91 +91,91 @@ public function firstWhereOrFail($column, $operator = null, $value = null, strin
$this->throwModelNotFoundException();
}

/** @var T $model */
/** @var TModel $model */
return $model;
}

public function where($column, $operator = null, $value = null, string $boolean = 'and'): Collection
{
/** @var Collection<int, T> $models */
$models = $this->model->where($column, $operator, $value, $boolean)->get();
/** @var Collection<int, TModel> $models */
$models = $this->builder()->where($column, $operator, $value, $boolean)->get();

return $models;
}

public function whereIn(string $column, array $values): Collection
{
/** @var Collection<int, T> $models */
$models = $this->model->whereIn($column, $values)->get();
/** @var Collection<int, TModel> $models */
$models = $this->builder()->whereIn($column, $values)->get();

return $models;
}

/**
* @param string $column
* @param mixed[] $values
* @return Collection<int, T>
* @return Collection<int, TModel>
*/
public function whereNotIn(string $column, array $values): Collection
{
/** @var Collection<int, T> $models */
$models = $this->model->whereNotIn($column, $values)->get();
/** @var Collection<int, TModel> $models */
$models = $this->builder()->whereNotIn($column, $values)->get();

return $models;
}

public function cursor(): LazyCollection
{
/** @var LazyCollection<int, T> $models */
$models = $this->model->cursor();
/** @var LazyCollection<int, TModel> $models */
$models = $this->builder()->cursor();

return $models;
}

public function all(string ...$column): Collection
{
/** @var Collection<int, T> $models */
$models = $this->model->all($this->parseColumns(...$column));
/** @var Collection<int, TModel> $models */
$models = $this->builder()->get($this->parseColumns(...$column));

return $models;
}

public function make(array $attributes)
{
/** @var T $model */
$model = $this->model->make($attributes);
/** @var TModel $model */
$model = $this->builder()->make($attributes);

return $model;
}

public function findOrMake($key, string ...$column)
{
/** @var T $model */
$model = $this->model->findOrNew($key, $this->parseColumns(...$column));
/** @var TModel $model */
$model = $this->builder()->findOrNew($key, $this->parseColumns(...$column));

return $model;
}

public function firstWhereOrMake(array $attributes, array $values = [])
{
/** @var T $model */
$model = $this->model->firstOrNew($attributes, $values);
/** @var TModel $model */
$model = $this->builder()->firstOrNew($attributes, $values);

return $model;
}

public function create(array $attributes)
{
/** @var T $model */
$model = $this->model->create($attributes);
/** @var TModel $model */
$model = $this->builder()->create($attributes);

return $model;
}

public function firstWhereOrCreate(array $attributes, array $values = [])
{
/** @var T $model */
$model = $this->model->firstOrCreate($attributes, $values);
/** @var TModel $model */
$model = $this->builder()->firstOrCreate($attributes, $values);

return $model;
}
Expand Down

0 comments on commit 4e9650d

Please sign in to comment.