77 lines
2.0 KiB
PHP
77 lines
2.0 KiB
PHP
![]() |
<?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;
|
||
|
}
|
||
|
}
|