Compare commits
41 Commits
7a88398379
...
3.0.0
Author | SHA1 | Date | |
---|---|---|---|
67fea3b15f | |||
2a06507876 | |||
faf615e79d | |||
b8409182d7 | |||
e576ef5054 | |||
b19654bc70 | |||
0af6be2c8e | |||
ba3c70a70e | |||
bc006f3e01 | |||
4ccc38ffac | |||
8531658899 | |||
02f8bb0b4f | |||
10e87b71a3 | |||
37c5a79d5a | |||
d12f3f7897 | |||
7fc7de7390 | |||
3bc54fb9d1 | |||
123d46d33c | |||
65c224c636 | |||
6cd26a88ea | |||
022ba575b7 | |||
c913f65b91 | |||
c8a7781c88 | |||
1505539e61 | |||
3087a48c43 | |||
43f545516d | |||
b757ed19b2 | |||
9dc71e4d77 | |||
c6806a1c62 | |||
6fd19a11be | |||
7c727d93e9 | |||
e02b8c4063 | |||
89d1db7a7e | |||
8dc0a27fd9 | |||
ae172b902c | |||
7f81b987c9 | |||
af801e769f | |||
c40baaad3f | |||
a82fdce64b | |||
8126b1f67d | |||
2177cb4652 |
@ -6,6 +6,16 @@ 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
|
||||||
{
|
{
|
||||||
@ -16,4 +26,64 @@ 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,6 +6,15 @@ 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;
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement\Model;
|
namespace ProVM\Implement\Model;
|
||||||
|
|
||||||
use Psr\Container\ContainerInterface;
|
|
||||||
use ProVM\Concept\Model;
|
use ProVM\Concept\Model;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
use ProVM\Concept\Model\Factory as FactoryInterface;
|
use ProVM\Concept\Model\Factory as FactoryInterface;
|
||||||
use ProVM\Concept\Model\Repository;
|
use ProVM\Concept\Model\Repository;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user