Skip to content

Commit daf159a

Browse files
committed
accept array of strings for alternates
1 parent 8be22c3 commit daf159a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/SeoManager.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,24 @@ public function setLocale(
274274
}
275275

276276
/**
277-
* @param null|Alternate[] $value
277+
* @param null|Alternate[]|array<string, string> $value
278278
* @return $this
279279
*/
280280
public function setAlternates(?array $value): static
281281
{
282+
if ($value) {
283+
$value = collect($value)
284+
->map(function ($href, $hrelang) {
285+
if ($href instanceof Alternate) {
286+
return $href;
287+
}
288+
289+
return new Alternate($hrelang, $href);
290+
})
291+
->values()
292+
->all();
293+
}
294+
282295
if ($this->standard) {
283296
$this->standard->alternates = $value;
284297
}

tests/Units/SeoManagerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,26 @@
133133
expect($clone->customTags[0])->not->toBe($base->customTags[0]);
134134
expect($clone->customTags[0]->properties)->not->toBe($base->customTags[0]->properties);
135135
});
136+
137+
it('can set alternates using assoc array', function () {
138+
$manager = new SeoManager(
139+
standard: new Standard(
140+
title: 'foo',
141+
canonical: 'bar',
142+
),
143+
opengraph: new OpenGraph(
144+
title: 'foo',
145+
url: 'bar',
146+
),
147+
);
148+
149+
$manager->setAlternates([
150+
'fr' => 'example.com/fr',
151+
'en' => 'example.com/en',
152+
'x-default' => 'example.com/en',
153+
]);
154+
155+
expect($manager->standard->alternates)->toHaveLength(3);
156+
expect($manager->standard->alternates[0]->hreflang)->toBe('fr');
157+
expect($manager->standard->alternates[0]->href)->toBe('example.com/fr');
158+
});

0 commit comments

Comments
 (0)