From 7848d973a6e1056cd05aedc254d3ce278c57ebd3 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Thu, 22 Dec 2022 19:48:05 -0300 Subject: [PATCH 1/2] Reorder --- src/Implement/Model/Factory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Implement/Model/Factory.php b/src/Implement/Model/Factory.php index 8c8d33d..0e4abab 100644 --- a/src/Implement/Model/Factory.php +++ b/src/Implement/Model/Factory.php @@ -1,8 +1,8 @@ Date: Thu, 22 Dec 2022 19:48:23 -0300 Subject: [PATCH 2/2] Simplified Model --- src/Alias/Model.php | 70 ------------------------------------------- src/Concept/Model.php | 9 ------ 2 files changed, 79 deletions(-) diff --git a/src/Alias/Model.php b/src/Alias/Model.php index 39230ad..328875f 100644 --- a/src/Alias/Model.php +++ b/src/Alias/Model.php @@ -6,16 +6,6 @@ use ProVM\Concept\Model as 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; public function setId(int $id): ModelInterface { @@ -26,64 +16,4 @@ abstract class Model implements ModelInterface { 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('/(?getName()))); - if (!isset($this->{$p})) { - continue; - } - $output[$p] = $this->{$method->getName()}(); - } - return $output; - } } diff --git a/src/Concept/Model.php b/src/Concept/Model.php index 1f25e98..408c694 100644 --- a/src/Concept/Model.php +++ b/src/Concept/Model.php @@ -6,15 +6,6 @@ use ProVM\Concept\Model\Repository; interface Model extends JsonSerializable { - public function setRepository(Repository $factory): Model; - public function getRepository(): Repository; public function setId(int $id): Model; 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; }