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
{
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 Connection $connection;
protected QueryBuilder $builder;
protected Factory $factory;
protected string $model;
protected string $table;
public function getConnection(): Connection
{
@ -37,40 +28,6 @@ abstract class Repository implements RepositoryInterface
{
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
{
@ -84,22 +41,19 @@ abstract class Repository implements RepositoryInterface
}
public function update(Model $model, Model $old): void
{
$model_values = $this->valuesForUpdate($model);
$old_values = $this->valuesForUpdate($old);
$mapper = $this->getMapper();
$model_values = $mapper->mapModelToTable($model);
$old_values = $mapper->mapModelToTable($old);
$columns = [];
$values = [];
foreach ($this->fieldsForUpdate() as $i => $column) {
if (isset($model_values[$i]) and $old_values[$i] !== $model_values[$i]) {
$columns []= "`{$column}` = ?";
$values []= $model_values[$i];
}
}
$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;
}
$values = array_values($values);
$values []= $old->{$this->idProperty()}();
$query = $this->getQueryBuilder()->update($this->getTable())->set($columns)->where(["{$this->idField()}} = ?"]);
$this->getConnection()->execute($query, $values);
@ -115,8 +69,8 @@ abstract class Repository implements RepositoryInterface
}
public function delete(Model $model): void
{
$query = $this->getQueryBuilder()->delete($this->getTable())->where(['id = ?']);
$this->getConnection()->execute($query, [$model->getId()]);
$query = $this->getQueryBuilder()->delete($this->getTable())->where(["{$this->idField()} = ?"]);
$this->getConnection()->execute($query, [$model->{$this->idProperty()}()]);
$this->resetIndex();
$this->optimize();
}
@ -131,13 +85,14 @@ abstract class Repository implements RepositoryInterface
}
protected function insert(Model $model): void
{
$fields = $this->fieldsForInsert();
$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 = $this->valuesForInsert($model);
$values = array_values($mapper->mapModelToTable($model));
$this->getConnection()->execute($query, $values);
}
protected function resetIndex(): void
@ -197,6 +152,8 @@ abstract class Repository implements RepositoryInterface
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;

View File

@ -2,7 +2,6 @@
namespace ProVM\Concept;
use JsonSerializable;
use ProVM\Concept\Model\Repository;
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;
/**
* @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
* @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;
}
}