Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
d12f3f7897 | |||
7fc7de7390 | |||
3bc54fb9d1 | |||
123d46d33c | |||
65c224c636 | |||
6cd26a88ea | |||
022ba575b7 | |||
c913f65b91 | |||
c8a7781c88 | |||
1505539e61 | |||
3087a48c43 | |||
43f545516d | |||
b757ed19b2 | |||
9dc71e4d77 | |||
c6806a1c62 | |||
6fd19a11be | |||
7c727d93e9 | |||
e02b8c4063 | |||
89d1db7a7e | |||
8dc0a27fd9 | |||
ae172b902c | |||
7f81b987c9 | |||
af801e769f | |||
c40baaad3f | |||
a82fdce64b | |||
8126b1f67d | |||
2177cb4652 |
@ -1,11 +1,21 @@
|
||||
<?php
|
||||
namespace ProVM\Alias;
|
||||
|
||||
use ProVM\Concept\Model\Repository;
|
||||
use ProVM\Concept\Model\Factory;
|
||||
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
|
||||
{
|
||||
@ -16,4 +26,43 @@ 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();
|
||||
}
|
||||
public function delete(): void
|
||||
{
|
||||
$this->getFactory()->get(get_class($this))->delete($this);
|
||||
}
|
||||
}
|
||||
|
@ -1,138 +1,109 @@
|
||||
<?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
|
||||
{
|
||||
public function __construct(protected Connection $connection, protected QueryBuilder $builder, protected Factory $factory)
|
||||
{}
|
||||
|
||||
protected string $model;
|
||||
public function __construct(Connection $connection, QueryBuilder $builder, Factory $factory)
|
||||
{
|
||||
$this->setConnection($connection)
|
||||
->setQueryBuilder($builder)
|
||||
->setFactory($factory)
|
||||
->setup();
|
||||
}
|
||||
|
||||
protected Connection $connection;
|
||||
public function setConnection(Connection $connection): Repository
|
||||
{
|
||||
$this->connection = $connection;
|
||||
return $this;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
public function save(Model &$model): void
|
||||
protected string $table;
|
||||
public function setTable(string $table): RepositoryInterface
|
||||
{
|
||||
try {
|
||||
$old = $this->defaultFind($model);
|
||||
$this->update($model, $old);
|
||||
} catch (BlankResult $e) {
|
||||
$this->insert($model);
|
||||
$model->setId($this->getConnection()->getPDO()->lastInsertId());
|
||||
}
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
public function update(Model $model, Model $old): void
|
||||
public function getTable(): string
|
||||
{
|
||||
$mapper = $this->getMapper();
|
||||
$model_values = $mapper->mapModelToTable($model);
|
||||
$old_values = $mapper->mapModelToTable($old);
|
||||
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;
|
||||
}
|
||||
|
||||
$differences = array_diff_assoc($old_values, $model_values);
|
||||
$columns = array_map(function($key) {
|
||||
return "`{$key}` = ?";
|
||||
}, array_keys($differences));
|
||||
$values = array_values($differences);
|
||||
if (count($columns) === 0) {
|
||||
public function save(Model $model): void
|
||||
{
|
||||
if (!$model->isDirty() and !$model->isNew()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$values []= $old->{$this->idProperty()}();
|
||||
$query = $this->getQueryBuilder()->update($this->getTable())->set($columns)->where(["{$this->idField()}} = ?"]);
|
||||
$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 create(array $data): Model
|
||||
public function edit(Model $model, array $data): Model
|
||||
{
|
||||
try {
|
||||
return $this->defaultSearch($data);
|
||||
} catch (PDOException | BlankResult $e) {
|
||||
$data[$this->idField()] = 0;
|
||||
return $this->load($data);
|
||||
foreach ($this->getColumns() as $col) {
|
||||
if (isset($data[$col])) {
|
||||
$m = 'set' . ucwords($col);
|
||||
$model->{$m}($data[$col]);
|
||||
}
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
public function delete(Model $model): void
|
||||
{
|
||||
$query = $this->getQueryBuilder()->delete($this->getTable())->where(["{$this->idField()} = ?"]);
|
||||
$this->getConnection()->execute($query, [$model->{$this->idProperty()}()]);
|
||||
$this->resetIndex();
|
||||
$this->optimize();
|
||||
}
|
||||
|
||||
protected function idProperty(): string
|
||||
{
|
||||
return 'getId';
|
||||
}
|
||||
protected function idField(): string
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
protected function insert(Model $model): void
|
||||
{
|
||||
$mapper = $this->getMapper();
|
||||
$fields = $mapper->getColumns();
|
||||
$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 = array_values($mapper->mapModelToTable($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);
|
||||
$query = $this->getQueryBuilder()->delete($this->getTable())->where(['id = ?']);
|
||||
$this->getConnection()->execute($query, [$model->getId()]);
|
||||
}
|
||||
|
||||
public function fetchById(int $id): Model
|
||||
@ -140,24 +111,15 @@ abstract class Repository implements RepositoryInterface
|
||||
$query = $this->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where(['id = ?'])
|
||||
->where([['id', '?']])
|
||||
->limit(1);
|
||||
return $this->fetchOne($query, [$id]);
|
||||
return $this->load($this->getConnection()->execute($query, [$id])->getAsArray()[0]);
|
||||
}
|
||||
public function fetchAll(): array
|
||||
{
|
||||
$query = $this->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable());
|
||||
return $this->fetchMany($query);
|
||||
return array_map([$this, 'load'], $this->getConnection()->query($query)->getAsArray());
|
||||
}
|
||||
|
||||
abstract protected function getMapper(): Model\Mapper;
|
||||
|
||||
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,10 +1,19 @@
|
||||
<?php
|
||||
namespace ProVM\Concept;
|
||||
|
||||
use JsonSerializable;
|
||||
use ProVM\Concept\Model\Factory;
|
||||
|
||||
interface Model extends JsonSerializable
|
||||
interface Model
|
||||
{
|
||||
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;
|
||||
public function delete(): void;
|
||||
}
|
||||
|
@ -1,48 +1,11 @@
|
||||
<?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;
|
||||
}
|
||||
|
@ -1,11 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Model;
|
||||
|
||||
use ProVM\Concept;
|
||||
|
||||
interface Mapper
|
||||
{
|
||||
public function getColumns(): array;
|
||||
public function mapModelToTable(Concept\Model $model): array;
|
||||
public function mapTableToModel(array $data, Concept\Model $model): Concept\Model;
|
||||
}
|
@ -7,67 +7,23 @@ use ProVM\Concept\Model;
|
||||
|
||||
interface Repository
|
||||
{
|
||||
/**
|
||||
* @return Connection
|
||||
*/
|
||||
public function setConnection(Connection $connection): Repository;
|
||||
public function getConnection(): Connection;
|
||||
/**
|
||||
* @return QueryBuilder
|
||||
*/
|
||||
public function setQueryBuilder(QueryBuilder $builder): Repository;
|
||||
public function getQueryBuilder(): QueryBuilder;
|
||||
/**
|
||||
* @return Factory
|
||||
*/
|
||||
public function setFactory(Factory $factory): Repository;
|
||||
public function getFactory(): Factory;
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getModel(): string;
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function setup(): Repository;
|
||||
public function setTable(string $table): Repository;
|
||||
public function getTable(): string;
|
||||
|
||||
/**
|
||||
* Transform result array to Model
|
||||
* @param array $data
|
||||
* @return Model
|
||||
*/
|
||||
public function setColumns(array $columns): Repository;
|
||||
public function addColumn(string $column): Repository;
|
||||
public function getColumns(): array;
|
||||
public function load(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 save(Model $model): void;
|
||||
public function edit(Model $model, 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;
|
||||
/**
|
||||
* Fetch all Models
|
||||
* @return array
|
||||
*/
|
||||
public function fetchAll(): array;
|
||||
}
|
||||
|
@ -2,7 +2,6 @@
|
||||
namespace ProVM\Implement\Model;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Concept\Model;
|
||||
use ProVM\Concept\Model\Factory as FactoryInterface;
|
||||
use ProVM\Concept\Model\Repository;
|
||||
|
||||
@ -27,18 +26,4 @@ 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();
|
||||
}
|
||||
}
|
||||
|
@ -1,39 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Model;
|
||||
|
||||
use ProVM\Concept;
|
||||
use ProVM\Implement\Model\Mapper\PropertyMap;
|
||||
|
||||
class Mapper implements Concept\Model\Mapper
|
||||
{
|
||||
protected array $map;
|
||||
|
||||
public function getColumns(): array
|
||||
{
|
||||
return array_keys($this->map);
|
||||
}
|
||||
public function registerColumn(string $columnName, ?PropertyMap $propertyMap = null): Mapper
|
||||
{
|
||||
if ($propertyMap == null) {
|
||||
$this->map[$columnName] = (new PropertyMap())->setColumn($columnName);
|
||||
return $this;
|
||||
}
|
||||
$this->map[$columnName] = $propertyMap;
|
||||
return $this;
|
||||
}
|
||||
public function mapModelToTable(Concept\Model $model): array
|
||||
{
|
||||
$data = [];
|
||||
foreach ($this->map as $columnName => $propertyMap) {
|
||||
$data[$columnName] = $propertyMap->mapToTable($model);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
public function mapTableToModel(array $data, Concept\Model $model): Concept\Model
|
||||
{
|
||||
foreach ($this->map as $propertyMap) {
|
||||
$propertyMap->map($model, $data);
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Model\Mapper;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use ProVM\Concept\Model;
|
||||
|
||||
class PropertyMap
|
||||
{
|
||||
protected string $column;
|
||||
protected string $property;
|
||||
protected Closure $callback;
|
||||
protected mixed $defaultValue;
|
||||
|
||||
public function setProperty(string $property): PropertyMap
|
||||
{
|
||||
$this->property = $property;
|
||||
return $this;
|
||||
}
|
||||
public function setColumn(string $column): PropertyMap
|
||||
{
|
||||
$this->column = $column;
|
||||
return $this;
|
||||
}
|
||||
public function setCallback(Closure $callback): PropertyMap
|
||||
{
|
||||
$this->callback = $callback;
|
||||
return $this;
|
||||
}
|
||||
public function setDefaultValue(mixed $defaultValue): PropertyMap
|
||||
{
|
||||
$this->defaultValue = $defaultValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function map(Model $model, array $data): Model
|
||||
{
|
||||
$property = $this->property ?? $this->column;
|
||||
if (isset($this->callback)) {
|
||||
try {
|
||||
$model->{$property} = call_user_func_array($this->callback, $data);
|
||||
} catch (Exception) {
|
||||
if (!isset($data[$this->column])) {
|
||||
$model->{$property} = $this->defaultValue;
|
||||
} else {
|
||||
$model->{$property} = $data[$this->column];
|
||||
}
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
if (!isset($data[$this->column])) {
|
||||
$model->{$property} = $this->defaultValue;
|
||||
} else {
|
||||
$model->{$property} = $data[$this->column];
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
public function mapToTable(Model $model): mixed
|
||||
{
|
||||
$property = $this->property ?? $this->column;
|
||||
return $model->{$property} ?? $this->defaultValue;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user