Implemented repository mapper, and venta show
This commit is contained in:
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