Compare commits
39 Commits
develop
...
faf615e79d
Author | SHA1 | Date | |
---|---|---|---|
faf615e79d | |||
b8409182d7 | |||
e576ef5054 | |||
b19654bc70 | |||
0af6be2c8e | |||
ba3c70a70e | |||
bc006f3e01 | |||
4ccc38ffac | |||
8531658899 | |||
02f8bb0b4f | |||
10e87b71a3 | |||
37c5a79d5a | |||
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 |
@ -6,6 +6,16 @@ use ProVM\Concept\Model as 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;
|
||||
public function setId(int $id): ModelInterface
|
||||
{
|
||||
@ -16,4 +26,64 @@ 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->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,20 +1,31 @@
|
||||
<?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)
|
||||
{}
|
||||
public function __construct(Connection $connection, QueryBuilder $builder, Factory $factory)
|
||||
{
|
||||
$this->setConnection($connection)
|
||||
->setQueryBuilder($builder)
|
||||
->setFactory($factory)
|
||||
->setup();
|
||||
}
|
||||
|
||||
protected Connection $connection;
|
||||
protected QueryBuilder $builder;
|
||||
protected Factory $factory;
|
||||
protected string $model;
|
||||
protected string $table;
|
||||
protected array $columns;
|
||||
protected array $required_columns;
|
||||
protected array $optional_columns;
|
||||
protected array $properties;
|
||||
|
||||
public function getConnection(): Connection
|
||||
{
|
||||
@ -28,111 +39,286 @@ abstract class Repository implements RepositoryInterface
|
||||
{
|
||||
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 save(Model &$model): void
|
||||
|
||||
public function setConnection(Connection $connection): Repository
|
||||
{
|
||||
$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 {
|
||||
$old = $this->defaultFind($model);
|
||||
$this->update($model, $old);
|
||||
} catch (BlankResult $e) {
|
||||
$this->insert($model);
|
||||
$model->setId($this->getConnection()->getPDO()->lastInsertId());
|
||||
$val = $model->{$getter}();
|
||||
if ($optional and $this->validateDefaultProperty($val)) {
|
||||
$model->{$setter}($value);
|
||||
}
|
||||
} catch (\Error|\Exception $e) {
|
||||
$model->{$setter}($value);
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
public function update(Model $model, Model $old): void
|
||||
public function mapModel(Model $model, array $data): Model
|
||||
{
|
||||
$mapper = $this->getMapper();
|
||||
$model_values = $mapper->mapModelToTable($model);
|
||||
$old_values = $mapper->mapModelToTable($old);
|
||||
/**
|
||||
* Default Model map
|
||||
* It is assumed that the Model properties are named like the table columns
|
||||
*/
|
||||
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)) {
|
||||
trigger_error("{$property} has default value in " . get_called_class());
|
||||
}
|
||||
} catch (\Error | \Exception $e) {
|
||||
trigger_error("Missing {$property} in data for " . get_called_class() . "::fillData", E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
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)) {
|
||||
trigger_error("Missing getter for {$column} in " . get_called_class() . "::mapArray", E_USER_WARNING);
|
||||
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;
|
||||
}
|
||||
|
||||
$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 load(array $data): Model
|
||||
{
|
||||
return $this->mapModel($this->getNewModel()
|
||||
->setId($data['id']), $data);
|
||||
}
|
||||
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()}} = ?"]);
|
||||
if (!$model->isNew()) {
|
||||
$this->update($model);
|
||||
return;
|
||||
}
|
||||
$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);
|
||||
}
|
||||
public function create(array $data): Model
|
||||
{
|
||||
try {
|
||||
return $this->defaultSearch($data);
|
||||
} catch (PDOException | BlankResult $e) {
|
||||
$data[$this->idField()] = 0;
|
||||
return $this->load($data);
|
||||
return $this->mapModel($this->getNewModel()
|
||||
->setNew(), $data);
|
||||
}
|
||||
public function edit(Model $model, array $data): Model
|
||||
{
|
||||
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
|
||||
{
|
||||
$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
|
||||
@ -142,22 +328,13 @@ abstract class Repository implements RepositoryInterface
|
||||
->from($this->getTable())
|
||||
->where(['id = ?'])
|
||||
->limit(1);
|
||||
return $this->fetchOne($query, [$id]);
|
||||
return $this->load($this->getConnection()->execute($query, [$id])->getFirstAsArray());
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -2,9 +2,19 @@
|
||||
namespace ProVM\Concept;
|
||||
|
||||
use JsonSerializable;
|
||||
use ProVM\Concept\Model\Repository;
|
||||
|
||||
interface Model extends JsonSerializable
|
||||
{
|
||||
public function setRepository(Repository $factory): Model;
|
||||
public function getRepository(): Repository;
|
||||
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,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;
|
||||
}
|
@ -27,6 +27,144 @@ interface Repository
|
||||
* @return 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
|
||||
@ -39,19 +177,26 @@ interface Repository
|
||||
* @param Model $model
|
||||
* @return void
|
||||
*/
|
||||
public function save(Model &$model): 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;
|
||||
public function update(Model $model): void;
|
||||
/**
|
||||
* Create new Model with data
|
||||
* @param array $data
|
||||
* @return 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
|
||||
* @param Model $model
|
||||
|
@ -1,8 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Model;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Concept\Model;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Concept\Model\Factory as FactoryInterface;
|
||||
use ProVM\Concept\Model\Repository;
|
||||
|
||||
|
@ -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