Compare commits
4 Commits
2a06507876
...
develop
Author | SHA1 | Date | |
---|---|---|---|
4e7ff1e508 | |||
7a88398379 | |||
7848d973a6 | |||
d2c5c1180b |
@ -6,16 +6,6 @@ use ProVM\Concept\Model as ModelInterface;
|
|||||||
|
|
||||||
abstract class Model implements 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;
|
protected int $id;
|
||||||
public function setId(int $id): ModelInterface
|
public function setId(int $id): ModelInterface
|
||||||
{
|
{
|
||||||
@ -26,64 +16,4 @@ 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->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(preg_replace('/(?<!^)[A-Z]/', '_$0', str_replace('get', '', $method->getName())));
|
|
||||||
if (!isset($this->{$p})) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$output[$p] = $this->{$method->getName()}();
|
|
||||||
}
|
|
||||||
return $output;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,31 +1,20 @@
|
|||||||
<?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
|
||||||
{
|
{
|
||||||
public function __construct(Connection $connection, QueryBuilder $builder, Factory $factory)
|
public function __construct(protected Connection $connection, protected QueryBuilder $builder, protected Factory $factory)
|
||||||
{
|
{}
|
||||||
$this->setConnection($connection)
|
|
||||||
->setQueryBuilder($builder)
|
|
||||||
->setFactory($factory)
|
|
||||||
->setup();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Connection $connection;
|
|
||||||
protected QueryBuilder $builder;
|
|
||||||
protected Factory $factory;
|
|
||||||
protected string $model;
|
protected string $model;
|
||||||
protected string $table;
|
|
||||||
protected array $columns;
|
|
||||||
protected array $required_columns;
|
|
||||||
protected array $optional_columns;
|
|
||||||
protected array $properties;
|
|
||||||
|
|
||||||
public function getConnection(): Connection
|
public function getConnection(): Connection
|
||||||
{
|
{
|
||||||
@ -39,285 +28,111 @@ abstract class Repository implements RepositoryInterface
|
|||||||
{
|
{
|
||||||
return $this->factory;
|
return $this->factory;
|
||||||
}
|
}
|
||||||
public function getModel(): string
|
|
||||||
{
|
|
||||||
return $this->model;
|
|
||||||
}
|
|
||||||
public function getTable(): string
|
|
||||||
{
|
|
||||||
return $this->table;
|
|
||||||
}
|
|
||||||
public function getColumns(): array
|
|
||||||
{
|
|
||||||
return $this->columns ?? array_merge($this->getRequiredColumns(), $this->getOptionalColumns());
|
|
||||||
}
|
|
||||||
public function getRequiredColumns(): array
|
|
||||||
{
|
|
||||||
if (isset($this->optional_columns) and !isset($this->required_columns)) {
|
|
||||||
return array_diff($this->getColumns(), $this->getOptionalColumns());
|
|
||||||
}
|
|
||||||
return $this->required_columns ?? $this->getColumns();
|
|
||||||
}
|
|
||||||
public function getOptionalColumns(): array
|
|
||||||
{
|
|
||||||
if (isset($this->required_columns) and !isset($this->optional_columns)) {
|
|
||||||
return array_diff($this->getColumns(), $this->getRequiredColumns());
|
|
||||||
}
|
|
||||||
return $this->optional_columns ?? [];
|
|
||||||
}
|
|
||||||
public function getProperties(): array
|
|
||||||
{
|
|
||||||
return $this->properties ?? $this->getColumns();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setConnection(Connection $connection): Repository
|
public function save(Model &$model): void
|
||||||
{
|
|
||||||
$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;
|
|
||||||
}
|
|
||||||
public function setTable(string $table): RepositoryInterface
|
|
||||||
{
|
|
||||||
$this->table = $table;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function setColumns(array $columns): RepositoryInterface
|
|
||||||
{
|
|
||||||
foreach ($columns as $column) {
|
|
||||||
$this->addColumn($column);
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function setRequiredColumns(array $columns): RepositoryInterface
|
|
||||||
{
|
|
||||||
foreach ($columns as $item) {
|
|
||||||
$this->addRequiredColumn($item);
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function setOptionalColumns(array $columns): RepositoryInterface
|
|
||||||
{
|
|
||||||
foreach ($columns as $item) {
|
|
||||||
$this->addOptionalColumn($item);
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function setProperties(array $properties): RepositoryInterface
|
|
||||||
{
|
|
||||||
foreach ($properties as $property) {
|
|
||||||
$this->addProperty($property);
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addColumn(string $column): RepositoryInterface
|
|
||||||
{
|
|
||||||
$this->columns []= $column;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function addRequiredColumn(string $column): RepositoryInterface
|
|
||||||
{
|
|
||||||
$this->required_columns []= $column;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function addOptionalColumn(string $column): RepositoryInterface
|
|
||||||
{
|
|
||||||
$this->optional_columns []= $column;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function addProperty(string $property): RepositoryInterface
|
|
||||||
{
|
|
||||||
$this->properties []= $property;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getNewModel(): Model
|
|
||||||
{
|
|
||||||
$class = $this->getModel();
|
|
||||||
return (new $class())
|
|
||||||
->setRepository($this);
|
|
||||||
}
|
|
||||||
public function getMethod(string $property, bool $get = true): string
|
|
||||||
{
|
|
||||||
$m = str_replace(' ', '', ucwords(str_replace('_', ' ', $property)));
|
|
||||||
if ($get) {
|
|
||||||
return "get{$m}";
|
|
||||||
}
|
|
||||||
return "set{$m}";
|
|
||||||
}
|
|
||||||
public function getProperty(string $method): string
|
|
||||||
{
|
|
||||||
$parts = preg_split('/(?=[A-Z])/', $method);
|
|
||||||
if (in_array(strtolower($parts[0]), ['get', 'set'])) {
|
|
||||||
array_shift($parts);
|
|
||||||
}
|
|
||||||
return strtolower(implode('_', $parts));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function compareProp(string $property, ?array $data = null): bool
|
|
||||||
{
|
|
||||||
if ($data === null) {
|
|
||||||
return in_array($property, $this->getRequiredColumns());
|
|
||||||
}
|
|
||||||
return in_array($property, $this->getOptionalColumns()) and key_exists($property, $data);
|
|
||||||
}
|
|
||||||
protected function compareProperty(string $property, ?array $data = null, ?string &$tmp_property = null): bool
|
|
||||||
{
|
|
||||||
$tmp_property = $property;
|
|
||||||
if ($this->compareProp($tmp_property, $data)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
$tmp_property = "{$property}_id";
|
|
||||||
return $this->compareProp($tmp_property, $data);
|
|
||||||
}
|
|
||||||
protected function validateDefaultProperty(mixed $value): bool
|
|
||||||
{
|
|
||||||
$defaults = [
|
|
||||||
0,
|
|
||||||
'',
|
|
||||||
null
|
|
||||||
];
|
|
||||||
return in_array($value, $defaults);
|
|
||||||
}
|
|
||||||
protected function editModel(Model $model, string $getter, string $setter, mixed $value, bool $optional = false): Model
|
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$val = $model->{$getter}();
|
$old = $this->defaultFind($model);
|
||||||
if ($optional and $this->validateDefaultProperty($val)) {
|
$this->update($model, $old);
|
||||||
$model->{$setter}($value);
|
} catch (BlankResult $e) {
|
||||||
}
|
$this->insert($model);
|
||||||
} catch (\Error|\Exception $e) {
|
$model->setId($this->getConnection()->getPDO()->lastInsertId());
|
||||||
$model->{$setter}($value);
|
|
||||||
}
|
}
|
||||||
return $model;
|
|
||||||
}
|
}
|
||||||
public function mapModel(Model $model, array $data): Model
|
public function update(Model $model, Model $old): void
|
||||||
{
|
{
|
||||||
/**
|
$mapper = $this->getMapper();
|
||||||
* Default Model map
|
$model_values = $mapper->mapModelToTable($model);
|
||||||
* It is assumed that the Model properties are named like the table columns
|
$old_values = $mapper->mapModelToTable($old);
|
||||||
*/
|
|
||||||
foreach ($this->getProperties() as $property) {
|
|
||||||
$getter = $this->getMethod($property);
|
|
||||||
$setter = $this->getMethod($property, false);
|
|
||||||
if ($this->compareProperty($property, tmp_property: $tmp)) {
|
|
||||||
$model = $this->editModel($model, $getter, $setter, $data[$tmp]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!method_exists($model, $setter)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if ($this->compareProperty($property, $data, $tmp)) {
|
|
||||||
$model = $this->editModel($model, $getter, $setter, $data[$tmp], true);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
$value = $model->{$getter}();
|
|
||||||
if ($this->validateDefaultProperty($value)) {
|
|
||||||
error_log("Notice: {$property} has default value in " . get_called_class());
|
|
||||||
}
|
|
||||||
} catch (\Error | \Exception $e) {
|
|
||||||
error_log("Warning: Missing {$property} in data for " . get_called_class() . "::fillData");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $model;
|
|
||||||
}
|
|
||||||
public function mapTable(Model $model, array $data): array
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Default table map
|
|
||||||
* It is assumed that the table columns are named the same as the Model properties
|
|
||||||
*/
|
|
||||||
foreach ($this->getRequiredColumns() as $column) {
|
|
||||||
$m = $this->getMethod($column);
|
|
||||||
if (!method_exists($model, $m)) {
|
|
||||||
error_log("Warning: Missing getter for {$column} in " . get_called_class() . "::mapArray");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!isset($data[$column])) {
|
|
||||||
$data[$column] = $model->{$m}();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
foreach ($this->getOptionalColumns() as $column) {
|
|
||||||
$m = $this->getMethod($column);
|
|
||||||
if (!method_exists($model, $m)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!isset($data[$column])) {
|
|
||||||
$data[$column] = $model->{$m}();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function load(array $data): Model
|
$differences = array_diff_assoc($old_values, $model_values);
|
||||||
{
|
$columns = array_map(function($key) {
|
||||||
return $this->mapModel($this->getNewModel()
|
return "`{$key}` = ?";
|
||||||
->setId($data['id']), $data);
|
}, array_keys($differences));
|
||||||
}
|
$values = array_values($differences);
|
||||||
public function save(Model $model): void
|
if (count($columns) === 0) {
|
||||||
{
|
|
||||||
if (!$model->isDirty() and !$model->isNew()) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!$model->isNew()) {
|
|
||||||
$this->update($model);
|
$values []= $old->{$this->idProperty()}();
|
||||||
return;
|
$query = $this->getQueryBuilder()->update($this->getTable())->set($columns)->where(["{$this->idField()}} = ?"]);
|
||||||
}
|
|
||||||
$values = array_replace(array_flip($this->getColumns()), $this->mapTable($model, []));
|
|
||||||
$cols = array_fill(0, count($values), '?');
|
|
||||||
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols);
|
|
||||||
$this->getConnection()->execute($query, array_values($values));
|
|
||||||
}
|
|
||||||
public function update(Model $model): void
|
|
||||||
{
|
|
||||||
if (!$model->isDirty() and !$model->isNew()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$values = $this->mapTable($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 = ?']);
|
|
||||||
$this->getConnection()->execute($query, $values);
|
$this->getConnection()->execute($query, $values);
|
||||||
}
|
}
|
||||||
public function create(array $data): Model
|
public function create(array $data): Model
|
||||||
{
|
{
|
||||||
return $this->mapModel($this->getNewModel()
|
try {
|
||||||
->setNew(), $data);
|
return $this->defaultSearch($data);
|
||||||
}
|
} catch (PDOException | BlankResult $e) {
|
||||||
public function edit(Model $model, array $data): Model
|
$data[$this->idField()] = 0;
|
||||||
{
|
return $this->load($data);
|
||||||
foreach ($this->getColumns() as $col) {
|
|
||||||
if (isset($data[$col])) {
|
|
||||||
$m = $this->getMethod($col, false);
|
|
||||||
if (!method_exists($model, $m)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$model->{$m}($data[$col]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return $model;
|
|
||||||
}
|
}
|
||||||
public function delete(Model $model): void
|
public function delete(Model $model): void
|
||||||
{
|
{
|
||||||
$query = $this->getQueryBuilder()->delete($this->getTable())->where(['id = ?']);
|
$query = $this->getQueryBuilder()->delete($this->getTable())->where(["{$this->idField()} = ?"]);
|
||||||
$this->getConnection()->execute($query, [$model->getId()]);
|
$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
|
public function fetchById(int $id): Model
|
||||||
@ -327,13 +142,22 @@ abstract class Repository implements RepositoryInterface
|
|||||||
->from($this->getTable())
|
->from($this->getTable())
|
||||||
->where(['id = ?'])
|
->where(['id = ?'])
|
||||||
->limit(1);
|
->limit(1);
|
||||||
return $this->load($this->getConnection()->execute($query, [$id])->getFirstAsArray());
|
return $this->fetchOne($query, [$id]);
|
||||||
}
|
}
|
||||||
public function fetchAll(): array
|
public function fetchAll(): array
|
||||||
{
|
{
|
||||||
$query = $this->getQueryBuilder()
|
$query = $this->getQueryBuilder()
|
||||||
->select()
|
->select()
|
||||||
->from($this->getTable());
|
->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;
|
||||||
}
|
}
|
||||||
|
@ -2,19 +2,9 @@
|
|||||||
namespace ProVM\Concept;
|
namespace ProVM\Concept;
|
||||||
|
|
||||||
use JsonSerializable;
|
use JsonSerializable;
|
||||||
use ProVM\Concept\Model\Repository;
|
|
||||||
|
|
||||||
interface Model extends JsonSerializable
|
interface Model extends JsonSerializable
|
||||||
{
|
{
|
||||||
public function setRepository(Repository $factory): Model;
|
|
||||||
public function getRepository(): Repository;
|
|
||||||
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;
|
|
||||||
public function delete(): void;
|
|
||||||
}
|
}
|
||||||
|
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;
|
||||||
|
}
|
@ -27,144 +27,6 @@ interface Repository
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getTable(): string;
|
public function getTable(): string;
|
||||||
/**
|
|
||||||
* Get table columns
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getColumns(): array;
|
|
||||||
/**
|
|
||||||
* Get required columns
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getRequiredColumns(): array;
|
|
||||||
/**
|
|
||||||
* Get optional columns
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getOptionalColumns(): array;
|
|
||||||
/**
|
|
||||||
* Get Model properties
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getProperties(): 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 columns in table
|
|
||||||
* @param array $columns
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setColumns(array $columns): Repository;
|
|
||||||
/**
|
|
||||||
* Set required columns
|
|
||||||
* Optional
|
|
||||||
* @param array $columns
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setRequiredColumns(array $columns): Repository;
|
|
||||||
/**
|
|
||||||
* Set optional columns
|
|
||||||
* @param array $columns
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setOptionalColumns(array $columns): Repository;
|
|
||||||
/**
|
|
||||||
* Set Model properties
|
|
||||||
* @param array $properties
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setProperties(array $properties): Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $column
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function addColumn(string $column): Repository;
|
|
||||||
/**
|
|
||||||
* @param string $column
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function addRequiredColumn(string $column): Repository;
|
|
||||||
/**
|
|
||||||
* @param string $column
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function addOptionalColumn(string $column): Repository;
|
|
||||||
/**
|
|
||||||
* @param string $property
|
|
||||||
* @param $value
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function addProperty(string $property): Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set up the Repository
|
|
||||||
* SHOULD CALL
|
|
||||||
* setTable
|
|
||||||
* setColumns [setRequired, setOptional]
|
|
||||||
* setProperties
|
|
||||||
* setMapping
|
|
||||||
* setModel
|
|
||||||
* @return Repository
|
|
||||||
*/
|
|
||||||
public function setup(): Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get clean empty Model
|
|
||||||
* @return Model
|
|
||||||
*/
|
|
||||||
public function getNewModel(): Model;
|
|
||||||
/**
|
|
||||||
* Get Model method
|
|
||||||
* @param string $property
|
|
||||||
* @param bool $get
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getMethod(string $property, bool $get = true): string;
|
|
||||||
/**
|
|
||||||
* @param string $method
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getProperty(string $method): string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fill empty Model with data
|
|
||||||
* @param Model $model
|
|
||||||
* @param array $data
|
|
||||||
* @return Model
|
|
||||||
*/
|
|
||||||
public function mapModel(Model $model, array $data): Model;
|
|
||||||
/**
|
|
||||||
* Fill data array with Model values. Accepts a preset data array
|
|
||||||
* @param Model $model
|
|
||||||
* @param array $data
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function mapTable(Model $model, array $data): array;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transform result array to Model
|
* Transform result array to Model
|
||||||
@ -177,26 +39,19 @@ interface Repository
|
|||||||
* @param Model $model
|
* @param Model $model
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function save(Model $model): void;
|
public function save(Model &$model): void;
|
||||||
/**
|
/**
|
||||||
* Update table value with Model
|
* Update table value with Model
|
||||||
* @param Model $model
|
* @param Model $model
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function update(Model $model): void;
|
public function update(Model $model, Model $old): void;
|
||||||
/**
|
/**
|
||||||
* Create new Model with data
|
* Create new Model with data
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return Model
|
* @return Model
|
||||||
*/
|
*/
|
||||||
public function create(array $data): Model;
|
public function create(array $data): Model;
|
||||||
/**
|
|
||||||
* Edit Model with data
|
|
||||||
* @param Model $model
|
|
||||||
* @param array $data
|
|
||||||
* @return Model
|
|
||||||
*/
|
|
||||||
public function edit(Model $model, array $data): Model;
|
|
||||||
/**
|
/**
|
||||||
* Delete Model from table
|
* Delete Model from table
|
||||||
* @param Model $model
|
* @param Model $model
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement\Model;
|
namespace ProVM\Implement\Model;
|
||||||
|
|
||||||
use ProVM\Concept\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;
|
||||||
|
|
||||||
|
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