Skip to content

Commit f316b80

Browse files
committed
Add the ability to nest props using dot notation
1 parent 9134598 commit f316b80

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Response.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ public function toResponse($request)
7676
}
7777
});
7878

79+
foreach ($props as $key => $value) {
80+
if (str_contains($key, '.')) {
81+
Arr::set($props, $key, $value);
82+
unset($props[$key]);
83+
}
84+
}
85+
7986
$page = [
8087
'component' => $this->component,
8188
'props' => $props,

tests/ResponseTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,26 @@ public function test_lazy_props_are_included_in_partial_reload()
219219
$this->assertObjectNotHasAttribute('users', $page->props);
220220
$this->assertSame('A lazy value', $page->props->lazy);
221221
}
222+
223+
public function test_can_nest_props_using_dot_notation()
224+
{
225+
$request = Request::create('/products/123', 'GET');
226+
227+
$props = [
228+
'auth' => [
229+
'user' => [
230+
'name' => 'Jonathan Reinink',
231+
]
232+
],
233+
'auth.user.can.deleteProducts' => true,
234+
'product' => ['name' => 'My example product'],
235+
];
236+
$response = new Response('Products/Edit', $props, 'app', '123');
237+
$response = $response->toResponse($request);
238+
$view = $response->getOriginalContent();
239+
$user = $view->getData()['page']['props']['auth']['user'];
240+
241+
$this->assertSame('Jonathan Reinink', $user['name']);
242+
$this->assertTrue($user['can']['deleteProducts']);
243+
}
222244
}

0 commit comments

Comments
 (0)