118 lines
2.2 KiB
PHP
118 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Traits\RepositoryRelationshipsTraits;
|
|
|
|
class RepositoryBase implements RepositoryBaseInterface
|
|
{
|
|
use RepositoryRelationshipsTraits;
|
|
|
|
protected string $Model;
|
|
|
|
/**
|
|
* @var array|string[]
|
|
*/
|
|
private array $defaultRelations = [];
|
|
|
|
/**
|
|
* PriceListRepository constructor.
|
|
*/
|
|
public function __construct($Model, $defaultRelations = null)
|
|
{
|
|
$this->Model = $Model;
|
|
if ($defaultRelations) {
|
|
$this->actualRelations = $defaultRelations;
|
|
} else {
|
|
$this->actualRelations = $this->defaultRelations;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get's a record by it's ID
|
|
*
|
|
* @param int
|
|
*/
|
|
public function get($id)
|
|
{
|
|
return $this->Model::find($id);
|
|
}
|
|
|
|
/**
|
|
* Get's a record by it's ID
|
|
*
|
|
* @param int
|
|
*/
|
|
public function getWithRelations($id)
|
|
{
|
|
return $this->getQueryWithRelations()->where('id', $id)->get()->first()->toArray();
|
|
}
|
|
|
|
/**
|
|
* Get's all records.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function all()
|
|
{
|
|
return $this->Model::all();
|
|
}
|
|
|
|
/**
|
|
* Get's all records.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function allWithRelations()
|
|
{
|
|
return $this->getQueryWithRelations()->get()->toArray();
|
|
|
|
}
|
|
|
|
/**
|
|
* Deletes a record.
|
|
*
|
|
* @param int
|
|
*/
|
|
public function delete($id): bool
|
|
{
|
|
return $this->Model::destroy($id);
|
|
}
|
|
|
|
/**
|
|
* Updates a post.
|
|
*
|
|
* @param int
|
|
* @param array
|
|
*/
|
|
public function update($id, array $data): bool
|
|
{
|
|
return $this->Model::find($id)->update($data);
|
|
}
|
|
|
|
/**
|
|
* create new Record
|
|
*
|
|
* @param array
|
|
*/
|
|
public function add($data): ?int
|
|
{
|
|
$obj = new $this->Model;
|
|
if (! isset($data['canSee']) || $data['canSee'] == null) {
|
|
$obj->canSee = true;
|
|
}
|
|
if (! isset($data['status']) || ! $data['status']) {
|
|
$obj->status = \App\Enums\DbStatusFieldEnum::draft;
|
|
}
|
|
$obj->fill($data);
|
|
$obj->save();
|
|
|
|
return $obj->id;
|
|
}
|
|
|
|
public function getModelName(): string
|
|
{
|
|
return get_class($this->Model);
|
|
}
|
|
}
|