1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace WilderAmorim\StructuredData; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Schema |
8
|
|
|
* @package WilderAmorim\StructuredData |
9
|
|
|
*/ |
10
|
|
|
abstract class Schema |
11
|
|
|
{ |
12
|
|
|
/** @var */ |
13
|
|
|
protected $person; |
14
|
|
|
|
15
|
|
|
/** @var */ |
16
|
|
|
protected $organization; |
17
|
|
|
|
18
|
|
|
/** @var */ |
19
|
|
|
protected $imageObject; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
protected $companyName; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $companyUrl; |
26
|
|
|
|
27
|
|
|
/** @var array */ |
28
|
|
|
protected $companySameAs; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @see http://schema.org |
32
|
|
|
*/ |
33
|
|
|
const CONTEXT = 'http://schema.org'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @see https://schema.org/WebPage |
37
|
|
|
*/ |
38
|
|
|
const TYPE = 'WebPage'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Schema constructor. |
42
|
|
|
*/ |
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
// |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @see https://schema.org/Person |
50
|
|
|
* |
51
|
|
|
* @param string $name |
52
|
|
|
* @param string $image |
53
|
|
|
* @param array|null $sameAs |
54
|
|
|
* @return Schema |
55
|
|
|
*/ |
56
|
|
|
protected function person(string $name, string $image, ?array $sameAs = null): Schema |
57
|
|
|
{ |
58
|
|
|
$this->person = (object)[ |
59
|
|
|
'@type' => 'Person', |
60
|
|
|
'name' => $name, |
61
|
|
|
'image' => $image, |
62
|
|
|
'sameAs' => $sameAs |
63
|
|
|
]; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @see https://schema.org/Organization |
69
|
|
|
* |
70
|
|
|
* @param string $logo |
71
|
|
|
* @param string $image |
72
|
|
|
* @return Schema |
73
|
|
|
*/ |
74
|
|
|
protected function organization(string $logo, string $image): Schema |
75
|
|
|
{ |
76
|
|
|
$logo = $this->imageObject($logo, null, null); |
77
|
|
|
$image = $this->imageObject($image, null, null); |
78
|
|
|
$this->organization = (object)[ |
79
|
|
|
'@type' => 'Organization', |
80
|
|
|
'name' => $this->companyName, |
81
|
|
|
'url' => $this->companyUrl, |
82
|
|
|
'logo' => $logo, |
83
|
|
|
'image' => $image, |
84
|
|
|
'sameAs' => ($this->companySameAs ?? null) |
85
|
|
|
]; |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @see https://schema.org/ImageObject |
91
|
|
|
* |
92
|
|
|
* @param string $url |
93
|
|
|
* @param int|null $width |
94
|
|
|
* @param int|null $height |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
protected function imageObject(string $url, ?int $width = 1280, ?int $height = 720): array |
98
|
|
|
{ |
99
|
|
|
$this->imageObject = [ |
100
|
|
|
'@type' => 'ImageObject', |
101
|
|
|
'url' => $url, |
102
|
|
|
'width' => $width, |
103
|
|
|
'height' => $height, |
104
|
|
|
'caption' => $this->companyName |
105
|
|
|
]; |
106
|
|
|
return $this->imageObject; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param array $schema |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
protected function json(array $schema): string |
114
|
|
|
{ |
115
|
|
|
return json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $date |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
protected function setDate(string $date): string |
123
|
|
|
{ |
124
|
|
|
$date = (strpos($date, ' ') ? explode(' ', $date)[0] : $date); |
125
|
|
|
|
126
|
|
|
$date = str_replace('/', '-', $date); |
127
|
|
|
$date = explode('-', $date); |
128
|
|
|
|
129
|
|
|
if (strlen($date[0]) == 4) { |
130
|
|
|
$date = "{$date[0]}-{$date[1]}-$date[2]"; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (strlen($date[2]) == 4) { |
134
|
|
|
$date = "{$date[2]}-{$date[1]}-$date[0]"; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $date; |
|
|
|
|
138
|
|
|
} |
139
|
|
|
} |