20240423#5

This commit is contained in:
User
2024-04-23 05:41:46 +03:00
parent 98c2d37870
commit de10cbae31
2 changed files with 11 additions and 15 deletions

View File

@@ -2,6 +2,7 @@
namespace Base\Controllers; namespace Base\Controllers;
use Base\Domain\DomainException;
use Base\Services\DTOException; use Base\Services\DTOException;
use Base\Services\ServiceException; use Base\Services\ServiceException;
use Exception; use Exception;
@@ -28,7 +29,7 @@ abstract class AbstractPageController extends AbstractController {
*/ */
public function checkError(Throwable $e) : string { public function checkError(Throwable $e) : string {
($e instanceof Exception) ? $this->logException($e) : $this->logError($e); ($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"); return "Ошибка. Дата и время: ".date("d-m-Y H:i:s");
} }
} }

View File

@@ -8,13 +8,13 @@ use ReflectionClass;
abstract class AbstractDTO { abstract class AbstractDTO {
/** /**
* @throws Exception * @param array $data
* @return static
*/ */
static function fromArray(array $data) : static { static function fromArray(array $data) : static {
$class = new ReflectionClass(static::class); $class = new ReflectionClass(static::class);
$self = new static(); $self = new static();
foreach ($class->getProperties() as $property) { foreach ($class->getProperties() as $property) {
try {
$propertyNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName())); $propertyNameSnakeCase = strtolower(preg_replace("'([A-Z])'", "_$1", $property->getName()));
// data[propertyName] ?? data[property_name] ?? null // data[propertyName] ?? data[property_name] ?? null
$value = $data[$property->getName()] ?? $data[$propertyNameSnakeCase] ?? null; $value = $data[$property->getName()] ?? $data[$propertyNameSnakeCase] ?? null;
@@ -23,11 +23,6 @@ abstract class AbstractDTO {
// прямое присовение по умолчанию // прямое присовение по умолчанию
elseif(isset($value)) $self->{$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]));
}
return $self; return $self;
} }
} }