Skip to content

Commit

Permalink
[Common] Create Abstract Repository Class (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
onairmarc authored Aug 3, 2024
1 parent 88f472d commit 46e0752
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .idea/PHPGenesis.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions .idea/codeception.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/phpspec.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"encoredigitalgroup/tachyon": "^2.0",
"guzzlehttp/guzzle": "6.x|7.x",
"illuminate/console": "^10|^11",
"illuminate/database": "^10|^11",
"illuminate/filesystem": "^10|^11",
"illuminate/http": "^10|^11",
"illuminate/support": "^10|^11",
Expand Down
1 change: 1 addition & 0 deletions src/Common/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"composer-runtime-api": "^2.0",
"composer/composer": "^2.0",
"encoredigitalgroup/stdlib": "^1.3",
"illuminate/database": "^10|^11",
"illuminate/support": "^10|^11"
},
"scripts": {
Expand Down
95 changes: 95 additions & 0 deletions src/Common/src/Repositories/Repository.php
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();
}
}

0 comments on commit 46e0752

Please sign in to comment.