Simplified Model
This commit is contained in:
@ -6,16 +6,6 @@ use ProVM\Concept\Model as ModelInterface;
|
|||||||
|
|
||||||
abstract class Model implements ModelInterface
|
abstract class Model implements ModelInterface
|
||||||
{
|
{
|
||||||
protected Repository $repository;
|
|
||||||
public function setRepository(Repository $repository): ModelInterface
|
|
||||||
{
|
|
||||||
$this->repository = $repository;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function getRepository(): Repository
|
|
||||||
{
|
|
||||||
return $this->repository;
|
|
||||||
}
|
|
||||||
protected int $id;
|
protected int $id;
|
||||||
public function setId(int $id): ModelInterface
|
public function setId(int $id): ModelInterface
|
||||||
{
|
{
|
||||||
@ -26,64 +16,4 @@ abstract class Model implements ModelInterface
|
|||||||
{
|
{
|
||||||
return $this->id;
|
return $this->id;
|
||||||
}
|
}
|
||||||
protected bool $new;
|
|
||||||
public function setNew(): ModelInterface
|
|
||||||
{
|
|
||||||
$this->new = true;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function isNew(): bool
|
|
||||||
{
|
|
||||||
return $this->new ?? false;
|
|
||||||
}
|
|
||||||
protected bool $dirty;
|
|
||||||
public function setDirty(): ModelInterface
|
|
||||||
{
|
|
||||||
$this->dirty = true;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function isDirty(): bool
|
|
||||||
{
|
|
||||||
return $this->dirty ?? $this->isNew();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function save(): void
|
|
||||||
{
|
|
||||||
if ($this->isDirty()) {
|
|
||||||
$this->getRepository()->save($this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public function edit(array $data): void
|
|
||||||
{
|
|
||||||
foreach ($data as $key => $val) {
|
|
||||||
$m = 'set' . ucwords($key);
|
|
||||||
$this->{$m}($val);
|
|
||||||
}
|
|
||||||
$this->isDirty();
|
|
||||||
}
|
|
||||||
public function delete(): void
|
|
||||||
{
|
|
||||||
$this->getRepository()->delete($this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function jsonSerialize(): mixed
|
|
||||||
{
|
|
||||||
$obj = new \ReflectionObject($this);
|
|
||||||
$methods = $obj->getMethods();
|
|
||||||
$output = [];
|
|
||||||
foreach ($methods as $method) {
|
|
||||||
if (!str_contains($method->getName(), 'get')) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if ($method->getName() === 'getRepository') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$p = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', str_replace('get', '', $method->getName())));
|
|
||||||
if (!isset($this->{$p})) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$output[$p] = $this->{$method->getName()}();
|
|
||||||
}
|
|
||||||
return $output;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -6,15 +6,6 @@ use ProVM\Concept\Model\Repository;
|
|||||||
|
|
||||||
interface Model extends JsonSerializable
|
interface Model extends JsonSerializable
|
||||||
{
|
{
|
||||||
public function setRepository(Repository $factory): Model;
|
|
||||||
public function getRepository(): Repository;
|
|
||||||
public function setId(int $id): Model;
|
public function setId(int $id): Model;
|
||||||
public function getId(): int;
|
public function getId(): int;
|
||||||
public function setNew(): Model;
|
|
||||||
public function isNew(): bool;
|
|
||||||
public function setDirty(): Model;
|
|
||||||
public function isDirty(): bool;
|
|
||||||
public function save(): void;
|
|
||||||
public function edit(array $data): void;
|
|
||||||
public function delete(): void;
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user