Skip to content

Commit

Permalink
accept array of strings for alternates
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Sep 14, 2024
1 parent 8be22c3 commit daf159a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/SeoManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,24 @@ public function setLocale(
}

/**
* @param null|Alternate[] $value
* @param null|Alternate[]|array<string, string> $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;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Units/SeoManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

0 comments on commit daf159a

Please sign in to comment.