Implemented repository mapper, and venta show
This commit is contained in:
@ -7,6 +7,19 @@ abstract class Model implements Define\Model
|
||||
{
|
||||
public int $id;
|
||||
|
||||
protected array $factories;
|
||||
|
||||
public function addFactory(string $property, Define\Repository\Factory $factory): Model
|
||||
{
|
||||
$this->factories[$property] = $factory;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function runFactory(string $property): mixed
|
||||
{
|
||||
return $this->factories[$property]->run();
|
||||
}
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return [
|
||||
|
@ -1,10 +1,11 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Ideal;
|
||||
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use PDO;
|
||||
use Incoviba\Common\Define\Model;
|
||||
use ReflectionProperty;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
|
||||
abstract class Repository implements Define\Repository
|
||||
{
|
||||
@ -21,14 +22,14 @@ abstract class Repository implements Define\Repository
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function load(array $data_row): Model
|
||||
public function load(array $data_row): Define\Model
|
||||
{
|
||||
$model = $this->create($data_row);
|
||||
$model->{$this->getKey()} = $data_row[$this->getKey()];
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function remove(Model $model): void
|
||||
public function remove(Define\Model $model): void
|
||||
{
|
||||
$query = "DELETE FROM `{$this->getTable()}` WHERE `{$this->getKey()}` = ?";
|
||||
$this->connection->execute($query, [$model->getId()]);
|
||||
@ -49,26 +50,50 @@ abstract class Repository implements Define\Repository
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
protected function parseData(Define\Model $model, ?array $data, array $data_map): Define\Model
|
||||
protected function parseData(Define\Model $model, ?array $data, Implement\Repository\MapperParser $data_map): Define\Model
|
||||
{
|
||||
if ($data === null) {
|
||||
return $model;
|
||||
}
|
||||
foreach ($data_map as $column => $settings) {
|
||||
if (isset($data[$column])) {
|
||||
$property = $column;
|
||||
if (isset($settings['property'])) {
|
||||
$property = $settings['property'];
|
||||
}
|
||||
$value = $data[$column];
|
||||
if (isset($settings['function'])) {
|
||||
$value = $settings['function']($data);
|
||||
}
|
||||
$model->{$property} = $value;
|
||||
foreach ($data_map->getColumns() as $column) {
|
||||
if (!$data_map->hasMapper($column)) {
|
||||
$this->parsePlainColumn($model, $column, $data);
|
||||
continue;
|
||||
}
|
||||
|
||||
$settings = $data_map->getMapper($column);
|
||||
if ($settings->parse($model, $column, $data)) {
|
||||
continue;
|
||||
}
|
||||
$property = $column;
|
||||
if ($settings->hasProperty()) {
|
||||
$property = $settings->property;
|
||||
}
|
||||
$this->setDefault($model, $property);
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
protected function parsePlainColumn(Define\Model &$model, string $column, ?array $data): void
|
||||
{
|
||||
$property = $column;
|
||||
if (isset($data[$column])) {
|
||||
$model->{$property} = $data[$column];
|
||||
return;
|
||||
}
|
||||
$this->setDefault($model, $property);
|
||||
}
|
||||
protected function setDefault(Define\Model &$model, string $property): void
|
||||
{
|
||||
$prop = new ReflectionProperty($model, $property);
|
||||
$type = $prop->getType()->getName();
|
||||
$value = match ($type) {
|
||||
'int' => 0,
|
||||
'float' => 0.0,
|
||||
'string' => '',
|
||||
default => null,
|
||||
};
|
||||
$model->{$property} = $value;
|
||||
}
|
||||
protected function saveNew(array $columns, array $values): int
|
||||
{
|
||||
$columns_string = implode(', ', array_map(function($column) {return "`{$column}`";}, $columns));
|
||||
|
Reference in New Issue
Block a user