1
0
Fork 0
mirror of https://github.com/archtechx/laravel-seo.git synced 2025-12-12 01:44:03 +00:00

Thunk support

This commit is contained in:
Samuel Štancl 2021-05-24 18:24:10 +02:00
parent 04cfab679c
commit 084736672e
2 changed files with 18 additions and 5 deletions

View file

@ -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<string, string> $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. */

View file

@ -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');
});