Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
8c481eb512 | |||
7a88398379 | |||
7848d973a6 | |||
67fea3b15f | |||
d2c5c1180b | |||
2a06507876 | |||
e4cec2e2f2 | |||
faf615e79d | |||
8a03d9e675 | |||
b8409182d7 | |||
100df73916 | |||
e576ef5054 | |||
5c1f424c76 | |||
b19654bc70 | |||
fe67519682 | |||
0af6be2c8e | |||
0a9bed1735 | |||
ba3c70a70e | |||
422d9a6dbd | |||
bc006f3e01 | |||
23c56f9511 |
@ -6,16 +6,6 @@ use ProVM\Concept\Model as ModelInterface;
|
||||
|
||||
abstract class Model implements ModelInterface
|
||||
{
|
||||
protected Repository $repository;
|
||||
public function setRepository(Repository $repository): ModelInterface
|
||||
{
|
||||
$this->repository = $repository;
|
||||
return $this;
|
||||
}
|
||||
public function getRepository(): Repository
|
||||
{
|
||||
return $this->repository;
|
||||
}
|
||||
protected int $id;
|
||||
public function setId(int $id): ModelInterface
|
||||
{
|
||||
@ -26,64 +16,4 @@ abstract class Model implements ModelInterface
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
protected bool $new;
|
||||
public function setNew(): ModelInterface
|
||||
{
|
||||
$this->new = true;
|
||||
return $this;
|
||||
}
|
||||
public function isNew(): bool
|
||||
{
|
||||
return $this->new ?? false;
|
||||
}
|
||||
protected bool $dirty;
|
||||
public function setDirty(): ModelInterface
|
||||
{
|
||||
$this->dirty = true;
|
||||
return $this;
|
||||
}
|
||||
public function isDirty(): bool
|
||||
{
|
||||
return $this->dirty ?? $this->isNew();
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
if ($this->isDirty()) {
|
||||
$this->getRepository()->save($this);
|
||||
}
|
||||
}
|
||||
public function edit(array $data): void
|
||||
{
|
||||
foreach ($data as $key => $val) {
|
||||
$m = 'set' . ucwords($key);
|
||||
$this->{$m}($val);
|
||||
}
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
<?php
|
||||
namespace ProVM\Alias\Model;
|
||||
|
||||
use PDOException;
|
||||
use ProVM\Concept\Database\Connection;
|
||||
use ProVM\Concept\Database\QueryBuilder;
|
||||
use ProVM\Concept\Model;
|
||||
use ProVM\Concept\Model\Factory;
|
||||
use ProVM\Concept\Model\Repository as RepositoryInterface;
|
||||
use ProVM\Exception\BlankResult;
|
||||
|
||||
abstract class Repository implements RepositoryInterface
|
||||
{
|
||||
@ -18,226 +20,164 @@ abstract class Repository implements RepositoryInterface
|
||||
}
|
||||
|
||||
protected Connection $connection;
|
||||
public function setConnection(Connection $connection): Repository
|
||||
{
|
||||
$this->connection = $connection;
|
||||
return $this;
|
||||
}
|
||||
protected QueryBuilder $builder;
|
||||
protected Factory $factory;
|
||||
protected string $model;
|
||||
protected string $table;
|
||||
|
||||
public function getConnection(): Connection
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
protected QueryBuilder $builder;
|
||||
public function setQueryBuilder(QueryBuilder $builder): RepositoryInterface
|
||||
{
|
||||
$this->builder = $builder;
|
||||
return $this;
|
||||
}
|
||||
public function getQueryBuilder(): QueryBuilder
|
||||
{
|
||||
return $this->builder;
|
||||
}
|
||||
protected Factory $factory;
|
||||
public function setFactory(Factory $factory): RepositoryInterface
|
||||
{
|
||||
$this->factory = $factory;
|
||||
return $this;
|
||||
}
|
||||
public function getFactory(): 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
|
||||
public function getTable(): string
|
||||
{
|
||||
$class = $this->getModel();
|
||||
return (new $class())
|
||||
->setRepository($this);
|
||||
return $this->table;
|
||||
}
|
||||
|
||||
public function setConnection(Connection $connection): Repository
|
||||
{
|
||||
$this->connection = $connection;
|
||||
return $this;
|
||||
}
|
||||
public function setQueryBuilder(QueryBuilder $builder): RepositoryInterface
|
||||
{
|
||||
$this->builder = $builder;
|
||||
return $this;
|
||||
}
|
||||
public function setFactory(Factory $factory): RepositoryInterface
|
||||
{
|
||||
$this->factory = $factory;
|
||||
return $this;
|
||||
}
|
||||
public function setModel(string $model_class): RepositoryInterface
|
||||
{
|
||||
$this->model = $model_class;
|
||||
return $this;
|
||||
}
|
||||
protected string $table;
|
||||
public function setTable(string $table): RepositoryInterface
|
||||
{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
public function getTable(): string
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
protected array $columns;
|
||||
public function setColumns(array $columns): RepositoryInterface
|
||||
{
|
||||
foreach ($columns as $column) {
|
||||
$this->addColumn($column);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function addColumn(string $column): RepositoryInterface
|
||||
{
|
||||
$this->columns []= $column;
|
||||
return $this;
|
||||
}
|
||||
public function getColumns(): array
|
||||
{
|
||||
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
|
||||
public function save(Model &$model): void
|
||||
{
|
||||
$m = str_replace(' ', '', ucwords(str_replace('_', ' ', $column)));
|
||||
if ($get) {
|
||||
return "get{$m}";
|
||||
try {
|
||||
$old = $this->defaultFind($model);
|
||||
$this->update($model, $old);
|
||||
} catch (BlankResult $e) {
|
||||
$this->insert($model);
|
||||
$model->setId($this->getConnection()->getPDO()->lastInsertId());
|
||||
}
|
||||
return "set{$m}";
|
||||
}
|
||||
public function getProperty(string $method): string
|
||||
public function update(Model $model, Model $old): void
|
||||
{
|
||||
$parts = preg_split('/(?=[A-Z])/', $method);
|
||||
if (in_array(strtolower($parts[0]), ['get', 'set'])) {
|
||||
array_shift($parts);
|
||||
$model_values = $this->valuesForUpdate($model);
|
||||
$old_values = $this->valuesForUpdate($old);
|
||||
|
||||
$columns = [];
|
||||
$values = [];
|
||||
foreach ($this->fieldsForUpdate() as $i => $column) {
|
||||
if (isset($model_values[$i]) and $old_values[$i] !== $model_values[$i]) {
|
||||
$columns []= "`{$column}` = ?";
|
||||
$values []= $model_values[$i];
|
||||
}
|
||||
}
|
||||
return strtolower(implode('_', $parts));
|
||||
}
|
||||
public function fillData(Model $model, array $data): Model
|
||||
{
|
||||
foreach ($this->getRequired() as $column) {
|
||||
$m = $this->getMethod($column, false);
|
||||
if (!method_exists($model, $m)) {
|
||||
continue;
|
||||
}
|
||||
$model->{$m}($data[$column]);
|
||||
}
|
||||
foreach ($this->getOptional() as $column) {
|
||||
if (!isset($data[$column])) {
|
||||
continue;
|
||||
}
|
||||
$m = $this->getMethod($column, false);
|
||||
if (!method_exists($model, $m)) {
|
||||
continue;
|
||||
}
|
||||
$model->{$m}($data[$column]);
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
public function mapArray(Model $model, array $data): array
|
||||
{
|
||||
foreach ($this->getColumns() as $column) {
|
||||
if (isset($data[$column])) {
|
||||
continue;
|
||||
}
|
||||
$m = $this->getMethod($column);
|
||||
if (!method_exists($model, $m)) {
|
||||
continue;
|
||||
}
|
||||
$val = $model->{$m}();
|
||||
$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()) {
|
||||
if (count($columns) === 0) {
|
||||
return;
|
||||
}
|
||||
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 = ?']);
|
||||
$values []= $old->{$this->idProperty()}();
|
||||
$query = $this->getQueryBuilder()->update($this->getTable())->set($columns)->where(["{$this->idField()}} = ?"]);
|
||||
$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) {
|
||||
if (isset($data[$col])) {
|
||||
$m = $this->getMethod($col, false);
|
||||
if (!method_exists($model, $m)) {
|
||||
continue;
|
||||
}
|
||||
$model->{$m}($data[$col]);
|
||||
}
|
||||
try {
|
||||
return $this->defaultSearch($data);
|
||||
} catch (PDOException | BlankResult $e) {
|
||||
$data[$this->idField()] = 0;
|
||||
return $this->load($data);
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
public function delete(Model $model): void
|
||||
{
|
||||
$query = $this->getQueryBuilder()->delete($this->getTable())->where(['id = ?']);
|
||||
$this->getConnection()->execute($query, [$model->getId()]);
|
||||
$this->resetIndex();
|
||||
$this->optimize();
|
||||
}
|
||||
|
||||
protected function idProperty(): string
|
||||
{
|
||||
return 'getId';
|
||||
}
|
||||
protected function idField(): string
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
protected function insert(Model $model): void
|
||||
{
|
||||
$fields = $this->fieldsForInsert();
|
||||
$fields_string = array_map(function($field) {
|
||||
return "`{$field}`";
|
||||
}, $fields);
|
||||
$fields_questions = array_fill(0, count($fields), '?');
|
||||
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($fields_string)->values($fields_questions);
|
||||
$values = $this->valuesForInsert($model);
|
||||
$this->getConnection()->execute($query, $values);
|
||||
}
|
||||
protected function resetIndex(): void
|
||||
{
|
||||
$query = "ALTER TABLE `{$this->getTable()}` AUTO_INCREMENT = 1";
|
||||
$this->getConnection()->query($query);
|
||||
}
|
||||
protected function optimize(): void
|
||||
{
|
||||
$query = "OPTIMIZE TABLE `{$this->getTable()}`";
|
||||
$this->getConnection()->query($query);
|
||||
}
|
||||
|
||||
protected function fetchOne(string $query, ?array $values = null): Model
|
||||
{
|
||||
if ($values !== null) {
|
||||
$rs = $this->getConnection()->prepare($query);
|
||||
$rs->execute($values);
|
||||
} else {
|
||||
$rs = $this->getConnection()->query($query);
|
||||
}
|
||||
$row = $rs->getFirstAsArray();
|
||||
if (!$row) {
|
||||
throw new BlankResult();
|
||||
}
|
||||
return $this->load($row);
|
||||
}
|
||||
protected function fetchMany(string $query, ?array $values = null): array
|
||||
{
|
||||
if ($values !== null) {
|
||||
$rs = $this->getConnection()->prepare($query);
|
||||
$rs->execute($values);
|
||||
} else {
|
||||
$rs = $this->getConnection()->query($query);
|
||||
}
|
||||
$rows = $rs->getAsArray();
|
||||
if (!$rows) {
|
||||
throw new BlankResult();
|
||||
}
|
||||
return array_map([$this, 'load'], $rows);
|
||||
}
|
||||
|
||||
public function fetchById(int $id): Model
|
||||
@ -247,13 +187,20 @@ abstract class Repository implements RepositoryInterface
|
||||
->from($this->getTable())
|
||||
->where(['id = ?'])
|
||||
->limit(1);
|
||||
return $this->load($this->getConnection()->execute($query, [$id])->getAsArray()[0]);
|
||||
return $this->fetchOne($query, [$id]);
|
||||
}
|
||||
public function fetchAll(): array
|
||||
{
|
||||
$query = $this->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable());
|
||||
return array_map([$this, 'load'], $this->getConnection()->query($query)->getAsArray());
|
||||
return $this->fetchMany($query);
|
||||
}
|
||||
|
||||
abstract protected function fieldsForUpdate(): array;
|
||||
abstract protected function fieldsForInsert(): array;
|
||||
abstract protected function valuesForUpdate(Model $model): array;
|
||||
abstract protected function valuesForInsert(Model $model): array;
|
||||
abstract protected function defaultFind(Model $model): Model;
|
||||
abstract protected function defaultSearch(array $data): Model;
|
||||
}
|
||||
|
@ -6,15 +6,6 @@ use ProVM\Concept\Model\Repository;
|
||||
|
||||
interface Model extends JsonSerializable
|
||||
{
|
||||
public function setRepository(Repository $factory): Model;
|
||||
public function getRepository(): Repository;
|
||||
public function setId(int $id): Model;
|
||||
public function getId(): int;
|
||||
public function setNew(): Model;
|
||||
public function isNew(): bool;
|
||||
public function setDirty(): Model;
|
||||
public function isDirty(): bool;
|
||||
public function save(): void;
|
||||
public function edit(array $data): void;
|
||||
public function delete(): void;
|
||||
}
|
||||
|
@ -1,11 +1,48 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Model;
|
||||
|
||||
use ProVM\Concept\Model;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
interface Factory
|
||||
{
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
* @return Factory
|
||||
*/
|
||||
public function setContainer(ContainerInterface $container): Factory;
|
||||
|
||||
/**
|
||||
* @return ContainerInterface
|
||||
*/
|
||||
public function getContainer(): ContainerInterface;
|
||||
|
||||
/**
|
||||
* Get Repository by class
|
||||
* @param string $repository_class
|
||||
* @return Repository
|
||||
*/
|
||||
public function get(string $repository_class): Repository;
|
||||
|
||||
/**
|
||||
* Get Repository by Model class
|
||||
* @param string $model_class
|
||||
* @return Repository
|
||||
*/
|
||||
public function getByModel(string $model_class): Repository;
|
||||
|
||||
/**
|
||||
* Fetch Model by Id
|
||||
* @param string $model_class
|
||||
* @param int $id
|
||||
* @return Model
|
||||
*/
|
||||
public function fetchById(string $model_class, int $id): Model;
|
||||
|
||||
/**
|
||||
* Fetch all Models
|
||||
* @param string $model_class
|
||||
* @return array
|
||||
*/
|
||||
public function fetchAll(string $model_class): array;
|
||||
}
|
||||
|
@ -7,37 +7,105 @@ use ProVM\Concept\Model;
|
||||
|
||||
interface Repository
|
||||
{
|
||||
public function setConnection(Connection $connection): Repository;
|
||||
/**
|
||||
* @return Connection
|
||||
*/
|
||||
public function getConnection(): Connection;
|
||||
public function setQueryBuilder(QueryBuilder $builder): Repository;
|
||||
/**
|
||||
* @return QueryBuilder
|
||||
*/
|
||||
public function getQueryBuilder(): QueryBuilder;
|
||||
public function setFactory(Factory $factory): Repository;
|
||||
/**
|
||||
* @return Factory
|
||||
*/
|
||||
public function getFactory(): Factory;
|
||||
public function setModel(string $model_class): Repository;
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getModel(): string;
|
||||
public function getNewModel(): Model;
|
||||
public function setup(): Repository;
|
||||
public function setTable(string $table): Repository;
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* @param Connection $connection
|
||||
* @return Repository
|
||||
*/
|
||||
public function setConnection(Connection $connection): Repository;
|
||||
/**
|
||||
* @param QueryBuilder $builder
|
||||
* @return Repository
|
||||
*/
|
||||
public function setQueryBuilder(QueryBuilder $builder): Repository;
|
||||
/**
|
||||
* @param Factory $factory
|
||||
* @return Repository
|
||||
*/
|
||||
public function setFactory(Factory $factory): Repository;
|
||||
/**
|
||||
* @param string $model_class
|
||||
* @return Repository
|
||||
*/
|
||||
public function setModel(string $model_class): Repository;
|
||||
/**
|
||||
* @param string $table
|
||||
* @return Repository
|
||||
*/
|
||||
public function setTable(string $table): Repository;
|
||||
|
||||
/**
|
||||
* Set up the Repository
|
||||
* SHOULD CALL
|
||||
* setTable
|
||||
* setColumns [setRequired, setOptional]
|
||||
* setProperties
|
||||
* setMapping
|
||||
* setModel
|
||||
* @return Repository
|
||||
*/
|
||||
public function setup(): Repository;
|
||||
|
||||
/**
|
||||
* Transform result array to Model
|
||||
* @param array $data
|
||||
* @return Model
|
||||
*/
|
||||
public function load(array $data): Model;
|
||||
public function save(Model $model): void;
|
||||
public function update(Model $model): void;
|
||||
/**
|
||||
* Save Model to table
|
||||
* @param Model $model
|
||||
* @return void
|
||||
*/
|
||||
public function save(Model &$model): void;
|
||||
/**
|
||||
* Update table value with Model
|
||||
* @param Model $model
|
||||
* @return void
|
||||
*/
|
||||
public function update(Model $model, Model $old): void;
|
||||
/**
|
||||
* Create new Model with data
|
||||
* @param array $data
|
||||
* @return Model
|
||||
*/
|
||||
public function create(array $data): Model;
|
||||
public function edit(Model $model, array $data): Model;
|
||||
/**
|
||||
* Delete Model from table
|
||||
* @param Model $model
|
||||
* @return void
|
||||
*/
|
||||
public function delete(Model $model): void;
|
||||
|
||||
/**
|
||||
* Fetch Model by Id
|
||||
* @param int $id
|
||||
* @return Model
|
||||
*/
|
||||
public function fetchById(int $id): Model;
|
||||
/**
|
||||
* Fetch all Models
|
||||
* @return array
|
||||
*/
|
||||
public function fetchAll(): array;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
namespace ProVM\Implement\Model;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Concept\Model;
|
||||
use ProVM\Concept\Model\Factory as FactoryInterface;
|
||||
use ProVM\Concept\Model\Repository;
|
||||
|
||||
@ -26,4 +27,18 @@ class Factory implements FactoryInterface
|
||||
{
|
||||
return $this->getContainer()->get($repository_class);
|
||||
}
|
||||
public function getByModel(string $model_class): Repository
|
||||
{
|
||||
$class = str_replace('Model', 'Repository', $model_class);
|
||||
return $this->get($class);
|
||||
}
|
||||
|
||||
public function fetchById(string $model_class, int $id): Model
|
||||
{
|
||||
return $this->getByModel($model_class)->fetchById($id);
|
||||
}
|
||||
public function fetchAll(string $model_class): array
|
||||
{
|
||||
return $this->getByModel($model_class)->fetchAll();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user