Skip to content

Commit 2dcc5ef

Browse files
committed
fix standard tags
1 parent 3193a1c commit 2dcc5ef

File tree

4 files changed

+39
-47
lines changed

4 files changed

+39
-47
lines changed

src/Standard/Standard.php

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Elegantly\Seo\Tags\Link;
88
use Elegantly\Seo\Tags\Meta;
99
use Elegantly\Seo\Tags\Title;
10-
use Illuminate\Support\Arr;
1110
use Illuminate\Support\Facades\Request;
1211

1312
/**
@@ -72,52 +71,43 @@ public static function default(
7271

7372
public function toTags(): SeoTags
7473
{
75-
$tags = new SeoTags([
76-
new Title(
77-
content: $this->title,
78-
),
79-
]);
74+
$tags = new SeoTags;
8075

81-
if ($this->description) {
82-
$tags->push(new Meta(
83-
name: 'description',
84-
content: $this->description,
85-
));
86-
}
87-
88-
if (! empty($this->keywords)) {
89-
$tags->push(new Meta(
90-
name: 'keywords',
91-
content: implode(',', Arr::wrap($this->keywords)),
92-
));
93-
}
94-
95-
if ($this->robots) {
96-
$tags->push(new Meta(
97-
name: 'robots',
98-
content: $this->robots,
99-
));
100-
}
76+
$items = get_object_vars($this);
10177

102-
if ($this->sitemap) {
103-
$tags->push(new Link(
104-
rel: 'sitemap',
105-
href: $this->sitemap,
106-
title: 'Sitemap',
107-
type: 'application/xml',
108-
));
109-
}
110-
111-
if ($this->canonical) {
112-
$tags->push(new Link(
113-
rel: 'canonical',
114-
href: $this->canonical,
115-
));
116-
}
78+
foreach ($items as $key => $value) {
79+
if (blank($value)) {
80+
continue;
81+
}
11782

118-
if ($this->alternates) {
119-
foreach ($this->alternates as $alternate) {
120-
$tags->push(...$alternate->toTags());
83+
if ($key === 'title') {
84+
$tags->push(new Title(
85+
content: $value,
86+
));
87+
} elseif ($key === 'sitemap') {
88+
$tags->push(new Link(
89+
rel: $key,
90+
href: $value,
91+
title: 'Sitemap',
92+
type: 'application/xml',
93+
));
94+
} elseif ($key === 'canonical') {
95+
$tags->push(new Link(
96+
rel: $key,
97+
href: $value,
98+
));
99+
} elseif ($key === 'alternates') {
100+
/**
101+
* @var Taggable[] $value
102+
*/
103+
foreach ($value as $item) {
104+
$tags->push(...$item->toTags());
105+
}
106+
} else {
107+
$tags->push(new Meta(
108+
name: $key,
109+
content: is_array($value) ? implode(',', $value) : $value,
110+
));
121111
}
122112
}
123113

tests/Features/SeoManagerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
)->toBe(implode("\n", [
2424
// standard
2525
'<title >'.$title.'</title>',
26+
'<link rel="canonical" href="'.$url.'" />',
2627
'<meta name="description" content="'.$description.'" />',
2728
'<meta name="robots" content="'.$robots.'" />',
28-
'<link rel="canonical" href="'.$url.'" />',
2929
// opengraph
3030
'<meta property="og:title" content="'.$title.'" />',
3131
'<meta property="og:url" content="'.$url.'" />',

tests/Units/SeoManagerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
$manager->toTags()->toHtml()
5757
)->toBe(implode("\n", [
5858
'<title >Foo</title>',
59-
'<meta name="description" content="Bar" />',
6059
'<link rel="canonical" href="https://example.com/standard" />',
60+
'<meta name="description" content="Bar" />',
6161
'<link rel="alternate" hreflang="en" href="https://example.com/standard/en" />',
6262
'<link rel="alternate" hreflang="fr" href="https://example.com/standard/fr" />',
6363
//

tests/Units/StandardTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
canonical: 'https://example.com/standard',
1010
description: 'Bar',
1111
keywords: ['foo', 'bar'],
12+
author: 'Quentin Gabriele',
1213
alternates: [
1314
new Alternate(
1415
hreflang: 'en',
@@ -25,9 +26,10 @@
2526
$opengraph->toTags()->toHtml()
2627
)->toBe(implode("\n", [
2728
'<title >Foo</title>',
29+
'<link rel="canonical" href="https://example.com/standard" />',
30+
'<meta name="author" content="Quentin Gabriele" />',
2831
'<meta name="description" content="Bar" />',
2932
'<meta name="keywords" content="foo,bar" />',
30-
'<link rel="canonical" href="https://example.com/standard" />',
3133
'<link rel="alternate" hreflang="en" href="https://example.com/standard/en" />',
3234
'<link rel="alternate" hreflang="fr" href="https://example.com/standard/fr" />',
3335
]));

0 commit comments

Comments
 (0)