Skip to content

Commit

Permalink
Add the ability to nest props using dot notation
Browse files Browse the repository at this point in the history
  • Loading branch information
reinink committed Dec 3, 2020
1 parent 9134598 commit f316b80
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public function toResponse($request)
}
});

foreach ($props as $key => $value) {
if (str_contains($key, '.')) {
Arr::set($props, $key, $value);
unset($props[$key]);
}
}

$page = [
'component' => $this->component,
'props' => $props,
Expand Down
22 changes: 22 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,26 @@ public function test_lazy_props_are_included_in_partial_reload()
$this->assertObjectNotHasAttribute('users', $page->props);
$this->assertSame('A lazy value', $page->props->lazy);
}

public function test_can_nest_props_using_dot_notation()
{
$request = Request::create('/products/123', 'GET');

$props = [
'auth' => [
'user' => [
'name' => 'Jonathan Reinink',
]
],
'auth.user.can.deleteProducts' => true,
'product' => ['name' => 'My example product'],
];
$response = new Response('Products/Edit', $props, 'app', '123');
$response = $response->toResponse($request);
$view = $response->getOriginalContent();
$user = $view->getData()['page']['props']['auth']['user'];

$this->assertSame('Jonathan Reinink', $user['name']);
$this->assertTrue($user['can']['deleteProducts']);
}
}

0 comments on commit f316b80

Please sign in to comment.