Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
4e7ff1e508 | |||
7a88398379 | |||
7848d973a6 | |||
d2c5c1180b | |||
e4cec2e2f2 | |||
8a03d9e675 | |||
100df73916 | |||
5c1f424c76 | |||
fe67519682 | |||
0a9bed1735 | |||
422d9a6dbd | |||
23c56f9511 | |||
ef1603bc4b | |||
f19385ccc1 | |||
d3771d8844 | |||
aff0d4333d | |||
51124218d5 | |||
806b8e7b93 | |||
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,119 +1,163 @@
|
||||
<?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(Connection $connection, QueryBuilder $builder, Factory $factory)
|
||||
{
|
||||
$this->setConnection($connection)
|
||||
->setQueryBuilder($builder)
|
||||
->setFactory($factory)
|
||||
->setup();
|
||||
}
|
||||
public function __construct(protected Connection $connection, protected QueryBuilder $builder, protected Factory $factory)
|
||||
{}
|
||||
|
||||
protected string $model;
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
public function save(Model $model): void
|
||||
public function save(Model &$model): void
|
||||
{
|
||||
if (!$model->isDirty() and !$model->isNew()) {
|
||||
try {
|
||||
$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
|
||||
{
|
||||
$mapper = $this->getMapper();
|
||||
$model_values = $mapper->mapModelToTable($model);
|
||||
$old_values = $mapper->mapModelToTable($old);
|
||||
|
||||
$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) {
|
||||
return;
|
||||
}
|
||||
$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);
|
||||
|
||||
$values []= $old->{$this->idProperty()}();
|
||||
$query = $this->getQueryBuilder()->update($this->getTable())->set($columns)->where(["{$this->idField()}} = ?"]);
|
||||
$this->getConnection()->execute($query, $values);
|
||||
}
|
||||
public function edit(Model $model, array $data): Model
|
||||
public function create(array $data): Model
|
||||
{
|
||||
foreach ($this->getColumns() as $col) {
|
||||
if (isset($data[$col])) {
|
||||
$m = 'set' . ucwords($col);
|
||||
$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(["{$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);
|
||||
}
|
||||
|
||||
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 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,18 +1,10 @@
|
||||
<?php
|
||||
namespace ProVM\Concept;
|
||||
|
||||
use ProVM\Concept\Model\Factory;
|
||||
use JsonSerializable;
|
||||
|
||||
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;
|
||||
}
|
||||
|
11
src/Concept/Model/Mapper.php
Normal file
11
src/Concept/Model/Mapper.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?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,22 +7,67 @@ 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;
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
|
39
src/Implement/Model/Mapper.php
Normal file
39
src/Implement/Model/Mapper.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
63
src/Implement/Model/Mapper/PropertyMap.php
Normal file
63
src/Implement/Model/Mapper/PropertyMap.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?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