Skip to content

Commit 9d29544

Browse files
authored
Merge pull request #10 from apple-x-co/bracket-object
Support bracket object
2 parents 74d0fe6 + faf0f06 commit 9d29544

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/InputQuery.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,18 @@ private function resolveObjectType(ReflectionParameter $param, array $query, arr
263263
return $arrayObjectResult;
264264
}
265265

266+
// Check if the parameter name exists as a direct nested array in query
267+
if (array_key_exists($paramName, $query) && is_array($query[$paramName])) {
268+
// This should never fail as $className comes from ReflectionParameter type info
269+
assert(class_exists($className), "Internal logic error: Class '$className' from reflection should exist");
270+
271+
/** @var Query $nestedQuery */
272+
$nestedQuery = $query[$paramName];
273+
274+
/** @var class-string<T> $className */
275+
return $this->newInstance($className, $nestedQuery);
276+
}
277+
266278
// Regular object type with #[Input] - create nested
267279
$nestedQuery = $this->extractNestedQuery($paramName, $query);
268280

tests/InputQueryTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,26 @@ public function testCreateNestedObject(): void
118118
$this->assertSame('john@example.com', $todo->author->email);
119119
}
120120

121+
public function testCreateBracketObject(): void
122+
{
123+
$query = [
124+
'title' => 'Buy milk',
125+
'author' => [
126+
'name' => 'John',
127+
'email' => 'john@example.com',
128+
],
129+
];
130+
131+
$todo = $this->inputQuery->newInstance(TodoInput::class, $query);
132+
133+
$this->assertInstanceOf(TodoInput::class, $todo);
134+
/** @var TodoInput $todo */
135+
$this->assertSame('Buy milk', $todo->title);
136+
$this->assertInstanceOf(AuthorInput::class, $todo->author);
137+
$this->assertSame('John', $todo->author->name);
138+
$this->assertSame('john@example.com', $todo->author->email);
139+
}
140+
121141
public function testCreateMixedInputAndDI(): void
122142
{
123143
$query = [

0 commit comments

Comments
 (0)