This commit is contained in:
2024-04-22 13:31:05 -04:00
parent 7a88398379
commit 4e7ff1e508
6 changed files with 130 additions and 99 deletions

View File

@ -11,19 +11,10 @@ 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;
public function getConnection(): Connection public function getConnection(): Connection
{ {
@ -37,40 +28,6 @@ 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 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 save(Model &$model): void public function save(Model &$model): void
{ {
@ -84,22 +41,19 @@ abstract class Repository implements RepositoryInterface
} }
public function update(Model $model, Model $old): void public function update(Model $model, Model $old): void
{ {
$model_values = $this->valuesForUpdate($model); $mapper = $this->getMapper();
$old_values = $this->valuesForUpdate($old); $model_values = $mapper->mapModelToTable($model);
$old_values = $mapper->mapModelToTable($old);
$columns = []; $differences = array_diff_assoc($old_values, $model_values);
$values = []; $columns = array_map(function($key) {
foreach ($this->fieldsForUpdate() as $i => $column) { return "`{$key}` = ?";
if (isset($model_values[$i]) and $old_values[$i] !== $model_values[$i]) { }, array_keys($differences));
$columns []= "`{$column}` = ?"; $values = array_values($differences);
$values []= $model_values[$i];
}
}
if (count($columns) === 0) { if (count($columns) === 0) {
return; return;
} }
$values = array_values($values);
$values []= $old->{$this->idProperty()}(); $values []= $old->{$this->idProperty()}();
$query = $this->getQueryBuilder()->update($this->getTable())->set($columns)->where(["{$this->idField()}} = ?"]); $query = $this->getQueryBuilder()->update($this->getTable())->set($columns)->where(["{$this->idField()}} = ?"]);
$this->getConnection()->execute($query, $values); $this->getConnection()->execute($query, $values);
@ -115,8 +69,8 @@ abstract class Repository implements RepositoryInterface
} }
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->resetIndex();
$this->optimize(); $this->optimize();
} }
@ -131,13 +85,14 @@ abstract class Repository implements RepositoryInterface
} }
protected function insert(Model $model): void protected function insert(Model $model): void
{ {
$fields = $this->fieldsForInsert(); $mapper = $this->getMapper();
$fields = $mapper->getColumns();
$fields_string = array_map(function($field) { $fields_string = array_map(function($field) {
return "`{$field}`"; return "`{$field}`";
}, $fields); }, $fields);
$fields_questions = array_fill(0, count($fields), '?'); $fields_questions = array_fill(0, count($fields), '?');
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($fields_string)->values($fields_questions); $query = $this->getQueryBuilder()->insert($this->getTable())->columns($fields_string)->values($fields_questions);
$values = $this->valuesForInsert($model); $values = array_values($mapper->mapModelToTable($model));
$this->getConnection()->execute($query, $values); $this->getConnection()->execute($query, $values);
} }
protected function resetIndex(): void protected function resetIndex(): void
@ -197,6 +152,8 @@ abstract class Repository implements RepositoryInterface
return $this->fetchMany($query); return $this->fetchMany($query);
} }
abstract protected function getMapper(): Model\Mapper;
abstract protected function fieldsForUpdate(): array; abstract protected function fieldsForUpdate(): array;
abstract protected function fieldsForInsert(): array; abstract protected function fieldsForInsert(): array;
abstract protected function valuesForUpdate(Model $model): array; abstract protected function valuesForUpdate(Model $model): array;

View File

@ -2,7 +2,6 @@
namespace ProVM\Concept; namespace ProVM\Concept;
use JsonSerializable; use JsonSerializable;
use ProVM\Concept\Model\Repository;
interface Model extends JsonSerializable interface Model extends JsonSerializable
{ {

View 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;
}

View File

@ -28,44 +28,6 @@ interface Repository
*/ */
public function getTable(): string; public function getTable(): string;
/**
* @param Connection $connection
* @return Repository
*/
public function setConnection(Connection $connection): Repository;
/**
* @param QueryBuilder $builder
* @return Repository
*/
public function setQueryBuilder(QueryBuilder $builder): Repository;
/**
* @param Factory $factory
* @return Repository
*/
public function setFactory(Factory $factory): Repository;
/**
* @param string $model_class
* @return Repository
*/
public function setModel(string $model_class): Repository;
/**
* @param string $table
* @return Repository
*/
public function setTable(string $table): Repository;
/**
* Set up the Repository
* SHOULD CALL
* setTable
* setColumns [setRequired, setOptional]
* setProperties
* setMapping
* setModel
* @return Repository
*/
public function setup(): Repository;
/** /**
* Transform result array to Model * Transform result array to Model
* @param array $data * @param array $data

View 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;
}
}

View 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;
}
}