Skip to content

Commit

Permalink
add standard meta
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Sep 13, 2024
1 parent aeb8fda commit 5c3b239
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 27 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ class HomeController extends Controller
hreflang: "fr",
href: route('home', ['locale' => "fr"]),
),
]);
])
->setOpengraph(function(OpenGraph $opengraph){
$opengraph->title = "Custom opengraph title";
return $opengraph;
});

// Using the facade
SeoManager::current()
Expand All @@ -267,14 +271,14 @@ For more complex SEO needs, you can instantiate and configure the `SeoManager` c

```php
use Elegantly\Seo\SeoManager;
use Elegantly\Seo\Standard\StandardData;
use Elegantly\Seo\Standard\Standard;
use Elegantly\Seo\OpenGraph\OpenGraph;
use Elegantly\Seo\Twitter\Cards\Card;
use Elegantly\Seo\Schemas\Schema;
use Elegantly\Seo\SeoTags;

$seo = new SeoManager(
standard: new StandardData(/* ... */),
standard: new Standard(/* ... */),
opengraph: new OpenGraph(/* ... */),
twitter: new Card(/* ... */),
webpage: new WebPage(/* ... */),
Expand Down
50 changes: 50 additions & 0 deletions config/seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@
*/
'description' => null,

/*
|--------------------------------------------------------------------------
| Default Author
|--------------------------------------------------------------------------
|
| This is the default value used for <meta name="author">
|
*/
'author' => null,

/*
|--------------------------------------------------------------------------
| Default Generator
|--------------------------------------------------------------------------
|
| This is the default value used for <meta name="generator">
|
*/
'generator' => null,

/*
|--------------------------------------------------------------------------
| Default Keywords
Expand All @@ -34,6 +54,36 @@
*/
'keywords' => null,

/*
|--------------------------------------------------------------------------
| Default Referrer
|--------------------------------------------------------------------------
|
| This is the default value used for <meta name="referrer">
|
*/
'referrer' => null,

/*
|--------------------------------------------------------------------------
| Default Theme color
|--------------------------------------------------------------------------
|
| This is the default value used for <meta name="theme-color">
|
*/
'theme-color' => null,

/*
|--------------------------------------------------------------------------
| Default Color Scheme
|--------------------------------------------------------------------------
|
| This is the default value used for <meta name="color-scheme">
|
*/
'color-scheme' => null,

/*
|--------------------------------------------------------------------------
| Default Image path
Expand Down
88 changes: 72 additions & 16 deletions src/SeoManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Elegantly\Seo;

use Closure;
use Elegantly\Seo\Contracts\Taggable;
use Elegantly\Seo\OpenGraph\Locale;
use Elegantly\Seo\OpenGraph\OpenGraph;
use Elegantly\Seo\Schemas\Schema;
use Elegantly\Seo\Schemas\WebPage;
use Elegantly\Seo\Standard\Alternate;
use Elegantly\Seo\Standard\StandardData;
use Elegantly\Seo\Standard\Standard;
use Elegantly\Seo\Twitter\Cards\Card;
use Elegantly\Seo\Twitter\Cards\Summary;
use Illuminate\Contracts\Support\Htmlable;
Expand All @@ -24,7 +25,7 @@ class SeoManager implements Htmlable, Stringable, Taggable
* @param null|Schema[] $schemas
*/
public function __construct(
public ?StandardData $standard = null,
public ?Standard $standard = null,
public ?OpenGraph $opengraph = null,
public ?Card $twitter = null,
public ?WebPage $webpage = null,
Expand All @@ -41,31 +42,61 @@ public function current(): static
}

/**
* @param null|Standard|(Closure(Standard):null|Standard) $value
* @return $this
*/
public function setOpengraph(?OpenGraph $value): static
public function setStandard(null|Standard|Closure $value): static
{
$this->opengraph = $value;
if ($value instanceof Closure) {
$this->standard = $value($this->standard ?? Standard::default());
} else {
$this->standard = $value;
}

return $this;
}

/**
* @param null|OpenGraph|(Closure(OpenGraph):null|OpenGraph) $value
* @return $this
*/
public function setTwitter(?Card $value): static
public function setOpengraph(null|OpenGraph|Closure $value): static
{
$this->twitter = $value;
if ($value instanceof Closure) {
$this->opengraph = $value($this->opengraph ?? OpenGraph::default());
} else {
$this->opengraph = $value;
}

return $this;
}

/**
* @param null|Card|(Closure(Card):null|Card) $value
* @return $this
*/
public function setWebpage(?WebPage $value): static
public function setTwitter(null|Card|Closure $value): static
{
$this->webpage = $value;
if ($value instanceof Closure) {
$this->twitter = $value($this->twitter ?? Summary::default());
} else {
$this->twitter = $value;
}

return $this;
}

/**
* @param null|WebPage|(Closure(WebPage):null|WebPage) $value
* @return $this
*/
public function setWebpage(null|WebPage|Closure $value): static
{
if ($value instanceof Closure) {
$this->webpage = $value($this->webpage ?? WebPage::default());
} else {
$this->webpage = $value;
}

return $this;
}
Expand Down Expand Up @@ -198,6 +229,31 @@ public function setAlternates(?array $value): static
return $this;
}

/**
* @param null|string|string[] $value
* @return $this
*/
public function setKeywords(null|string|array $value): static
{
if ($this->standard) {
$this->standard->keywords = $value;
}

return $this;
}

/**
* @return $this
*/
public function setAuthor(?string $value): static
{
if ($this->standard) {
$this->standard->author = $value;
}

return $this;
}

/**
* @return $this
*/
Expand Down Expand Up @@ -229,14 +285,14 @@ public static function default(
?array $alternates = null,
): self {
return new self(
standard: StandardData::default(
$title,
$url,
$description,
$keywords,
$robots,
$sitemap,
$alternates
standard: Standard::default(
title: $title,
canonical: $url,
description: $description,
keywords: $keywords,
robots: $robots,
sitemap: $sitemap,
alternates: $alternates
),
opengraph: OpenGraph::default(
title: $title,
Expand Down
26 changes: 22 additions & 4 deletions src/Standard/StandardData.php → src/Standard/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Request;

class StandardData implements Taggable
/**
* @see https://developer.mozilla.org/fr/docs/Web/HTML/Element/meta/name
*/
class Standard implements Taggable
{
/**
* @param null|string|string[] $keywords
Expand All @@ -19,8 +22,13 @@ class StandardData implements Taggable
public function __construct(
public string $title,
public string $canonical,
public ?string $author = null,
public ?string $description = null,
public ?string $generator = null,
public null|string|array $keywords = null,
public ?string $referrer = null,
public ?string $themeColor = null,
public ?string $colorScheme = null,
public ?string $robots = null,
public ?string $sitemap = null,
public ?array $alternates = null,
Expand All @@ -35,17 +43,27 @@ public function __construct(
public static function default(
?string $title = null,
?string $canonical = null,
?string $author = null,
?string $description = null,
?string $generator = null,
null|string|array $keywords = null,
?string $referrer = null,
?string $themeColor = null,
?string $colorScheme = null,
?string $robots = null,
?string $sitemap = null,
?array $alternates = null,
): self {
return new self(
title: $title ?? __(config('seo.defaults.title') ?? config('app.name')),
title: $title ?? config('seo.defaults.title') ?? config('app.name'),
canonical: $canonical ?? Request::url(),
description: $description ?? __(config('seo.defaults.description')),
keywords: $keywords ?? __(config('seo.defaults.keywords')),
author: $author ?? config('seo.defaults.author'),
description: $description ?? config('seo.defaults.description'),
generator: $generator ?? config('seo.defaults.generator'),
keywords: $keywords ?? config('seo.defaults.keywords'),
referrer: $referrer ?? config('seo.defaults.referrer'),
themeColor: $themeColor ?? config('seo.defaults.theme-color'),
colorScheme: $colorScheme ?? config('seo.defaults.color-scheme'),
robots: $robots ?? config('seo.defaults.robots'),
sitemap: $sitemap ?? config('seo.defaults.sitemap'),
alternates: $alternates,
Expand Down
4 changes: 2 additions & 2 deletions tests/Units/SeoManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
use Elegantly\Seo\OpenGraph\Verticals\Website;
use Elegantly\Seo\SeoManager;
use Elegantly\Seo\Standard\Alternate;
use Elegantly\Seo\Standard\StandardData;
use Elegantly\Seo\Standard\Standard;
use Elegantly\Seo\Twitter\Cards\Summary;
use Elegantly\Seo\Twitter\Image as TwitterImage;

it('renders all standard, opengraph and twitter tags', function () {
$manager = new SeoManager(
standard: new StandardData(
standard: new Standard(
title: 'Foo',
canonical: 'https://example.com/standard',
description: 'Bar',
Expand Down
4 changes: 2 additions & 2 deletions tests/Units/StandardTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use Elegantly\Seo\Standard\Alternate;
use Elegantly\Seo\Standard\StandardData;
use Elegantly\Seo\Standard\Standard;

it('renders standard tags', function () {
$opengraph = new StandardData(
$opengraph = new Standard(
title: 'Foo',
canonical: 'https://example.com/standard',
description: 'Bar',
Expand Down

0 comments on commit 5c3b239

Please sign in to comment.