Skip to content

Commit

Permalink
Add spatie Schema.org integration
Browse files Browse the repository at this point in the history
  • Loading branch information
romanzipp committed Apr 28, 2019
1 parent 712d4ad commit 3a2b647
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 6 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions src/Services/SeoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,7 @@ class SeoService
use CollisionTrait;
use HooksTrait;
use Macroable;
use SchemaOrgTrait;

/**
* Config
Expand All @@ -33,6 +35,13 @@ class SeoService
*/
protected $structs = [];

/**
* Applied schema.org schemes.
*
* @var array
*/
protected $schemeOrgTypes = [];

/**
* Constructor
*/
Expand Down
14 changes: 9 additions & 5 deletions src/Services/Traits/RenderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions src/Services/Traits/SchemaOrgTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace romanzipp\Seo\Services\Traits;

use Spatie\SchemaOrg\Type;

trait SchemaOrgTrait
{
/**
* Get spatie/schema-org types.
*
* @return array
*/
public function getSchemes(): array
{
return $this->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;
}
}
50 changes: 50 additions & 0 deletions tests/SchemaOrgTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace romanzipp\Seo\Test;

use romanzipp\Seo\Facades\Seo;
use romanzipp\Seo\Test\TestCase;
use Spatie\SchemaOrg\Schema;

class SchemaOrgTest extends TestCase
{
public function testAppending()
{
seo()->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(
'<script type="application/ld+json">',
seo()->render()->toHtml()
);
}
}

0 comments on commit 3a2b647

Please sign in to comment.