-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathSchema.php
65 lines (55 loc) · 1.1 KB
/
Schema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace Melbahja\Seo;
use Melbahja\Seo\Interfaces\SchemaInterface;
/**
* @package Melbahja\Seo
* @since v2.0
* @see https://git.io/phpseo
* @license MIT
* @copyright 2019-present Mohamed Elabhja
*/
class Schema implements SchemaInterface
{
protected $things = [];
/**
* @param string $type
* @param array $data
* @param SchemaInterface|null $parent
* @param SchemaInterface|null $root
*/
public function __construct(SchemaInterface ...$things)
{
$this->things = $things;
}
/**
* Add schema item to the graph.
*
* @param SchemaInterface $thing
*/
public function add(SchemaInterface $thing): SchemaInterface
{
$this->things[] = $thing;
return $this;
}
/**
* Get data as array
*
* @return array
*/
public function jsonSerialize(): array
{
return [
'@context' => 'https://schema.org',
'@graph' => $this->things
];
}
/**
* Serialize root schema
*
* @return string
*/
public function __toString(): string
{
return '<script type="application/ld+json">'. json_encode($this->jsonSerialize()) .'</script>';
}
}