diff --git a/src/SeoManager.php b/src/SeoManager.php index 8943345..c8d1626 100644 --- a/src/SeoManager.php +++ b/src/SeoManager.php @@ -274,11 +274,24 @@ public function setLocale( } /** - * @param null|Alternate[] $value + * @param null|Alternate[]|array $value * @return $this */ public function setAlternates(?array $value): static { + if ($value) { + $value = collect($value) + ->map(function ($href, $hrelang) { + if ($href instanceof Alternate) { + return $href; + } + + return new Alternate($hrelang, $href); + }) + ->values() + ->all(); + } + if ($this->standard) { $this->standard->alternates = $value; } diff --git a/tests/Units/SeoManagerTest.php b/tests/Units/SeoManagerTest.php index 1c9999e..cfd1dc1 100644 --- a/tests/Units/SeoManagerTest.php +++ b/tests/Units/SeoManagerTest.php @@ -133,3 +133,26 @@ expect($clone->customTags[0])->not->toBe($base->customTags[0]); expect($clone->customTags[0]->properties)->not->toBe($base->customTags[0]->properties); }); + +it('can set alternates using assoc array', function () { + $manager = new SeoManager( + standard: new Standard( + title: 'foo', + canonical: 'bar', + ), + opengraph: new OpenGraph( + title: 'foo', + url: 'bar', + ), + ); + + $manager->setAlternates([ + 'fr' => 'example.com/fr', + 'en' => 'example.com/en', + 'x-default' => 'example.com/en', + ]); + + expect($manager->standard->alternates)->toHaveLength(3); + expect($manager->standard->alternates[0]->hreflang)->toBe('fr'); + expect($manager->standard->alternates[0]->href)->toBe('example.com/fr'); +});