Skip to content

Commit

Permalink
Updated $model property type to allow for Builder contract
Browse files Browse the repository at this point in the history
  • Loading branch information
sinemacula-ben committed Apr 24, 2024
1 parent 4a924cb commit 8f03f63
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 117 deletions.
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,12 @@
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"extra": {
"laravel": {
"providers": [
"SineMacula\\Repositories\\RepositoryServiceProvider"
]
}
}
}
233 changes: 117 additions & 116 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SineMacula\Repositories;

use Closure;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
Expand All @@ -19,8 +20,8 @@
*/
abstract class Repository implements RepositoryInterface, RepositoryCriteriaInterface
{
/** @var \Illuminate\Database\Eloquent\Model The model instance */
protected Model $model;
/** @var \Illuminate\Database\Eloquent\Model|\Illuminate\Contracts\Database\Eloquent\Builder The model instance */
protected Model|Builder $model;

/** @var \Illuminate\Support\Collection The persistent criteria */
protected Collection $persistentCriteria;
Expand Down Expand Up @@ -54,11 +55,58 @@ public function __construct(
$this->boot();
}

/**
* Reset the scopes.
*
* @return static
*/
public function resetScopes(): static
{
$this->scopes = [];

return $this;
}

/**
* Create a new model instance.
*
* @return \Illuminate\Database\Eloquent\Model
*
* @throws \SineMacula\Repositories\Exceptions\RepositoryException|\Illuminate\Contracts\Container\BindingResolutionException
*/
public function makeModel(): Model
{
$model = $this->app->make($this->model());

if (!$model instanceof Model) {
throw new RepositoryException("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model");
}

return $this->model = $model;
}

/**
* Boot the repository instance.
*
* This is a useful method for setting immediate properties when extending
* the base repository class.
*
* @return void
*/
protected function boot(): void {}

/**
* Return the model class
*
* @return class-string
*/
abstract public function model(): string;

/**
* Trigger a static method call on the repository.
*
* @param string $method
* @param array $arguments
* @param array $arguments
* @return mixed
*/
public static function __callStatic(string $method, array $arguments): mixed
Expand All @@ -70,7 +118,7 @@ public static function __callStatic(string $method, array $arguments): mixed
* Forward method calls to the model
*
* @param string $method
* @param array $arguments
* @param array $arguments
* @return mixed
*/
public function __call(string $method, array $arguments): mixed
Expand All @@ -84,41 +132,80 @@ public function __call(string $method, array $arguments): mixed
}

/**
* Reset the scopes.
* Apply the criteria to the current query.
*
* @return static
*/
public function resetScopes(): static
protected function applyCriteria(): static
{
$this->scopes = [];
if ($this->skipCriteria) {

$this->skipCriteria = false;
$this->resetTransientCriteria();

return $this;
}

if ($this->transientCriteria->isNotEmpty()) {

foreach ($this->transientCriteria as $criterion) {
$this->model = $criterion->apply($this->model);
}

$this->resetTransientCriteria();
}

if (!$this->disableCriteria && $this->persistentCriteria->isNotEmpty()) {
foreach ($this->persistentCriteria as $criterion) {
$this->model = $criterion->apply($this->model);
}
}

return $this;
}

/**
* Create a new model instance.
*
* @return \Illuminate\Database\Eloquent\Model
* Apply all accumulated scopes to the model.
*
* @throws \SineMacula\Repositories\Exceptions\RepositoryException|\Illuminate\Contracts\Container\BindingResolutionException
* @return static
*/
public function makeModel(): Model
protected function applyScopes(): static
{
$model = $this->app->make($this->model());

if (!$model instanceof Model) {
throw new RepositoryException("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model");
foreach ($this->scopes as $scope) {
if (is_callable($scope)) {
$this->model = $scope($this->model);
}
}

return $this->model = $model;
return $this;
}

/**
* Return the model class
* Reset the various transient values and return the result.
*
* @return class-string
* @param mixed $result
* @return mixed
*/
abstract public function model(): string;
protected function resetAndReturn(mixed $result): mixed
{
$this->resetTransientCriteria();
$this->resetScopes();
$this->resetModel();

return $result;
}

/**
* Clears all transient criteria.
*
* @return static
*/
private function resetTransientCriteria(): static
{
$this->transientCriteria = collect();

return $this;
}

/**
* Reset the model instance.
Expand Down Expand Up @@ -302,116 +389,30 @@ public function resetCriteria(): static
}

/**
* Add a new scope.
*
* @param \Closure $scope
* @return static
*/
public function addScope(Closure $scope): static
{
$this->scopes[] = $scope;

return $this;
}

/**
* Boot the repository instance.
*
* This is a useful method for setting immediate properties when extending
* the base repository class.
*
* @return void
*/
protected function boot(): void {}

/**
* Apply the criteria to the current query.
*
* @return static
*/
protected function applyCriteria(): static
{
if ($this->skipCriteria) {

$this->skipCriteria = false;
$this->resetTransientCriteria();

return $this;
}

if ($this->transientCriteria->isNotEmpty()) {

foreach ($this->transientCriteria as $criterion) {
$this->model = $criterion->apply($this->model);
}

$this->resetTransientCriteria();
}

if (!$this->disableCriteria && $this->persistentCriteria->isNotEmpty()) {
foreach ($this->persistentCriteria as $criterion) {
$this->model = $criterion->apply($this->model);
}
}

return $this;
}

/**
* Apply all accumulated scopes to the model.
*
* @return static
*/
protected function applyScopes(): static
{
foreach ($this->scopes as $scope) {
if (is_callable($scope)) {
$this->model = $scope($this->model);
}
}

return $this;
}

/**
* Reset the various transient values and return the result.
* Sanitize the given array of criteria to ensure they are valid criteria
* instances.
*
* @param mixed $result
* @return mixed
* @param array $criteria
* @return array
*/
protected function resetAndReturn(mixed $result): mixed
private function sanitizeCriteria(array $criteria): array
{
$this->resetTransientCriteria();
$this->resetScopes();
$this->resetModel();

return $result;
return array_filter($criteria, fn ($criterion) => $criterion instanceof CriteriaInterface);
}

/**
* Clears all transient criteria.
* Add a new scope.
*
* @param \Closure $scope
* @return static
*/
private function resetTransientCriteria(): static
public function addScope(Closure $scope): static
{
$this->transientCriteria = collect();
$this->scopes[] = $scope;

return $this;
}

/**
* Sanitize the given array of criteria to ensure they are valid criteria
* instances.
*
* @param array $criteria
* @return array
*/
private function sanitizeCriteria(array $criteria): array
{
return array_filter($criteria, fn ($criterion) => $criterion instanceof CriteriaInterface);
}

/**
* Clears all persistent criteria.
*
Expand Down

0 comments on commit 8f03f63

Please sign in to comment.