diff --git a/src/Exceptions/CascadeDataNotFoundException.php b/src/Exceptions/CascadeDataNotFoundException.php new file mode 100644 index 0000000000..4a66bc61f0 --- /dev/null +++ b/src/Exceptions/CascadeDataNotFoundException.php @@ -0,0 +1,15 @@ +key = $key; + } +} diff --git a/src/Providers/ViewServiceProvider.php b/src/Providers/ViewServiceProvider.php index 28421a0253..1818212d84 100644 --- a/src/Providers/ViewServiceProvider.php +++ b/src/Providers/ViewServiceProvider.php @@ -160,6 +160,9 @@ public function registerBladeDirectives() Blade::directive('tags', function ($expression) { return ""; }); + Blade::directive('cascade', function ($expression) { + return ""; + }); } public function boot() diff --git a/src/View/Blade/CascadeDirective.php b/src/View/Blade/CascadeDirective.php new file mode 100644 index 0000000000..824bad9911 --- /dev/null +++ b/src/View/Blade/CascadeDirective.php @@ -0,0 +1,35 @@ +toArray(); + } + + if (! isset($keys)) { + return $data; + } + + return collect($keys) + ->mapWithKeys(function ($default, $key) use ($data) { + if (is_numeric($key)) { + $key = $default; + $default = null; + if (! array_key_exists($key, $data)) { + throw new CascadeDataNotFoundException($key); + } + } + + return [$key => Arr::get($data, $key, $default)]; + }) + ->all(); + } +} diff --git a/tests/View/Blade/CascadeDirectiveTest.php b/tests/View/Blade/CascadeDirectiveTest.php new file mode 100644 index 0000000000..7a6b185111 --- /dev/null +++ b/tests/View/Blade/CascadeDirectiveTest.php @@ -0,0 +1,80 @@ +assertEquals($expected, $data); + } + + #[Test] + public function it_gets_no_data() + { + $data = CascadeDirective::handle([]); + + $this->assertEmpty($data); + } + + #[Test] + public function it_gets_specific_data() + { + $data = CascadeDirective::handle([ + 'homepage', + 'is_homepage', + ]); + + $expected = Cascade::toArray(); + + $this->assertCount(2, $data); + $this->assertArrayHasKey('homepage', $data); + $this->assertArrayHasKey('is_homepage', $data); + $this->assertEquals($data['homepage'], $expected['homepage']); + $this->assertEquals($data['is_homepage'], $expected['is_homepage']); + } + + #[Test] + public function it_throws_exception_for_missing_data() + { + $this->expectException(CascadeDataNotFoundException::class); + $this->expectExceptionMessage('Cascade data [live_preview] not found'); + + CascadeDirective::handle([ + 'live_preview', + ]); + } + + #[Test] + public function it_uses_fallback_for_missing_data() + { + $data = CascadeDirective::handle([ + 'homepage', + 'live_preview' => false, + ]); + + $expected = Cascade::toArray(); + + $this->assertCount(2, $data); + $this->assertArrayHasKey('homepage', $data); + $this->assertArrayHasKey('live_preview', $data); + $this->assertEquals($data['homepage'], $expected['homepage']); + $this->assertEquals($data['live_preview'], false); + } +}