diff --git a/src/Support/ValueFactory.php b/src/Support/ValueFactory.php index 8229b35..efebe48 100644 --- a/src/Support/ValueFactory.php +++ b/src/Support/ValueFactory.php @@ -24,11 +24,13 @@ class ValueFactory return $mapper($rawValue); } - if (is_array($rawValue)) { - return App::makeWith($className, $rawValue); + // Plain values or numeric arrays are passed as a single parameter to the constructor + if (!is_array($rawValue) || array_key_exists(0, $rawValue)) { + return new $className($className); } - return new $className($rawValue); + // Associative arrays leverage Laravel service container + return App::makeWith($className, $rawValue); } public static function resolveTypedValue(mixed $rawValue, Type $type): mixed diff --git a/tests/Classes/ObjectWithoutMapper.php b/tests/Classes/ObjectWithoutMapper.php index 4c37d78..82342e3 100644 --- a/tests/Classes/ObjectWithoutMapper.php +++ b/tests/Classes/ObjectWithoutMapper.php @@ -4,8 +4,8 @@ declare(strict_types=1); namespace Tests\Classes; +use Carbon\Carbon; use Icefox\DTO\DataObject; -use Illuminate\Support\Carbon; readonly class ObjectWithoutMapper { diff --git a/tests/DataObjectTest.php b/tests/DataObjectTest.php index 9514994..1ed2ce4 100644 --- a/tests/DataObjectTest.php +++ b/tests/DataObjectTest.php @@ -198,11 +198,6 @@ test('failed validation throws ValidationException', function () { })->throws(ValidationException::class); -test('tries to resolve without mapper', function () { - $object = ObjectWithoutMapper::fromArray(['date' => '1990-04-01']); - expect($object->date->isSameDay('1990-04-01'))->toBeTrue(); -})->group('object-without-mapper'); - test('creates collection', function () { $object = CollectionDataObject::fromArray([ 'values' => [