Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
02f8bb0b4f | |||
d3771d8844 | |||
10e87b71a3 | |||
aff0d4333d | |||
51124218d5 | |||
37c5a79d5a | |||
806b8e7b93 | |||
d12f3f7897 | |||
3811e8224b | |||
9834eb70a4 |
@ -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
|
||||
@ -61,4 +61,29 @@ abstract class Model implements ModelInterface
|
||||
}
|
||||
$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;
|
||||
}
|
||||
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,22 +90,127 @@ 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 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
|
||||
{
|
||||
if (!$model->isDirty() and !$model->isNew()) {
|
||||
return;
|
||||
}
|
||||
$cols = [];
|
||||
$values = [];
|
||||
foreach ($this->getColumns() as $column) {
|
||||
$m = 'get' . ucwords($column);
|
||||
$cols []= '?';
|
||||
$values []= $model->{$m}();
|
||||
if (!$model->isNew()) {
|
||||
$this->update($model);
|
||||
return;
|
||||
}
|
||||
$values = $this->mapArray($model, []);
|
||||
$cols = array_fill(0, count($values), '?');
|
||||
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols);
|
||||
$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
|
||||
{
|
||||
foreach ($this->getColumns() as $col) {
|
||||
@ -100,12 +221,18 @@ abstract class Repository implements RepositoryInterface
|
||||
}
|
||||
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
|
||||
{
|
||||
$query = $this->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where([['id', '?']])
|
||||
->where(['id = ?'])
|
||||
->limit(1);
|
||||
return $this->load($this->getConnection()->execute($query, [$id])->getAsArray()[0]);
|
||||
}
|
||||
|
@ -1,12 +1,13 @@
|
||||
<?php
|
||||
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 getFactory(): Factory;
|
||||
public function setRepository(Repository $factory): Model;
|
||||
public function getRepository(): Repository;
|
||||
public function setId(int $id): Model;
|
||||
public function getId(): int;
|
||||
public function setNew(): Model;
|
||||
@ -15,4 +16,5 @@ interface Model
|
||||
public function isDirty(): bool;
|
||||
public function save(): 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 getContainer(): ContainerInterface;
|
||||
public function setNamespace(string $namespace): Factory;
|
||||
public function getNamespace(): string;
|
||||
public function get(string $repository_name): Repository;
|
||||
public function get(string $repository_class): Repository;
|
||||
}
|
||||
|
@ -13,16 +13,31 @@ interface Repository
|
||||
public function getQueryBuilder(): QueryBuilder;
|
||||
public function setFactory(Factory $factory): Repository;
|
||||
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 setTable(string $table): Repository;
|
||||
public function getTable(): string;
|
||||
public function setColumns(array $columns): Repository;
|
||||
public function addColumn(string $column): Repository;
|
||||
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 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 edit(Model $model, array $data): Model;
|
||||
public function delete(Model $model): void;
|
||||
public function fetchById(int $id): Model;
|
||||
public function fetchAll(): array;
|
||||
}
|
||||
|
@ -22,25 +22,8 @@ class Factory implements FactoryInterface
|
||||
{
|
||||
return $this->container;
|
||||
}
|
||||
protected string $namespace;
|
||||
public function setNamespace(string $namespace): FactoryInterface
|
||||
public function get(string $repository_class): Repository
|
||||
{
|
||||
$this->namespace = $namespace;
|
||||
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));
|
||||
return $this->getContainer()->get($repository_class);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user