-
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.
[Common] Create Abstract Repository Class (#17)
- Loading branch information
Showing
7 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/* | ||
* Copyright (c) 2024. Encore Digital Group. | ||
* All Right Reserved. | ||
*/ | ||
|
||
namespace PHPGenesis\Common\Repositories; | ||
|
||
use EncoreDigitalGroup\StdLib\Exceptions\NullExceptions\ArgumentNullException; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Collection; | ||
|
||
abstract class Repository implements IModelRepository | ||
{ | ||
use IsRepository; | ||
|
||
protected string $modelClass; | ||
protected ?Model $model; | ||
|
||
public function __construct(?Model $model = null, ?string $modelClass = null) | ||
{ | ||
if (!is_null($model)) { | ||
$this->model = $model; | ||
$this->map(); | ||
} | ||
|
||
if (is_null($modelClass)) { | ||
throw new ArgumentNullException('modelClass'); | ||
} | ||
|
||
$this->modelClass = $modelClass; | ||
} | ||
|
||
public static function make(?Model $model = null): static | ||
{ | ||
if (is_null($model)) { | ||
/** @phpstan-ignore-next-line */ | ||
return new static(); | ||
} | ||
|
||
/** @phpstan-ignore-next-line */ | ||
return new static($model, static::class); | ||
} | ||
|
||
public static function get(?Model $model = null): static | ||
{ | ||
return static::make($model); | ||
} | ||
|
||
public function firstOrCreate(array $attributes = [], array $values = []): static | ||
{ | ||
$this->model = $this->modelClass::firstOrCreate($attributes, $values); | ||
|
||
return $this->map(); | ||
} | ||
|
||
public function all(): Collection | ||
{ | ||
$allRecords = $this->modelClass::all(); | ||
|
||
$records = []; | ||
|
||
foreach ($allRecords as $record) { | ||
// @phpstan-ignore-next-line | ||
$records[] = new static($record); | ||
} | ||
|
||
return collect($records); | ||
} | ||
|
||
public function find(int|string $id): static | ||
{ | ||
$this->model = $this->modelClass::find($id); | ||
|
||
return $this->map(); | ||
} | ||
|
||
public function save(): void | ||
{ | ||
$this->mapModel(); | ||
$this->model?->save(); | ||
} | ||
|
||
public function updateOrCreate(array $attributes = [], array $values = []): static | ||
{ | ||
$this->model = $this->modelClass::updateOrCreate($attributes, $values); | ||
|
||
return $this->map(); | ||
} | ||
|
||
public function delete(): void | ||
{ | ||
$this->model?->delete(); | ||
} | ||
} |