From 084736672e96b10118ee5d6f88928cf845a8fa88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 24 May 2021 18:24:10 +0200 Subject: [PATCH] Thunk support --- src/SEOManager.php | 11 ++++++----- tests/Pest/ManagerTest.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/SEOManager.php b/src/SEOManager.php index 7f59ae0..0860868 100644 --- a/src/SEOManager.php +++ b/src/SEOManager.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace ArchTech\SEO; +use Closure; use Illuminate\Support\Str; /** @@ -70,12 +71,12 @@ class SEOManager protected function modify(string $key): string|null { return isset($this->modifiers[$key]) - ? $this->modifiers[$key]($this->values[$key]) - : $this->values[$key]; + ? $this->modifiers[$key](value($this->values[$key])) + : value($this->values[$key]); } /** Set one or more values. */ - public function set(string|array $key, string|null $value = null): string|array|null + public function set(string|array $key, string|Closure|null $value = null): string|array|null { if (is_array($key)) { /** @var array $key */ @@ -103,9 +104,9 @@ class SEOManager { return isset($this->values[$key]) ? $this->modify($key) - : $this->defaults[$key] ?? ( + : value($this->defaults[$key] ?? ( Str::contains($key, '.') ? $this->get(Str::after($key, '.')) : null - ); + )); } /** Configure an extension. */ diff --git a/tests/Pest/ManagerTest.php b/tests/Pest/ManagerTest.php index eb0f697..df43b07 100644 --- a/tests/Pest/ManagerTest.php +++ b/tests/Pest/ManagerTest.php @@ -66,3 +66,15 @@ test('magic access gets converted to dot syntax', function () { expect(seo('abc.def'))->toBe('xyz'); expect(seo()->abcDef)->toBe('xyz'); }); + +test('thunks can be used as values', function () { + seo()->title(fn () => 'bar'); + + expect(seo('title'))->toBe('bar'); +}); + +test('thunks can be used as deafults', function () { + seo()->title(default: fn () => 'bar'); + + expect(seo('title'))->toBe('bar'); +});