Compare commits
35 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 | |||
4ccc38ffac | |||
ef1603bc4b | |||
8531658899 | |||
f19385ccc1 | |||
02f8bb0b4f | |||
d3771d8844 | |||
10e87b71a3 | |||
aff0d4333d | |||
51124218d5 | |||
37c5a79d5a | |||
806b8e7b93 | |||
d12f3f7897 | |||
3811e8224b | |||
9834eb70a4 |
@ -1,21 +1,11 @@
|
||||
<?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
|
||||
{
|
||||
$this->factory = $factory;
|
||||
return $this;
|
||||
}
|
||||
public function getFactory(): Factory
|
||||
{
|
||||
return $this->factory;
|
||||
}
|
||||
protected int $id;
|
||||
public function setId(int $id): ModelInterface
|
||||
{
|
||||
@ -26,39 +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->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,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,102 +20,187 @@ 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 $table;
|
||||
public function setTable(string $table): RepositoryInterface
|
||||
public function getModel(): string
|
||||
{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
return $this->model;
|
||||
}
|
||||
public function getTable(): string
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
protected array $columns;
|
||||
public function setColumns(array $columns): RepositoryInterface
|
||||
|
||||
public function setConnection(Connection $connection): Repository
|
||||
{
|
||||
foreach ($columns as $column) {
|
||||
$this->addColumn($column);
|
||||
}
|
||||
$this->connection = $connection;
|
||||
return $this;
|
||||
}
|
||||
public function addColumn(string $column): RepositoryInterface
|
||||
public function setQueryBuilder(QueryBuilder $builder): RepositoryInterface
|
||||
{
|
||||
$this->columns []= $column;
|
||||
$this->builder = $builder;
|
||||
return $this;
|
||||
}
|
||||
public function getColumns(): array
|
||||
public function setFactory(Factory $factory): RepositoryInterface
|
||||
{
|
||||
return $this->columns;
|
||||
$this->factory = $factory;
|
||||
return $this;
|
||||
}
|
||||
public function setModel(string $model_class): RepositoryInterface
|
||||
{
|
||||
$this->model = $model_class;
|
||||
return $this;
|
||||
}
|
||||
public function setTable(string $table): RepositoryInterface
|
||||
{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function save(Model $model): void
|
||||
public function save(Model &$model): void
|
||||
{
|
||||
if (!$model->isDirty() and !$model->isNew()) {
|
||||
return;
|
||||
try {
|
||||
$old = $this->defaultFind($model);
|
||||
$this->update($model, $old);
|
||||
} catch (BlankResult $e) {
|
||||
$this->insert($model);
|
||||
$model->setId($this->getConnection()->getPDO()->lastInsertId());
|
||||
}
|
||||
$cols = [];
|
||||
$values = [];
|
||||
foreach ($this->getColumns() as $column) {
|
||||
$m = 'get' . ucwords($column);
|
||||
$cols []= '?';
|
||||
$values []= $model->{$m}();
|
||||
}
|
||||
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols);
|
||||
$this->getConnection()->execute($query, $values);
|
||||
}
|
||||
public function edit(Model $model, array $data): Model
|
||||
public function update(Model $model, Model $old): void
|
||||
{
|
||||
foreach ($this->getColumns() as $col) {
|
||||
if (isset($data[$col])) {
|
||||
$m = 'set' . ucwords($col);
|
||||
$model->{$m}($data[$col]);
|
||||
$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 $model;
|
||||
if (count($columns) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$values = array_values($values);
|
||||
$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
|
||||
{
|
||||
try {
|
||||
return $this->defaultSearch($data);
|
||||
} catch (PDOException | BlankResult $e) {
|
||||
$data[$this->idField()] = 0;
|
||||
return $this->load($data);
|
||||
}
|
||||
}
|
||||
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
|
||||
{
|
||||
$query = $this->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where([['id', '?']])
|
||||
->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;
|
||||
}
|
||||
|
@ -1,18 +1,11 @@
|
||||
<?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 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;
|
||||
}
|
||||
|
@ -1,13 +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;
|
||||
public function setNamespace(string $namespace): Factory;
|
||||
public function getNamespace(): string;
|
||||
public function get(string $repository_name): Repository;
|
||||
|
||||
/**
|
||||
* 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,22 +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 setup(): Repository;
|
||||
public function setTable(string $table): Repository;
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getModel(): string;
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTable(): string;
|
||||
public function setColumns(array $columns): Repository;
|
||||
public function addColumn(string $column): Repository;
|
||||
public function getColumns(): 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 edit(Model $model, array $data): Model;
|
||||
/**
|
||||
* 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;
|
||||
/**
|
||||
* 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;
|
||||
|
||||
@ -22,25 +23,22 @@ 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;
|
||||
return $this->getContainer()->get($repository_class);
|
||||
}
|
||||
public function getNamespace(): string
|
||||
public function getByModel(string $model_class): Repository
|
||||
{
|
||||
return $this->namespace;
|
||||
$class = str_replace('Model', 'Repository', $model_class);
|
||||
return $this->get($class);
|
||||
}
|
||||
protected function buildRepository(string $repository_name): string
|
||||
|
||||
public function fetchById(string $model_class, int $id): Model
|
||||
{
|
||||
return implode("\\", [
|
||||
$this->getNamespace(),
|
||||
$repository_name
|
||||
]);
|
||||
return $this->getByModel($model_class)->fetchById($id);
|
||||
}
|
||||
public function get(string $repository_name): Repository
|
||||
public function fetchAll(string $model_class): array
|
||||
{
|
||||
return $this->getContainer()->get($this->buildRepository($repository_name));
|
||||
return $this->getByModel($model_class)->fetchAll();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user