From 3a2b64781b90068e8753001a501bffb4ca65c083 Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Sun, 28 Apr 2019 11:50:40 +0200 Subject: [PATCH] Add spatie Schema.org integration --- README.md | 24 +++++++++++++ composer.json | 3 +- src/Services/SeoService.php | 9 +++++ src/Services/Traits/RenderTrait.php | 14 +++++--- src/Services/Traits/SchemaOrgTrait.php | 42 ++++++++++++++++++++++ tests/SchemaOrgTest.php | 50 ++++++++++++++++++++++++++ 6 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 src/Services/Traits/SchemaOrgTrait.php create mode 100644 tests/SchemaOrgTest.php diff --git a/README.md b/README.md index c30f33a..e9c41c7 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,30 @@ For more information see the [Structs Documentation](https://github.com/romanzip {{ seo()->render() }} ``` +## Schema.org Integration + +This package features a basic integration for [Spaties Schema.org](https://github.com/spatie/schema-org) package to generate ld+json scripts. +Added Schema types render with the packages structs. + +```php +use Spatie\SchemaOrg\Schema; + +seo()->addSchema( + Schema::localBusiness()->name('Spatie') +); +``` + +```php +use Spatie\SchemaOrg\Schema; + +seo()->setSchemes([ + Schema::localBusiness()->name('Spatie'), + Schema::airline()->name('Spatie'), +]); +``` + +Take a look at the [Schema.org package Docs](https://github.com/spatie/schema-org#usage). + ## Cheat Sheet | Code | Rendered HTML | diff --git a/composer.json b/composer.json index eb21aa1..bce3232 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "require": { "php": ">=7.0", "illuminate/console": "~5.5.0|~5.6.0|~5.7.0|~5.8.0", - "illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0" + "illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0", + "spatie/schema-org": "^2.1" }, "require-dev": { "phpunit/phpunit": "^8.0", diff --git a/src/Services/SeoService.php b/src/Services/SeoService.php index 8b3da7a..ad14b02 100644 --- a/src/Services/SeoService.php +++ b/src/Services/SeoService.php @@ -6,6 +6,7 @@ use romanzipp\Seo\Services\Traits\CollisionTrait; use romanzipp\Seo\Services\Traits\HooksTrait; use romanzipp\Seo\Services\Traits\RenderTrait; +use romanzipp\Seo\Services\Traits\SchemaOrgTrait; use romanzipp\Seo\Services\Traits\SetterTrait; use romanzipp\Seo\Services\Traits\ShorthandSetterTrait; use romanzipp\Seo\Structs\Struct; @@ -18,6 +19,7 @@ class SeoService use CollisionTrait; use HooksTrait; use Macroable; + use SchemaOrgTrait; /** * Config @@ -33,6 +35,13 @@ class SeoService */ protected $structs = []; + /** + * Applied schema.org schemes. + * + * @var array + */ + protected $schemeOrgTypes = []; + /** * Constructor */ diff --git a/src/Services/Traits/RenderTrait.php b/src/Services/Traits/RenderTrait.php index 3aaca32..ec506f6 100644 --- a/src/Services/Traits/RenderTrait.php +++ b/src/Services/Traits/RenderTrait.php @@ -14,13 +14,17 @@ trait RenderTrait */ public function renderContentsArray(): array { - $structs = $this->getStructs(); - - $contents = array_map(function ($struct) { + $structs = array_map(function ($struct) { return StructBuilder::build($struct)->toHtml(); - }, $structs); + }, $this->getStructs()); + + $schemas = array_map(function ($schema) { + return $schema->toScript(); + }, $this->getSchemes()); - return array_values($contents); + return array_values( + array_merge($structs, $schemas) + ); } /** diff --git a/src/Services/Traits/SchemaOrgTrait.php b/src/Services/Traits/SchemaOrgTrait.php new file mode 100644 index 0000000..8e83525 --- /dev/null +++ b/src/Services/Traits/SchemaOrgTrait.php @@ -0,0 +1,42 @@ +schemeOrgTypes; + } + + /** + * Add spatie/schema-org object. + * + * @param Type $schema schema.org Type + */ + public function addSchema(Type $schema): self + { + $this->schemeOrgTypes[] = $schema; + + return $this; + } + + /** + * Set array of spatie/schema-org objects. + * + * @param array $types + */ + public function setSchemes(array $types): self + { + $this->schemeOrgTypes = $types; + + return $this; + } +} diff --git a/tests/SchemaOrgTest.php b/tests/SchemaOrgTest.php new file mode 100644 index 0000000..1dab15a --- /dev/null +++ b/tests/SchemaOrgTest.php @@ -0,0 +1,50 @@ +addSchema( + Schema::localBusiness()->name('Spatie') + ); + + $this->assertCount( + 1, + seo()->renderContentsArray() + ); + } + + public function testSetter() + { + seo()->addSchema( + Schema::localBusiness()->name('Spatie') + ); + + seo()->setSchemes([ + Schema::airline()->name('Spatie'), + ]); + + $this->assertCount( + 1, + seo()->renderContentsArray() + ); + } + + public function testBasicRender() + { + seo()->addSchema( + Schema::localBusiness()->name('Spatie') + ); + + $this->assertStringStartsWith( + '