Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
81ad81f7bf | |||
12065b376b |
@ -1,11 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Alias;
|
namespace ProVM\Alias;
|
||||||
|
|
||||||
use ProVM\Concept\Model\Repository;
|
use ProVM\Concept\Model\Factory;
|
||||||
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;
|
||||||
|
public function setFactory(Factory $factory): ModelInterface
|
||||||
|
{
|
||||||
|
$this->factory = $factory;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function getFactory(): Factory
|
||||||
|
{
|
||||||
|
return $this->factory;
|
||||||
|
}
|
||||||
protected int $id;
|
protected int $id;
|
||||||
public function setId(int $id): ModelInterface
|
public function setId(int $id): ModelInterface
|
||||||
{
|
{
|
||||||
@ -16,4 +26,39 @@ abstract class Model implements ModelInterface
|
|||||||
{
|
{
|
||||||
return $this->id;
|
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->getFactory()->get(get_class($this))->save($this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function edit(array $data): void
|
||||||
|
{
|
||||||
|
foreach ($data as $key => $val) {
|
||||||
|
$m = 'set' . ucwords($key);
|
||||||
|
$this->{$m}($val);
|
||||||
|
}
|
||||||
|
$this->isDirty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Alias\Model;
|
namespace ProVM\Alias\Model;
|
||||||
|
|
||||||
use PDOException;
|
|
||||||
use ProVM\Concept\Database\Connection;
|
use ProVM\Concept\Database\Connection;
|
||||||
use ProVM\Concept\Database\QueryBuilder;
|
use ProVM\Concept\Database\QueryBuilder;
|
||||||
use ProVM\Concept\Model;
|
use ProVM\Concept\Model;
|
||||||
use ProVM\Concept\Model\Factory;
|
use ProVM\Concept\Model\Factory;
|
||||||
use ProVM\Concept\Model\Repository as RepositoryInterface;
|
use ProVM\Concept\Model\Repository as RepositoryInterface;
|
||||||
use ProVM\Exception\BlankResult;
|
|
||||||
|
|
||||||
abstract class Repository implements RepositoryInterface
|
abstract class Repository implements RepositoryInterface
|
||||||
{
|
{
|
||||||
@ -20,187 +18,102 @@ abstract class Repository implements RepositoryInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected Connection $connection;
|
protected Connection $connection;
|
||||||
protected QueryBuilder $builder;
|
|
||||||
protected Factory $factory;
|
|
||||||
protected string $model;
|
|
||||||
protected string $table;
|
|
||||||
|
|
||||||
public function getConnection(): Connection
|
|
||||||
{
|
|
||||||
return $this->connection;
|
|
||||||
}
|
|
||||||
public function getQueryBuilder(): QueryBuilder
|
|
||||||
{
|
|
||||||
return $this->builder;
|
|
||||||
}
|
|
||||||
public function getFactory(): Factory
|
|
||||||
{
|
|
||||||
return $this->factory;
|
|
||||||
}
|
|
||||||
public function getModel(): string
|
|
||||||
{
|
|
||||||
return $this->model;
|
|
||||||
}
|
|
||||||
public function getTable(): string
|
|
||||||
{
|
|
||||||
return $this->table;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setConnection(Connection $connection): Repository
|
public function setConnection(Connection $connection): Repository
|
||||||
{
|
{
|
||||||
$this->connection = $connection;
|
$this->connection = $connection;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
public function getConnection(): Connection
|
||||||
|
{
|
||||||
|
return $this->connection;
|
||||||
|
}
|
||||||
|
protected QueryBuilder $builder;
|
||||||
public function setQueryBuilder(QueryBuilder $builder): RepositoryInterface
|
public function setQueryBuilder(QueryBuilder $builder): RepositoryInterface
|
||||||
{
|
{
|
||||||
$this->builder = $builder;
|
$this->builder = $builder;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
public function getQueryBuilder(): QueryBuilder
|
||||||
|
{
|
||||||
|
return $this->builder;
|
||||||
|
}
|
||||||
|
protected Factory $factory;
|
||||||
public function setFactory(Factory $factory): RepositoryInterface
|
public function setFactory(Factory $factory): RepositoryInterface
|
||||||
{
|
{
|
||||||
$this->factory = $factory;
|
$this->factory = $factory;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setModel(string $model_class): RepositoryInterface
|
public function getFactory(): Factory
|
||||||
{
|
{
|
||||||
$this->model = $model_class;
|
return $this->factory;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
protected string $table;
|
||||||
public function setTable(string $table): RepositoryInterface
|
public function setTable(string $table): RepositoryInterface
|
||||||
{
|
{
|
||||||
$this->table = $table;
|
$this->table = $table;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
public function getTable(): string
|
||||||
public function save(Model &$model): void
|
|
||||||
{
|
{
|
||||||
try {
|
return $this->table;
|
||||||
$old = $this->defaultFind($model);
|
|
||||||
$this->update($model, $old);
|
|
||||||
} catch (BlankResult $e) {
|
|
||||||
$this->insert($model);
|
|
||||||
$model->setId($this->getConnection()->getPDO()->lastInsertId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
public function update(Model $model, Model $old): void
|
protected array $columns;
|
||||||
|
public function setColumns(array $columns): RepositoryInterface
|
||||||
{
|
{
|
||||||
$model_values = $this->valuesForUpdate($model);
|
foreach ($columns as $column) {
|
||||||
$old_values = $this->valuesForUpdate($old);
|
$this->addColumn($column);
|
||||||
|
|
||||||
$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];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (count($columns) === 0) {
|
return $this;
|
||||||
|
}
|
||||||
|
public function addColumn(string $column): RepositoryInterface
|
||||||
|
{
|
||||||
|
$this->columns []= $column;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function getColumns(): array
|
||||||
|
{
|
||||||
|
return $this->columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save(Model $model): void
|
||||||
|
{
|
||||||
|
if (!$model->isDirty() and !$model->isNew()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
$cols = [];
|
||||||
$values = array_values($values);
|
$values = [];
|
||||||
$values []= $old->{$this->idProperty()}();
|
foreach ($this->getColumns() as $column) {
|
||||||
$query = $this->getQueryBuilder()->update($this->getTable())->set($columns)->where(["{$this->idField()}} = ?"]);
|
$m = 'get' . ucwords($column);
|
||||||
|
$cols []= '?';
|
||||||
|
$values []= $model->{$m}();
|
||||||
|
}
|
||||||
|
$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
|
public function edit(Model $model, array $data): Model
|
||||||
{
|
{
|
||||||
try {
|
foreach ($this->getColumns() as $col) {
|
||||||
return $this->defaultSearch($data);
|
if (isset($data[$col])) {
|
||||||
} catch (PDOException | BlankResult $e) {
|
$m = 'set' . ucwords($col);
|
||||||
$data[$this->idField()] = 0;
|
$model->{$m}($data[$col]);
|
||||||
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
|
public function fetchById(int $id): Model
|
||||||
{
|
{
|
||||||
$query = $this->getQueryBuilder()
|
$query = $this->getQueryBuilder()
|
||||||
->select()
|
->select()
|
||||||
->from($this->getTable())
|
->from($this->getTable())
|
||||||
->where(['id = ?'])
|
->where([['id', '?']])
|
||||||
->limit(1);
|
->limit(1);
|
||||||
return $this->fetchOne($query, [$id]);
|
return $this->load($this->getConnection()->execute($query, [$id])->getAsArray()[0]);
|
||||||
}
|
}
|
||||||
public function fetchAll(): array
|
public function fetchAll(): array
|
||||||
{
|
{
|
||||||
$query = $this->getQueryBuilder()
|
$query = $this->getQueryBuilder()
|
||||||
->select()
|
->select()
|
||||||
->from($this->getTable());
|
->from($this->getTable());
|
||||||
return $this->fetchMany($query);
|
return array_map([$this, 'load'], $this->getConnection()->query($query)->getAsArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,18 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Concept;
|
namespace ProVM\Concept;
|
||||||
|
|
||||||
use JsonSerializable;
|
use ProVM\Concept\Model\Factory;
|
||||||
use ProVM\Concept\Model\Repository;
|
|
||||||
|
|
||||||
interface Model extends JsonSerializable
|
interface Model
|
||||||
{
|
{
|
||||||
|
public function setFactory(Factory $factory): Model;
|
||||||
|
public function getFactory(): Factory;
|
||||||
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 isNew(): bool;
|
||||||
|
public function setDirty(): Model;
|
||||||
|
public function isDirty(): bool;
|
||||||
|
public function save(): void;
|
||||||
|
public function edit(array $data): void;
|
||||||
}
|
}
|
||||||
|
@ -1,48 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Concept\Model;
|
namespace ProVM\Concept\Model;
|
||||||
|
|
||||||
use ProVM\Concept\Model;
|
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
interface Factory
|
interface Factory
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @param ContainerInterface $container
|
|
||||||
* @return Factory
|
|
||||||
*/
|
|
||||||
public function setContainer(ContainerInterface $container): Factory;
|
public function setContainer(ContainerInterface $container): Factory;
|
||||||
|
|
||||||
/**
|
|
||||||
* @return ContainerInterface
|
|
||||||
*/
|
|
||||||
public function getContainer(): ContainerInterface;
|
public function getContainer(): ContainerInterface;
|
||||||
|
public function setNamespace(string $namespace): Factory;
|
||||||
/**
|
public function getNamespace(): string;
|
||||||
* Get Repository by class
|
public function get(string $repository_name): Repository;
|
||||||
* @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,105 +7,22 @@ use ProVM\Concept\Model;
|
|||||||
|
|
||||||
interface Repository
|
interface Repository
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @return Connection
|
|
||||||
*/
|
|
||||||
public function getConnection(): Connection;
|
|
||||||
/**
|
|
||||||
* @return QueryBuilder
|
|
||||||
*/
|
|
||||||
public function getQueryBuilder(): QueryBuilder;
|
|
||||||
/**
|
|
||||||
* @return Factory
|
|
||||||
*/
|
|
||||||
public function getFactory(): Factory;
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getModel(): string;
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getTable(): string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Connection $connection
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setConnection(Connection $connection): Repository;
|
public function setConnection(Connection $connection): Repository;
|
||||||
/**
|
public function getConnection(): Connection;
|
||||||
* @param QueryBuilder $builder
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setQueryBuilder(QueryBuilder $builder): Repository;
|
public function setQueryBuilder(QueryBuilder $builder): Repository;
|
||||||
/**
|
public function getQueryBuilder(): QueryBuilder;
|
||||||
* @param Factory $factory
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setFactory(Factory $factory): Repository;
|
public function setFactory(Factory $factory): Repository;
|
||||||
/**
|
public function getFactory(): Factory;
|
||||||
* @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;
|
public function setup(): Repository;
|
||||||
|
public function setTable(string $table): Repository;
|
||||||
/**
|
public function getTable(): string;
|
||||||
* Transform result array to Model
|
public function setColumns(array $columns): Repository;
|
||||||
* @param array $data
|
public function addColumn(string $column): Repository;
|
||||||
* @return Model
|
public function getColumns(): array;
|
||||||
*/
|
|
||||||
public function load(array $data): Model;
|
public function load(array $data): Model;
|
||||||
/**
|
public function save(Model $model): void;
|
||||||
* Save Model to table
|
public function edit(Model $model, array $data): Model;
|
||||||
* @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 create(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;
|
public function fetchById(int $id): Model;
|
||||||
/**
|
|
||||||
* Fetch all Models
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function fetchAll(): array;
|
public function fetchAll(): array;
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
namespace ProVM\Implement\Model;
|
namespace ProVM\Implement\Model;
|
||||||
|
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
use ProVM\Concept\Model;
|
|
||||||
use ProVM\Concept\Model\Factory as FactoryInterface;
|
use ProVM\Concept\Model\Factory as FactoryInterface;
|
||||||
use ProVM\Concept\Model\Repository;
|
use ProVM\Concept\Model\Repository;
|
||||||
|
|
||||||
@ -23,22 +22,25 @@ class Factory implements FactoryInterface
|
|||||||
{
|
{
|
||||||
return $this->container;
|
return $this->container;
|
||||||
}
|
}
|
||||||
public function get(string $repository_class): Repository
|
protected string $namespace;
|
||||||
|
public function setNamespace(string $namespace): FactoryInterface
|
||||||
{
|
{
|
||||||
return $this->getContainer()->get($repository_class);
|
$this->namespace = $namespace;
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
public function getByModel(string $model_class): Repository
|
public function getNamespace(): string
|
||||||
{
|
{
|
||||||
$class = str_replace('Model', 'Repository', $model_class);
|
return $this->namespace;
|
||||||
return $this->get($class);
|
|
||||||
}
|
}
|
||||||
|
protected function buildRepository(string $repository_name): string
|
||||||
public function fetchById(string $model_class, int $id): Model
|
|
||||||
{
|
{
|
||||||
return $this->getByModel($model_class)->fetchById($id);
|
return implode("\\", [
|
||||||
|
$this->getNamespace(),
|
||||||
|
$repository_name
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
public function fetchAll(string $model_class): array
|
public function get(string $repository_name): Repository
|
||||||
{
|
{
|
||||||
return $this->getByModel($model_class)->fetchAll();
|
return $this->getContainer()->get($this->buildRepository($repository_name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user