From a737879baf423c4f1c68807bd1d4ec60e1e6ee96 Mon Sep 17 00:00:00 2001 From: Tobias Petry Date: Tue, 5 Jul 2022 20:19:46 +0200 Subject: [PATCH] Add previewify prefix (#24) * add required previewify prefix * Fix code style (php-cs-fixer) * only add prefix when not already available * Fix code style (php-cs-fixer) Co-authored-by: PHP CS Fixer --- README.md | 3 +++ src/SEOManager.php | 9 +++++++-- tests/Pest/PreviewifyTest.php | 10 +++++----- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0e73c09..cb57c5a 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,9 @@ The `previewify()` method also returns a signed URL to the image, which lets you @seo('title') ``` +> **Note** +> The `previewify:` prefix will be automatically prepended to all provided data keys. + ## Examples ### Service Provider diff --git a/src/SEOManager.php b/src/SEOManager.php index fab91f6..77a8114 100644 --- a/src/SEOManager.php +++ b/src/SEOManager.php @@ -192,9 +192,14 @@ public function previewify(string $alias, int|string|array $data = null): string if ($data === null) { $data = [ - 'title' => $this->raw('title'), - 'description' => $this->raw('description'), + 'previewify:title' => $this->raw('title'), + 'previewify:description' => $this->raw('description'), ]; + } else { + $data = array_combine( + array_map(fn ($key) => str_starts_with($key, 'previewify:') ? $key : "previewify:{$key}", array_keys($data)), + $data, + ); } $query = base64_encode(json_encode($data, JSON_THROW_ON_ERROR)); diff --git a/tests/Pest/PreviewifyTest.php b/tests/Pest/PreviewifyTest.php index ebc4030..9ac59c8 100644 --- a/tests/Pest/PreviewifyTest.php +++ b/tests/Pest/PreviewifyTest.php @@ -18,16 +18,16 @@ test('previewify templates can be given data', function () { seo()->previewify('blog', 1); - expect(seo()->previewify('blog', ['title' => 'abc', 'excerpt' => 'def'])) + expect(seo()->previewify('blog', ['title' => 'abc', 'previewify:excerpt' => 'def'])) ->toContain('previewify.app/generate/templates/1') - ->toContain(base64_encode(json_encode(['title' => 'abc', 'excerpt' => 'def']))); + ->toContain(base64_encode(json_encode(['previewify:title' => 'abc', 'previewify:excerpt' => 'def']))); }); test('the previewify method returns a link to a signed url', function () { seo()->previewify('blog', 1); expect(seo()->previewify('blog', ['title' => 'abc'])) - ->toContain('?signature=' . hash_hmac('sha256', base64_encode(json_encode(['title' => 'abc'])), config('services.previewify.key'))); + ->toContain('?signature=' . hash_hmac('sha256', base64_encode(json_encode(['previewify:title' => 'abc'])), config('services.previewify.key'))); }); test("previewify templates use default data when they're not passed any data explicitly", function () { @@ -37,7 +37,7 @@ expect(seo()->previewify('blog')) ->toContain('previewify.app/generate/templates/1') - ->toContain(base64_encode(json_encode(['title' => 'foo', 'description' => 'bar']))); + ->toContain(base64_encode(json_encode(['previewify:title' => 'foo', 'previewify:description' => 'bar']))); }); test('previewify images are used as the cover images', function () { @@ -66,7 +66,7 @@ expect(seo()->previewify('blog')) ->toContain('previewify.app/generate/templates/1') - ->toContain(base64_encode(json_encode(['title' => 'foo', 'description' => 'bar']))); + ->toContain(base64_encode(json_encode(['previewify:title' => 'foo', 'previewify:description' => 'bar']))); }); test('the @seo helper can be used for setting a previewify image', function () {