Made model jsonserializable and added functionality into repository
This commit is contained in:
@ -1,20 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Alias;
|
namespace ProVM\Alias;
|
||||||
|
|
||||||
use ProVM\Concept\Model\Factory;
|
use ProVM\Concept\Model\Repository;
|
||||||
use ProVM\Concept\Model as ModelInterface;
|
use ProVM\Concept\Model as ModelInterface;
|
||||||
|
|
||||||
abstract class Model implements ModelInterface
|
abstract class Model implements ModelInterface
|
||||||
{
|
{
|
||||||
protected Factory $factory;
|
protected Repository $repository;
|
||||||
public function setFactory(Factory $factory): ModelInterface
|
public function setRepository(Repository $repository): ModelInterface
|
||||||
{
|
{
|
||||||
$this->factory = $factory;
|
$this->repository = $repository;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function getFactory(): Factory
|
public function getRepository(): Repository
|
||||||
{
|
{
|
||||||
return $this->factory;
|
return $this->repository;
|
||||||
}
|
}
|
||||||
protected int $id;
|
protected int $id;
|
||||||
public function setId(int $id): ModelInterface
|
public function setId(int $id): ModelInterface
|
||||||
@ -50,7 +50,7 @@ abstract class Model implements ModelInterface
|
|||||||
public function save(): void
|
public function save(): void
|
||||||
{
|
{
|
||||||
if ($this->isDirty()) {
|
if ($this->isDirty()) {
|
||||||
$this->getFactory()->get(get_class($this))->save($this);
|
$this->getRepository()->save($this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public function edit(array $data): void
|
public function edit(array $data): void
|
||||||
@ -63,6 +63,24 @@ abstract class Model implements ModelInterface
|
|||||||
}
|
}
|
||||||
public function delete(): void
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,6 +47,22 @@ abstract class Repository implements RepositoryInterface
|
|||||||
{
|
{
|
||||||
return $this->factory;
|
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;
|
protected string $table;
|
||||||
public function setTable(string $table): RepositoryInterface
|
public function setTable(string $table): RepositoryInterface
|
||||||
{
|
{
|
||||||
@ -74,7 +90,66 @@ abstract class Repository implements RepositoryInterface
|
|||||||
{
|
{
|
||||||
return $this->columns;
|
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
|
public function save(Model $model): void
|
||||||
{
|
{
|
||||||
if (!$model->isDirty() and !$model->isNew()) {
|
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);
|
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols);
|
||||||
$this->getConnection()->execute($query, $values);
|
$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
|
public function edit(Model $model, array $data): Model
|
||||||
{
|
{
|
||||||
foreach ($this->getColumns() as $col) {
|
foreach ($this->getColumns() as $col) {
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Concept;
|
namespace ProVM\Concept;
|
||||||
|
|
||||||
use ProVM\Concept\Model\Factory;
|
use JsonSerializable;
|
||||||
|
use ProVM\Concept\Model\Repository;
|
||||||
|
|
||||||
interface Model
|
interface Model extends JsonSerializable
|
||||||
{
|
{
|
||||||
public function setFactory(Factory $factory): Model;
|
public function setRepository(Repository $factory): Model;
|
||||||
public function getFactory(): Factory;
|
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 setNew(): Model;
|
||||||
|
@ -13,16 +13,26 @@ interface Repository
|
|||||||
public function getQueryBuilder(): QueryBuilder;
|
public function getQueryBuilder(): QueryBuilder;
|
||||||
public function setFactory(Factory $factory): Repository;
|
public function setFactory(Factory $factory): Repository;
|
||||||
public function getFactory(): Factory;
|
public function getFactory(): Factory;
|
||||||
|
public function setModel(string $model_class): Repository;
|
||||||
|
public function getModel(): string;
|
||||||
|
public function getNewModel(): Model;
|
||||||
public function setup(): Repository;
|
public function setup(): Repository;
|
||||||
public function setTable(string $table): Repository;
|
public function setTable(string $table): Repository;
|
||||||
public function getTable(): string;
|
public function getTable(): string;
|
||||||
public function setColumns(array $columns): Repository;
|
public function setColumns(array $columns): Repository;
|
||||||
public function addColumn(string $column): Repository;
|
public function addColumn(string $column): Repository;
|
||||||
public function getColumns(): array;
|
public function getColumns(): array;
|
||||||
|
public function setRequired(array $columns): Repository;
|
||||||
|
public function addRequired(string $column): Repository;
|
||||||
|
public function getRequired(): array;
|
||||||
|
public function setOptional(array $columns): Repository;
|
||||||
|
public function addOptional(string $column): Repository;
|
||||||
|
public function getOptional(): array;
|
||||||
|
public function fillData(Model $model, array $data): Model;
|
||||||
public function load(array $data): Model;
|
public function load(array $data): Model;
|
||||||
public function save(Model $model): void;
|
public function save(Model $model): void;
|
||||||
public function edit(Model $model, array $data): Model;
|
|
||||||
public function create(array $data): Model;
|
public function create(array $data): Model;
|
||||||
|
public function edit(Model $model, array $data): Model;
|
||||||
public function delete(Model $model): void;
|
public function delete(Model $model): void;
|
||||||
public function fetchById(int $id): Model;
|
public function fetchById(int $id): Model;
|
||||||
public function fetchAll(): array;
|
public function fetchAll(): array;
|
||||||
|
Reference in New Issue
Block a user