Skip to content

Commit

Permalink
fix standard tags
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Sep 13, 2024
1 parent 3193a1c commit 2dcc5ef
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 47 deletions.
78 changes: 34 additions & 44 deletions src/Standard/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Elegantly\Seo\Tags\Link;
use Elegantly\Seo\Tags\Meta;
use Elegantly\Seo\Tags\Title;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Request;

/**
Expand Down Expand Up @@ -72,52 +71,43 @@ public static function default(

public function toTags(): SeoTags
{
$tags = new SeoTags([
new Title(
content: $this->title,
),
]);
$tags = new SeoTags;

if ($this->description) {
$tags->push(new Meta(
name: 'description',
content: $this->description,
));
}

if (! empty($this->keywords)) {
$tags->push(new Meta(
name: 'keywords',
content: implode(',', Arr::wrap($this->keywords)),
));
}

if ($this->robots) {
$tags->push(new Meta(
name: 'robots',
content: $this->robots,
));
}
$items = get_object_vars($this);

if ($this->sitemap) {
$tags->push(new Link(
rel: 'sitemap',
href: $this->sitemap,
title: 'Sitemap',
type: 'application/xml',
));
}

if ($this->canonical) {
$tags->push(new Link(
rel: 'canonical',
href: $this->canonical,
));
}
foreach ($items as $key => $value) {
if (blank($value)) {
continue;
}

if ($this->alternates) {
foreach ($this->alternates as $alternate) {
$tags->push(...$alternate->toTags());
if ($key === 'title') {
$tags->push(new Title(
content: $value,
));
} elseif ($key === 'sitemap') {
$tags->push(new Link(
rel: $key,
href: $value,
title: 'Sitemap',
type: 'application/xml',
));
} elseif ($key === 'canonical') {
$tags->push(new Link(
rel: $key,
href: $value,
));
} elseif ($key === 'alternates') {
/**
* @var Taggable[] $value
*/
foreach ($value as $item) {
$tags->push(...$item->toTags());
}
} else {
$tags->push(new Meta(
name: $key,
content: is_array($value) ? implode(',', $value) : $value,
));
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Features/SeoManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
)->toBe(implode("\n", [
// standard
'<title >'.$title.'</title>',
'<link rel="canonical" href="'.$url.'" />',
'<meta name="description" content="'.$description.'" />',
'<meta name="robots" content="'.$robots.'" />',
'<link rel="canonical" href="'.$url.'" />',
// opengraph
'<meta property="og:title" content="'.$title.'" />',
'<meta property="og:url" content="'.$url.'" />',
Expand Down
2 changes: 1 addition & 1 deletion tests/Units/SeoManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
$manager->toTags()->toHtml()
)->toBe(implode("\n", [
'<title >Foo</title>',
'<meta name="description" content="Bar" />',
'<link rel="canonical" href="https://example.com/standard" />',
'<meta name="description" content="Bar" />',
'<link rel="alternate" hreflang="en" href="https://example.com/standard/en" />',
'<link rel="alternate" hreflang="fr" href="https://example.com/standard/fr" />',
//
Expand Down
4 changes: 3 additions & 1 deletion tests/Units/StandardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
canonical: 'https://example.com/standard',
description: 'Bar',
keywords: ['foo', 'bar'],
author: 'Quentin Gabriele',
alternates: [
new Alternate(
hreflang: 'en',
Expand All @@ -25,9 +26,10 @@
$opengraph->toTags()->toHtml()
)->toBe(implode("\n", [
'<title >Foo</title>',
'<link rel="canonical" href="https://example.com/standard" />',
'<meta name="author" content="Quentin Gabriele" />',
'<meta name="description" content="Bar" />',
'<meta name="keywords" content="foo,bar" />',
'<link rel="canonical" href="https://example.com/standard" />',
'<link rel="alternate" hreflang="en" href="https://example.com/standard/en" />',
'<link rel="alternate" hreflang="fr" href="https://example.com/standard/fr" />',
]));
Expand Down

0 comments on commit 2dcc5ef

Please sign in to comment.