20240423#5
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Base\Controllers;
|
||||
|
||||
use Base\Domain\DomainException;
|
||||
use Base\Services\DTOException;
|
||||
use Base\Services\ServiceException;
|
||||
use Exception;
|
||||
@@ -28,7 +29,7 @@ abstract class AbstractPageController extends AbstractController {
|
||||
*/
|
||||
public function checkError(Throwable $e) : string {
|
||||
($e instanceof Exception) ? $this->logException($e) : $this->logError($e);
|
||||
if($e instanceof DTOException || $e instanceof ServiceException) return $e->getMessage();
|
||||
if($e instanceof DTOException || $e instanceof DomainException || $e instanceof ServiceException) return $e->getMessage();
|
||||
return "Ошибка. Дата и время: ".date("d-m-Y H:i:s");
|
||||
}
|
||||
}
|
||||
@@ -8,25 +8,20 @@ use ReflectionClass;
|
||||
abstract class AbstractDTO {
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @param array $data
|
||||
* @return static
|
||||
*/
|
||||
static function fromArray(array $data) : static {
|
||||
$class = new ReflectionClass(static::class);
|
||||
$self = new static();
|
||||
foreach ($class->getProperties() as $property) {
|
||||
try {
|
||||
$propertyNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()));
|
||||
// data[propertyName] ?? data[property_name] ?? null
|
||||
$value = $data[$property->getName()] ?? $data[$propertyNameSnakeCase] ?? null;
|
||||
// если есть внутренний метод (приоритетная обработка)
|
||||
if($class->hasMethod('set'.ucfirst($property->getName()))) $self->{'set'.ucfirst($property->getName())}($value);
|
||||
// прямое присовение по умолчанию
|
||||
elseif(isset($value)) $self->{$property->getName()} = $value;
|
||||
}
|
||||
catch (\Error $error) {
|
||||
$errorKey[$property->getName()] = $error->getMessage();
|
||||
}
|
||||
if(!empty($errorKey)) throw new Exception(json_encode(["data"=>$data, "error"=>$errorKey]));
|
||||
$propertyNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()));
|
||||
// data[propertyName] ?? data[property_name] ?? null
|
||||
$value = $data[$property->getName()] ?? $data[$propertyNameSnakeCase] ?? null;
|
||||
// если есть внутренний метод (приоритетная обработка)
|
||||
if($class->hasMethod('set'.ucfirst($property->getName()))) $self->{'set'.ucfirst($property->getName())}($value);
|
||||
// прямое присовение по умолчанию
|
||||
elseif(isset($value)) $self->{$property->getName()} = $value;
|
||||
}
|
||||
return $self;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user