Changed model to be jsonserialiazable and it's factory into repository
Added model, create and required/optional to repository
This commit is contained in:
@ -1,20 +1,20 @@
|
||||
<?php
|
||||
namespace ProVM\Alias;
|
||||
|
||||
use ProVM\Concept\Model\Factory;
|
||||
use ProVM\Concept\Model\Repository;
|
||||
use ProVM\Concept\Model as ModelInterface;
|
||||
|
||||
abstract class Model implements ModelInterface
|
||||
{
|
||||
protected Factory $factory;
|
||||
public function setFactory(Factory $factory): ModelInterface
|
||||
protected Repository $repository;
|
||||
public function setRepository(Repository $repository): ModelInterface
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user