Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
10e87b71a3 | |||
aff0d4333d | |||
51124218d5 | |||
37c5a79d5a | |||
806b8e7b93 | |||
d12f3f7897 | |||
3811e8224b | |||
9834eb70a4 |
@ -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
|
||||||
@ -61,4 +61,29 @@ abstract class Model implements ModelInterface
|
|||||||
}
|
}
|
||||||
$this->isDirty();
|
$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(str_replace('get', '', $method->getName()));
|
||||||
|
if (!isset($this->{$p})) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$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,22 +90,127 @@ 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 getMethod(string $column, bool $get = true): string
|
||||||
|
{
|
||||||
|
$m = str_replace(' ', '', ucwords(str_replace('_', ' ', $column)));
|
||||||
|
if ($get) {
|
||||||
|
return "get{$m}";
|
||||||
|
}
|
||||||
|
return "set{$m}";
|
||||||
|
}
|
||||||
|
public function getProperty(string $method): string
|
||||||
|
{
|
||||||
|
$parts = preg_split('/(?=[A-Z])/', $method);
|
||||||
|
if (in_array(strtolower($parts[0]), ['get', 'set'])) {
|
||||||
|
array_shift($parts);
|
||||||
|
}
|
||||||
|
return strtolower(implode('_', $parts));
|
||||||
|
}
|
||||||
|
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 mapArray(Model $model, array $data): array
|
||||||
|
{
|
||||||
|
foreach ($this->getColumns() as $column) {
|
||||||
|
$m = $this->getMethod($column);
|
||||||
|
$val = $model->{$m}();
|
||||||
|
if (isset($data[$column])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$data[$column] = $val;
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
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()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$cols = [];
|
if (!$model->isNew()) {
|
||||||
$values = [];
|
$this->update($model);
|
||||||
foreach ($this->getColumns() as $column) {
|
return;
|
||||||
$m = 'get' . ucwords($column);
|
|
||||||
$cols []= '?';
|
|
||||||
$values []= $model->{$m}();
|
|
||||||
}
|
}
|
||||||
|
$values = $this->mapArray($model, []);
|
||||||
|
$cols = array_fill(0, count($values), '?');
|
||||||
$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 update(Model $model): void
|
||||||
|
{
|
||||||
|
if (!$model->isDirty() and !$model->isNew()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$values = $this->mapArray($model, []);
|
||||||
|
$cols = array_map(function($column) {
|
||||||
|
return "{$column} = ?";
|
||||||
|
}, $values);
|
||||||
|
$values = array_values($values);
|
||||||
|
$values []= $model->getId();
|
||||||
|
$query = $this->getQueryBuilder()->update($this->getTable())->set($cols)->where(['id = ?']);
|
||||||
|
$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) {
|
||||||
@ -100,6 +221,12 @@ abstract class Repository implements RepositoryInterface
|
|||||||
}
|
}
|
||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
|
public function delete(Model $model): void
|
||||||
|
{
|
||||||
|
$query = $this->getQueryBuilder()->delete($this->getTable())->where(['id = ?']);
|
||||||
|
$this->getConnection()->execute($query, [$model->getId()]);
|
||||||
|
}
|
||||||
|
|
||||||
public function fetchById(int $id): Model
|
public function fetchById(int $id): Model
|
||||||
{
|
{
|
||||||
$query = $this->getQueryBuilder()
|
$query = $this->getQueryBuilder()
|
||||||
|
@ -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;
|
||||||
@ -15,4 +16,5 @@ interface Model
|
|||||||
public function isDirty(): bool;
|
public function isDirty(): bool;
|
||||||
public function save(): void;
|
public function save(): void;
|
||||||
public function edit(array $data): void;
|
public function edit(array $data): void;
|
||||||
|
public function delete(): void;
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,5 @@ interface Factory
|
|||||||
{
|
{
|
||||||
public function setContainer(ContainerInterface $container): Factory;
|
public function setContainer(ContainerInterface $container): Factory;
|
||||||
public function getContainer(): ContainerInterface;
|
public function getContainer(): ContainerInterface;
|
||||||
public function setNamespace(string $namespace): Factory;
|
public function get(string $repository_class): Repository;
|
||||||
public function getNamespace(): string;
|
|
||||||
public function get(string $repository_name): Repository;
|
|
||||||
}
|
}
|
||||||
|
@ -13,16 +13,31 @@ 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 getMethod(string $column, bool $get = true): string;
|
||||||
|
public function getProperty(string $method): string;
|
||||||
|
public function fillData(Model $model, array $data): Model;
|
||||||
|
public function mapArray(Model $model, array $data): array;
|
||||||
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 update(Model $model): void;
|
||||||
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 fetchById(int $id): Model;
|
public function fetchById(int $id): Model;
|
||||||
public function fetchAll(): array;
|
public function fetchAll(): array;
|
||||||
}
|
}
|
||||||
|
@ -22,25 +22,8 @@ class Factory implements FactoryInterface
|
|||||||
{
|
{
|
||||||
return $this->container;
|
return $this->container;
|
||||||
}
|
}
|
||||||
protected string $namespace;
|
public function get(string $repository_class): Repository
|
||||||
public function setNamespace(string $namespace): FactoryInterface
|
|
||||||
{
|
{
|
||||||
$this->namespace = $namespace;
|
return $this->getContainer()->get($repository_class);
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function getNamespace(): string
|
|
||||||
{
|
|
||||||
return $this->namespace;
|
|
||||||
}
|
|
||||||
protected function buildRepository(string $repository_name): string
|
|
||||||
{
|
|
||||||
return implode("\\", [
|
|
||||||
$this->getNamespace(),
|
|
||||||
$repository_name
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
public function get(string $repository_name): Repository
|
|
||||||
{
|
|
||||||
return $this->getContainer()->get($this->buildRepository($repository_name));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user