develop (#45)
Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl> Reviewed-on: #45
This commit is contained in:
@ -2,7 +2,9 @@
|
||||
namespace Incoviba\Common\Implement\Repository;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
|
||||
class Factory implements Define\Repository\Factory
|
||||
{
|
||||
@ -20,8 +22,16 @@ class Factory implements Define\Repository\Factory
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws EmptyResult
|
||||
*/
|
||||
public function run(): mixed
|
||||
{
|
||||
return call_user_func_array($this->callable, $this->args);
|
||||
try {
|
||||
return call_user_func_array($this->callable, $this->args);
|
||||
} catch (Exception $exception) {
|
||||
throw new EmptyResult($exception->getMessage(), $exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Repository;
|
||||
|
||||
use Error;
|
||||
use Closure;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
|
||||
class Mapper implements Define\Repository\Mapper
|
||||
{
|
||||
@ -46,7 +48,11 @@ class Mapper implements Define\Repository\Mapper
|
||||
}
|
||||
public function hasDefault(): bool
|
||||
{
|
||||
return isset($this->default);
|
||||
try {
|
||||
return isset($this->default) or $this->default === null;
|
||||
} catch (Error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function parse(Define\Model &$model, string $column, ?array $data): bool
|
||||
@ -62,10 +68,14 @@ class Mapper implements Define\Repository\Mapper
|
||||
}
|
||||
$value = $data[$column];
|
||||
if ($this->hasFunction()) {
|
||||
if ($value !== null) {
|
||||
try {
|
||||
$value = ($this->function)($data);
|
||||
} elseif ($this->hasDefault()) {
|
||||
$value = $this->default;
|
||||
} catch (EmptyResult $exception) {
|
||||
if ($this->hasDefault()) {
|
||||
$value = $this->default;
|
||||
} else {
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
$model->{$property} = $value;
|
||||
|
@ -2,6 +2,7 @@
|
||||
namespace Incoviba\Common\Implement\Repository\Mapper;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateMalformedStringException;
|
||||
use Incoviba\Common\Implement\Repository\Mapper;
|
||||
|
||||
class DateTime extends Mapper
|
||||
@ -9,7 +10,17 @@ class DateTime extends Mapper
|
||||
public function __construct(string $column, ?string $property = null)
|
||||
{
|
||||
$this->setFunction(function($data) use ($column) {
|
||||
return new DateTimeImmutable($data[$column] ?? '');
|
||||
if (!isset($data[$column])) {
|
||||
return null;
|
||||
}
|
||||
if (is_a($data[$column], DateTimeImmutable::class)) {
|
||||
return $data[$column];
|
||||
}
|
||||
try {
|
||||
return new DateTimeImmutable($data[$column] ?? '');
|
||||
} catch (DateMalformedStringException) {
|
||||
return new DateTimeImmutable();
|
||||
}
|
||||
});
|
||||
if ($property !== null) {
|
||||
$this->setProperty($property);
|
||||
|
Reference in New Issue
Block a user