65 lines
1.4 KiB
PHP
65 lines
1.4 KiB
PHP
|
<?php
|
||
|
namespace ProVM\Alias;
|
||
|
|
||
|
use ProVM\Concept\Model\Factory;
|
||
|
use ProVM\Concept\Model as ModelInterface;
|
||
|
|
||
|
abstract class Model implements ModelInterface
|
||
|
{
|
||
|
protected Factory $factory;
|
||
|
public function setFactory(Factory $factory): ModelInterface
|
||
|
{
|
||
|
$this->factory = $factory;
|
||
|
return $this;
|
||
|
}
|
||
|
public function getFactory(): Factory
|
||
|
{
|
||
|
return $this->factory;
|
||
|
}
|
||
|
protected int $id;
|
||
|
public function setId(int $id): ModelInterface
|
||
|
{
|
||
|
$this->id = $id;
|
||
|
return $this;
|
||
|
}
|
||
|
public function getId(): int
|
||
|
{
|
||
|
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->getFactory()->get(get_class($this))->save($this);
|
||
|
}
|
||
|
}
|
||
|
public function edit(array $data): void
|
||
|
{
|
||
|
foreach ($data as $key => $val) {
|
||
|
$m = 'set' . ucwords($key);
|
||
|
$this->{$m}($val);
|
||
|
}
|
||
|
$this->isDirty();
|
||
|
}
|
||
|
}
|