Implemented repository mapper, and venta show
This commit is contained in:
9
app/common/Define/Repository/Factory.php
Normal file
9
app/common/Define/Repository/Factory.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Repository;
|
||||
|
||||
interface Factory
|
||||
{
|
||||
public function setCallable(callable $callable): Factory;
|
||||
public function setArgs(array $args): Factory;
|
||||
public function run(): mixed;
|
||||
}
|
15
app/common/Define/Repository/Mapper.php
Normal file
15
app/common/Define/Repository/Mapper.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Repository;
|
||||
|
||||
interface Mapper
|
||||
{
|
||||
public function setProperty(string $property): Mapper;
|
||||
public function setFunction(callable $function): Mapper;
|
||||
public function setFactory(Factory $factory): Mapper;
|
||||
public function setDefault(mixed $value): Mapper;
|
||||
|
||||
public function hasProperty(): bool;
|
||||
public function hasFunction(): bool;
|
||||
public function hasFactory(): bool;
|
||||
public function hasDefault(): bool;
|
||||
}
|
10
app/common/Define/Repository/MapperParser.php
Normal file
10
app/common/Define/Repository/MapperParser.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Repository;
|
||||
|
||||
interface MapperParser
|
||||
{
|
||||
public function register(string $column, ?Mapper $mapper = null): MapperParser;
|
||||
public function getColumns(): array;
|
||||
public function hasMapper(string $column): bool;
|
||||
public function getMapper(string $column): Mapper;
|
||||
}
|
@ -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));
|
||||
|
27
app/common/Implement/Repository/Factory.php
Normal file
27
app/common/Implement/Repository/Factory.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Repository;
|
||||
|
||||
use Closure;
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
class Factory implements Define\Repository\Factory
|
||||
{
|
||||
public Closure $callable;
|
||||
public array $args;
|
||||
|
||||
public function setCallable(callable $callable): Define\Repository\Factory
|
||||
{
|
||||
$this->callable = $callable(...);
|
||||
return $this;
|
||||
}
|
||||
public function setArgs(array $args): Define\Repository\Factory
|
||||
{
|
||||
$this->args = $args;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function run(): mixed
|
||||
{
|
||||
return call_user_func_array($this->callable, $this->args);
|
||||
}
|
||||
}
|
76
app/common/Implement/Repository/Mapper.php
Normal file
76
app/common/Implement/Repository/Mapper.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Repository;
|
||||
|
||||
use Closure;
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
class Mapper implements Define\Repository\Mapper
|
||||
{
|
||||
public string $property;
|
||||
public Closure $function;
|
||||
public Define\Repository\Factory $factory;
|
||||
public mixed $default;
|
||||
|
||||
public function setProperty(string $property): Define\Repository\Mapper
|
||||
{
|
||||
$this->property = $property;
|
||||
return $this;
|
||||
}
|
||||
public function setFunction(callable $function): Define\Repository\Mapper
|
||||
{
|
||||
$this->function = $function(...);
|
||||
return $this;
|
||||
}
|
||||
public function setFactory(Define\Repository\Factory $factory): Define\Repository\Mapper
|
||||
{
|
||||
$this->factory = $factory;
|
||||
return $this;
|
||||
}
|
||||
public function setDefault(mixed $value): Define\Repository\Mapper
|
||||
{
|
||||
$this->default = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasProperty(): bool
|
||||
{
|
||||
return isset($this->property);
|
||||
}
|
||||
public function hasFunction(): bool
|
||||
{
|
||||
return isset($this->function);
|
||||
}
|
||||
public function hasFactory(): bool
|
||||
{
|
||||
return isset($this->factory);
|
||||
}
|
||||
public function hasDefault(): bool
|
||||
{
|
||||
return isset($this->default);
|
||||
}
|
||||
|
||||
public function parse(Define\Model &$model, string $column, ?array $data): bool
|
||||
{
|
||||
$property = $column;
|
||||
if ($this->hasProperty()) {
|
||||
$property = $this->property;
|
||||
}
|
||||
if (isset($data[$column])) {
|
||||
if ($this->hasFactory()) {
|
||||
$model->addFactory($property, $this->factory);
|
||||
return true;
|
||||
}
|
||||
$value = $data[$column];
|
||||
if ($this->hasFunction()) {
|
||||
$value = ($this->function)($data);
|
||||
}
|
||||
$model->{$property} = $value;
|
||||
return true;
|
||||
}
|
||||
if ($this->hasDefault()) {
|
||||
$model->{$property} = $this->default;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
17
app/common/Implement/Repository/Mapper/Boolean.php
Normal file
17
app/common/Implement/Repository/Mapper/Boolean.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Repository\Mapper;
|
||||
|
||||
use Incoviba\Common\Implement\Repository\Mapper;
|
||||
|
||||
class Boolean extends Mapper
|
||||
{
|
||||
public function __construct(string $column, ?string $property = null)
|
||||
{
|
||||
$this->setFunction(function($data) use ($column) {
|
||||
return $data[$column] !== 0;
|
||||
});
|
||||
if ($property !== null) {
|
||||
$this->setProperty($property);
|
||||
}
|
||||
}
|
||||
}
|
18
app/common/Implement/Repository/Mapper/DateTime.php
Normal file
18
app/common/Implement/Repository/Mapper/DateTime.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Repository\Mapper;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Incoviba\Common\Implement\Repository\Mapper;
|
||||
|
||||
class DateTime extends Mapper
|
||||
{
|
||||
public function __construct(string $column, ?string $property = null)
|
||||
{
|
||||
$this->setFunction(function($data) use ($column) {
|
||||
return new DateTimeImmutable($data[$column]);
|
||||
});
|
||||
if ($property !== null) {
|
||||
$this->setProperty($property);
|
||||
}
|
||||
}
|
||||
}
|
41
app/common/Implement/Repository/MapperParser.php
Normal file
41
app/common/Implement/Repository/MapperParser.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Repository;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Define\Repository\Mapper;
|
||||
|
||||
class MapperParser implements Define\Repository\MapperParser
|
||||
{
|
||||
public function __construct(?array $columns = null)
|
||||
{
|
||||
if ($columns !== null) {
|
||||
foreach ($columns as $column) {
|
||||
$this->register($column);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected array $maps;
|
||||
|
||||
public function register(string $column, ?Mapper $mapper = null): Define\Repository\MapperParser
|
||||
{
|
||||
if ($mapper !== null) {
|
||||
$this->maps[$column] = $mapper;
|
||||
return $this;
|
||||
}
|
||||
$this->maps[$column] = [];
|
||||
return $this;
|
||||
}
|
||||
public function getColumns(): array
|
||||
{
|
||||
return array_keys($this->maps);
|
||||
}
|
||||
public function hasMapper(string $column): bool
|
||||
{
|
||||
return is_a($this->maps[$column], Define\Repository\Mapper::class);
|
||||
}
|
||||
public function getMapper(string $column): Mapper
|
||||
{
|
||||
return $this->maps[$column];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user