This commit is contained in:
icefox 2026-02-18 20:18:39 -03:00
parent bef42b3352
commit afb47c1977
No known key found for this signature in database
7 changed files with 56 additions and 26 deletions

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Icefox\DTO\Support;
use Icefox\DTO\Attributes\CastWith;
use Icefox\DTO\Config;
use Illuminate\Support\Facades\App;
use ReflectionNamedType;
use ReflectionParameter;
@ -20,7 +22,7 @@ class ValueFactory
{
public static function constructObject(string $className, mixed $rawValue): object
{
if ($mapper = config('dto.mappers.' . $className, null)) {
if ($mapper = Config::getCaster($className)) {
return $mapper($rawValue);
}
@ -83,21 +85,32 @@ class ValueFactory
return null;
}
if (is_null($type)) {
$reflectedType = $reflection->getType();
if ($reflectedType instanceof ReflectionNamedType && $name = $reflectedType->getName()) {
return match ($name) {
'string' => $rawValue,
'bool' => boolval($rawValue),
'int' => intval($rawValue),
'float' => floatval($rawValue),
'array' => $rawValue,
default => self::constructObject($name, $rawValue),
};
}
return $rawValue;
$castWithAttrs = $reflection->getAttributes(CastWith::class);
if ($withCast = $reflection->getAttributes(CastWith::class)[0] ?? null) {
$caster = $withCast->newInstance()->class;
return App::call("$caster@cast", ['value' => $rawValue]);
}
return self::resolveTypedValue($rawValue, $type);
if (!is_null($type)) {
return self::resolveTypedValue($rawValue, $type);
}
$reflectedType = $reflection->getType();
if ($reflectedType instanceof ReflectionNamedType && $name = $reflectedType->getName()) {
if ($caster = Config::getCaster($name)) {
return App::call($caster, ['value' => $rawValue]);
}
return match ($name) {
'string' => $rawValue,
'bool' => boolval($rawValue),
'int' => intval($rawValue),
'float' => floatval($rawValue),
'array' => $rawValue,
default => self::constructObject($name, $rawValue),
};
}
return $rawValue;
}
}