Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/support/src/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,10 @@ protected static function getDependencyFromUnionType(ReflectionUnionType $type):
{
foreach ($type->getTypes() as $namedType) {
$className = $namedType->getName();
if (is_subclass_of($className, DataObject::class)
|| is_subclass_of($className, DateTimeInterface::class)) {
if (
is_subclass_of($className, DataObject::class)
|| is_subclass_of($className, DateTimeInterface::class)
) {
return $namedType;
}
}
Expand Down Expand Up @@ -284,10 +286,18 @@ protected static function resolveDependenciesMap(string $class, array &$visited
];
continue;
}
$dataKey = static::isAutoCasting()
? static::convertPropertyToDataKey($property->getName())
: $property->getName();
if (enum_exists($typeName)) {
$result[$dataKey] = [
'handler' => [$typeName, 'from'],
'nullable' => $allowsNull,
'children' => [],
];
continue;
}
if ($resolver = $customizedDependencies[$typeName] ?? null) {
$dataKey = static::isAutoCasting()
? static::convertPropertyToDataKey($property->getName())
: $property->getName();
$result[$dataKey] = [
'handler' => $resolver,
'nullable' => $allowsNull,
Expand Down
15 changes: 15 additions & 0 deletions tests/Support/DataObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ public function testMakeWithoutAutoResolve(): void
'city' => 'New York',
'zipCode' => '10001',
],
'gender' => TestGenderEnum::Male,
'created_at' => '2023-01-01 12:00:00',
];

Expand All @@ -265,6 +266,7 @@ public function testMakeWithoutAutoResolve(): void
$this->assertSame(['street' => '123 Main St', 'city' => 'New York', 'zipCode' => '10001'], $user->address);
$this->assertIsString($user->createdAt);
$this->assertSame('2023-01-01 12:00:00', $user->createdAt);
$this->assertSame(TestGenderEnum::Male, $user->gender);
}

/**
Expand All @@ -279,6 +281,7 @@ public function testMakeWithAutoResolveDataObject(): void
'city' => 'New York',
'zip_code' => '10001',
],
'gender' => 'male',
'created_at' => '2023-01-01 12:00:00',
];

Expand All @@ -291,6 +294,7 @@ public function testMakeWithAutoResolveDataObject(): void
$this->assertSame('10001', $user->address->zipCode);
$this->assertInstanceOf(DateTime::class, $user->createdAt);
$this->assertSame('2023-01-01 12:00:00', $user->createdAt->format('Y-m-d H:i:s'));
$this->assertSame(TestGenderEnum::Male, $user->gender);
}

/**
Expand All @@ -307,6 +311,7 @@ public function testMakeWithAutoResolveDeepNesting(): void
'city' => 'Boston',
'zip_code' => '02101',
],
'gender' => 'male',
'created_at' => '2023-06-15 09:30:00',
],
];
Expand All @@ -320,6 +325,7 @@ public function testMakeWithAutoResolveDeepNesting(): void
$this->assertSame('456 Oak Ave', $company->employee->address->street);
$this->assertSame('Boston', $company->employee->address->city);
$this->assertInstanceOf(DateTime::class, $company->employee->createdAt);
$this->assertSame(TestGenderEnum::Male, $company->employee->gender);
}

/**
Expand All @@ -331,13 +337,15 @@ public function testMakeWithAutoResolveNullValues(): void
'name' => 'John Doe',
'address' => null,
'created_at' => null,
'gender' => 'male',
];

$user = TestUserDataObject::make($data, true);

$this->assertSame('John Doe', $user->name);
$this->assertNull($user->address);
$this->assertNull($user->createdAt);
$this->assertSame(TestGenderEnum::Male, $user->gender);
}

protected function getData(): array
Expand Down Expand Up @@ -421,6 +429,7 @@ class TestUserDataObject extends DataObject
{
public function __construct(
public string $name,
public TestGenderEnum $gender,
public TestAddressDataObject|array|null $address,
public DateTime|string|null $createdAt,
) {
Expand All @@ -438,3 +447,9 @@ public function __construct(
) {
}
}

enum TestGenderEnum: string
{
case Male = 'male';
case Female = 'female';
}