122 lines
2.4 KiB
PHP
122 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Repositories\RepositoryBase;
|
|
use Illuminate\Support\MessageBag;
|
|
|
|
class ServiceBase
|
|
{
|
|
protected $Repository;
|
|
|
|
protected MessageBag $error;
|
|
|
|
public function __construct(?RepositoryBase $Repository = null)
|
|
{
|
|
$this->Repository = $Repository;
|
|
$this->error = new MessageBag;
|
|
}
|
|
|
|
/**
|
|
* Get's a record by it's ID
|
|
*
|
|
* @param int
|
|
*/
|
|
public function get($id): array
|
|
{
|
|
return $this->Repository->get($id)->toArray();
|
|
}
|
|
|
|
/**
|
|
* Get's a record with all relations by it's ID
|
|
*
|
|
* @param int
|
|
*/
|
|
public function getWithAllRelations($id): array
|
|
{
|
|
$this->Repository->setRelation($this->Repository->getAvailableRelations());
|
|
|
|
return $this->getWithRelations($id);
|
|
}
|
|
|
|
/**
|
|
* Get's a record by it's ID
|
|
*
|
|
* @param int
|
|
*/
|
|
public function getWithRelations($id): array
|
|
{
|
|
return $this->Repository->getWithRelations($id);
|
|
}
|
|
|
|
/**
|
|
* Get's all records.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function all()
|
|
{
|
|
return $this->Repository->all();
|
|
}
|
|
|
|
/**
|
|
* Deletes a record.
|
|
*
|
|
* @param int
|
|
*/
|
|
public function delete($id): bool
|
|
{
|
|
return $this->Repository->delete($id);
|
|
}
|
|
|
|
/**
|
|
* Updates a post.
|
|
*
|
|
* @param int
|
|
* @param array
|
|
*/
|
|
public function update($id, array $data): bool
|
|
{
|
|
return $this->Repository->update($id, $data);
|
|
}
|
|
|
|
/**
|
|
* create new Record
|
|
*
|
|
* @param array
|
|
*/
|
|
public function add($data): ?int
|
|
{
|
|
return $this->Repository->add($data);
|
|
}
|
|
|
|
public function hasError()
|
|
{
|
|
return $this->error->isNotEmpty();
|
|
}
|
|
|
|
public function getError(): array
|
|
{
|
|
return $this->error->toArray();
|
|
}
|
|
|
|
/**
|
|
* set relations for next query
|
|
*
|
|
* @param array|string &$relation <p>
|
|
* The relation name or relations array.
|
|
* </p>
|
|
* @param mixed ...$relations [optional] <p>
|
|
* The relations name.
|
|
* </p>
|
|
*/
|
|
public function setRelation($relation, ...$relations)
|
|
{
|
|
if (! is_array($relation)) {
|
|
$relations = func_get_args();
|
|
|
|
}
|
|
$this->Repository->setRelation($relations);
|
|
}
|
|
}
|