forked from devsam/core
misc: DTO 에러에서 name을 포함하여 알림
This commit is contained in:
@@ -27,12 +27,12 @@ class Convert
|
||||
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){
|
||||
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 {
|
||||
|
||||
@@ -19,15 +19,15 @@ class ArrayConverter implements Converter
|
||||
$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) {
|
||||
return null;
|
||||
}
|
||||
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
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
namespace sammo\DTO\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;
|
||||
}
|
||||
@@ -50,13 +50,13 @@ class DateTimeConverter implements Converter
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
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) {
|
||||
$objDateTime = new \DateTimeImmutable($raw, $this->timeZoneOffset);
|
||||
|
||||
@@ -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;
|
||||
if (is_subclass_of($type, \UnitEnum::class)) {
|
||||
@@ -48,7 +48,7 @@ class DefaultConverter implements Converter
|
||||
return null;
|
||||
}
|
||||
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) {
|
||||
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)) {
|
||||
continue;
|
||||
}
|
||||
throw new \Exception('DefaultConverter can not convert array');
|
||||
throw new \Exception("DefaultConverter can not convert array: $name");
|
||||
}
|
||||
$success = true;
|
||||
return $raw;
|
||||
@@ -93,7 +93,7 @@ class DefaultConverter implements Converter
|
||||
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) {
|
||||
return null;
|
||||
@@ -101,13 +101,13 @@ class DefaultConverter implements Converter
|
||||
|
||||
foreach ($this->types as $type) {
|
||||
$success = false;
|
||||
$value = self::convertFromItem($type, $raw, $success);
|
||||
$value = self::convertFromItem($type, $raw, $name, $success);
|
||||
if ($success) {
|
||||
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
|
||||
|
||||
@@ -20,18 +20,18 @@ class MapConverter implements Converter
|
||||
$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) {
|
||||
return null;
|
||||
}
|
||||
if (!is_array($raw)) {
|
||||
throw new \Exception('value is not a array');
|
||||
throw new \Exception("value is not a array: $name");
|
||||
}
|
||||
|
||||
$result = [];
|
||||
foreach ($raw as $key => $value) {
|
||||
$result[$key] = $this->itemConverter->convertFrom($value);
|
||||
$result[$key] = $this->itemConverter->convertFrom($value, $name);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ abstract class DTO
|
||||
} else {
|
||||
$converter = new Convert(DefaultConverter::class);
|
||||
}
|
||||
$value = $converter->setType($propTypes)->convertFrom($value);
|
||||
$value = $converter->setType($propTypes)->convertFrom($value, $name);
|
||||
|
||||
if($param !== null){
|
||||
$args[$name] = $value;
|
||||
|
||||
+1
-1
@@ -85,7 +85,7 @@ class ConverterDouble implements Converter
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user