From 806b8e7b9399cd3d67b2e49695086dced0882d8c Mon Sep 17 00:00:00 2001 From: Aldarien Date: Fri, 9 Sep 2022 15:55:45 -0400 Subject: [PATCH] Changed model to be jsonserialiazable and it's factory into repository Added model, create and required/optional to repository --- src/Alias/Model.php | 34 ++++++++++---- src/Alias/Model/Repository.php | 80 ++++++++++++++++++++++++++++++++ src/Concept/Model.php | 9 ++-- src/Concept/Model/Repository.php | 12 ++++- 4 files changed, 122 insertions(+), 13 deletions(-) diff --git a/src/Alias/Model.php b/src/Alias/Model.php index ea3dd91..0555b70 100644 --- a/src/Alias/Model.php +++ b/src/Alias/Model.php @@ -1,20 +1,20 @@ factory = $factory; + $this->repository = $repository; return $this; } - public function getFactory(): Factory + public function getRepository(): Repository { - return $this->factory; + return $this->repository; } protected int $id; public function setId(int $id): ModelInterface @@ -50,7 +50,7 @@ abstract class Model implements ModelInterface public function save(): void { if ($this->isDirty()) { - $this->getFactory()->get(get_class($this))->save($this); + $this->getRepository()->save($this); } } public function edit(array $data): void @@ -63,6 +63,24 @@ abstract class Model implements ModelInterface } public function delete(): void { - $this->getFactory()->get(get_class($this))->delete($this); + $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(str_replace('get', '', $method->getName())); + $output[$p] = $this->{$method->getName()}(); + } + return $output; } } diff --git a/src/Alias/Model/Repository.php b/src/Alias/Model/Repository.php index 084493a..ed9b008 100644 --- a/src/Alias/Model/Repository.php +++ b/src/Alias/Model/Repository.php @@ -47,6 +47,22 @@ abstract class Repository implements RepositoryInterface { return $this->factory; } + protected string $model; + public function setModel(string $model_class): RepositoryInterface + { + $this->model = $model_class; + return $this; + } + public function getModel(): string + { + return $this->model; + } + public function getNewModel(): Model + { + $class = $this->getModel(); + return (new $class()) + ->setRepository($this); + } protected string $table; public function setTable(string $table): RepositoryInterface { @@ -74,7 +90,66 @@ abstract class Repository implements RepositoryInterface { return $this->columns; } + protected array $required; + public function setRequired(array $columns): RepositoryInterface + { + foreach ($columns as $item) { + $this->addRequired($item); + } + return $this; + } + public function addRequired(string $column): RepositoryInterface + { + $this->required []= $column; + return $this; + } + public function getRequired(): array + { + if (isset($this->optional) and !isset($this->required)) { + return array_diff($this->getColumns(), $this->getOptional()); + } + return $this->required ?? $this->getColumns(); + } + protected array $optional; + public function setOptional(array $columns): RepositoryInterface + { + foreach ($columns as $item) { + $this->addColumn($item); + } + return $this; + } + public function addOptional(string $column): RepositoryInterface + { + $this->optional []= $column; + return $this; + } + public function getOptional(): array + { + if (isset($this->required) and !isset($this->optional)) { + return array_diff($this->getColumns(), $this->getRequired()); + } + return $this->optional ?? []; + } + public function fillData(Model $model, array $data): Model + { + foreach ($this->getRequired() as $column) { + $m = 'set' . ucwords($column); + $model->{$m}($data[$column]); + } + foreach ($this->getOptional() as $column) { + if (isset($data[$column])) { + $m = 'set' . ucwords($column); + $model->{$m}($data[$column]); + } + } + return $model; + } + public function load(array $data): Model + { + return $this->fillData($this->getNewModel() + ->setId($data['id']), $data); + } public function save(Model $model): void { if (!$model->isDirty() and !$model->isNew()) { @@ -90,6 +165,11 @@ abstract class Repository implements RepositoryInterface $query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols); $this->getConnection()->execute($query, $values); } + public function create(array $data): Model + { + return $this->fillData($this->getNewModel() + ->setNew(), $data); + } public function edit(Model $model, array $data): Model { foreach ($this->getColumns() as $col) { diff --git a/src/Concept/Model.php b/src/Concept/Model.php index d58d7ce..1f25e98 100644 --- a/src/Concept/Model.php +++ b/src/Concept/Model.php @@ -1,12 +1,13 @@