From f316b80d55f51e0bf6f69803ddfb5aa14a142d8b Mon Sep 17 00:00:00 2001 From: Jonathan Reinink Date: Thu, 3 Dec 2020 10:15:30 -0500 Subject: [PATCH] Add the ability to nest props using dot notation --- src/Response.php | 7 +++++++ tests/ResponseTest.php | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/Response.php b/src/Response.php index 528dcfea..84fe197d 100644 --- a/src/Response.php +++ b/src/Response.php @@ -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, diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 4a263eac..0002be5c 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -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']); + } }