misc: DTO 에러에서 name을 포함하여 알림

This commit is contained in:
2022-07-09 12:13:15 +09:00
parent 368f31d1fa
commit c2e956a639
8 changed files with 20 additions and 20 deletions
+3 -3
View File
@@ -27,12 +27,12 @@ class Convert
return $this; return $this;
} }
public function convertFrom(string|array|int|float|bool|null $raw): mixed public function convertFrom(string|array|int|float|bool|null $raw, string $name): mixed
{ {
if($this->converter === null){ if($this->converter === null){
throw new \Exception('converter is not set'); throw new \Exception("converter[{$name}] is not set");
} }
return $this->converter->convertFrom($raw); return $this->converter->convertFrom($raw, $name);
} }
public function convertTo(mixed $target): string|array|int|float|bool|null { public function convertTo(mixed $target): string|array|int|float|bool|null {
+3 -3
View File
@@ -19,15 +19,15 @@ class ArrayConverter implements Converter
$this->itemConverter = new $itemConverterClass($itemTypes, ...$args); $this->itemConverter = new $itemConverterClass($itemTypes, ...$args);
} }
public function convertFrom(string|array|int|float|bool|null $raw): mixed public function convertFrom(string|array|int|float|bool|null $raw, string $name): mixed
{ {
if ($raw === null && array_search('null', $this->types, true) !== false) { if ($raw === null && array_search('null', $this->types, true) !== false) {
return null; return null;
} }
if (!is_array($raw) || !array_is_list($raw)) { if (!is_array($raw) || !array_is_list($raw)) {
throw new \Exception('value is not a array'); throw new \Exception("value is not a array: $name");
} }
return array_map(fn ($v) => $this->itemConverter->convertFrom($v), $raw); return array_map(fn ($v) => $this->itemConverter->convertFrom($v, $name), $raw);
} }
public function convertTo(mixed $data): string|array|int|float|bool|null public function convertTo(mixed $data): string|array|int|float|bool|null
+1 -1
View File
@@ -2,6 +2,6 @@
namespace sammo\DTO\Converter; namespace sammo\DTO\Converter;
interface Converter{ interface Converter{
public function convertFrom(string|array|int|float|bool|null $raw): mixed; public function convertFrom(string|array|int|float|bool|null $raw, string $name): mixed;
public function convertTo(mixed $data): string|array|int|float|bool|null; public function convertTo(mixed $data): string|array|int|float|bool|null;
} }
@@ -50,13 +50,13 @@ class DateTimeConverter implements Converter
return new \DateTimeZone($timezone); return new \DateTimeZone($timezone);
} }
public function convertFrom(string|array|int|float|bool|null $raw): mixed public function convertFrom(string|array|int|float|bool|null $raw, string $name): mixed
{ {
if ($raw === null && array_search('null', $this->types, true) !== false) { if ($raw === null && array_search('null', $this->types, true) !== false) {
return null; return null;
} }
if (!is_string($raw)) { if (!is_string($raw)) {
throw new \InvalidArgumentException('DateTimeConverter can not convert non-string'); throw new \InvalidArgumentException("DateTimeConverter can not convert non-string: {$name}");
} }
if (array_search('DateTimeImmutable', $this->types, true) !== false) { if (array_search('DateTimeImmutable', $this->types, true) !== false) {
$objDateTime = new \DateTimeImmutable($raw, $this->timeZoneOffset); $objDateTime = new \DateTimeImmutable($raw, $this->timeZoneOffset);
+6 -6
View File
@@ -10,7 +10,7 @@ class DefaultConverter implements Converter
{ {
} }
public static function convertFromItem(string $type, $raw, bool &$success): mixed public static function convertFromItem(string $type, $raw, string $name, bool &$success): mixed
{ {
$success = false; $success = false;
if (is_subclass_of($type, \UnitEnum::class)) { if (is_subclass_of($type, \UnitEnum::class)) {
@@ -48,7 +48,7 @@ class DefaultConverter implements Converter
return null; return null;
} }
if (!array_is_list($raw)) { if (!array_is_list($raw)) {
throw new \Exception('value is not a array'); throw new \Exception("value is not a array: $name");
} }
foreach ($raw as $value) { foreach ($raw as $value) {
if (is_int($value) || is_float($value) || is_string($value)) { if (is_int($value) || is_float($value) || is_string($value)) {
@@ -57,7 +57,7 @@ class DefaultConverter implements Converter
if (is_bool($value) || is_null($value)) { if (is_bool($value) || is_null($value)) {
continue; continue;
} }
throw new \Exception('DefaultConverter can not convert array'); throw new \Exception("DefaultConverter can not convert array: $name");
} }
$success = true; $success = true;
return $raw; return $raw;
@@ -93,7 +93,7 @@ class DefaultConverter implements Converter
return null; return null;
} }
public function convertFrom(string|array|int|float|bool|null $raw): mixed public function convertFrom(string|array|int|float|bool|null $raw, string $name): mixed
{ {
if ($raw === null && array_search('null', $this->types, true) !== false) { if ($raw === null && array_search('null', $this->types, true) !== false) {
return null; return null;
@@ -101,13 +101,13 @@ class DefaultConverter implements Converter
foreach ($this->types as $type) { foreach ($this->types as $type) {
$success = false; $success = false;
$value = self::convertFromItem($type, $raw, $success); $value = self::convertFromItem($type, $raw, $name, $success);
if ($success) { if ($success) {
return $value; return $value;
} }
} }
throw new \Exception('DefaultConverter can not convert '.gettype($raw)); throw new \Exception('DefaultConverter can not convert '.gettype($raw).": $name");
} }
public function convertTo(mixed $data): string|array|int|float|bool|null public function convertTo(mixed $data): string|array|int|float|bool|null
+3 -3
View File
@@ -20,18 +20,18 @@ class MapConverter implements Converter
$this->itemConverter = new $itemConverterClass($itemTypes, ...$args); $this->itemConverter = new $itemConverterClass($itemTypes, ...$args);
} }
public function convertFrom(string|array|int|float|bool|null $raw): mixed public function convertFrom(string|array|int|float|bool|null $raw, string $name): mixed
{ {
if ($raw === null && array_search('null', $this->types, true) !== false) { if ($raw === null && array_search('null', $this->types, true) !== false) {
return null; return null;
} }
if (!is_array($raw)) { if (!is_array($raw)) {
throw new \Exception('value is not a array'); throw new \Exception("value is not a array: $name");
} }
$result = []; $result = [];
foreach ($raw as $key => $value) { foreach ($raw as $key => $value) {
$result[$key] = $this->itemConverter->convertFrom($value); $result[$key] = $this->itemConverter->convertFrom($value, $name);
} }
return $result; return $result;
} }
+1 -1
View File
@@ -94,7 +94,7 @@ abstract class DTO
} else { } else {
$converter = new Convert(DefaultConverter::class); $converter = new Convert(DefaultConverter::class);
} }
$value = $converter->setType($propTypes)->convertFrom($value); $value = $converter->setType($propTypes)->convertFrom($value, $name);
if($param !== null){ if($param !== null){
$args[$name] = $value; $args[$name] = $value;
+1 -1
View File
@@ -85,7 +85,7 @@ class ConverterDouble implements Converter
public function __construct(array $types, ...$args) public function __construct(array $types, ...$args)
{ {
} }
public function convertFrom(string|array|int|float|bool|null $raw): mixed public function convertFrom(string|array|int|float|bool|null $raw, string $name): mixed
{ {
return $raw * 2; return $raw * 2;
} }