1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace ElePHPant\StructuredData; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Schema |
8
|
|
|
* @package ElePHPant\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 */ |
22
|
|
|
protected $webPageName; |
23
|
|
|
|
24
|
|
|
/** @var */ |
25
|
|
|
protected $webPageUrl; |
26
|
|
|
|
27
|
|
|
/** @var */ |
28
|
|
|
protected $webPageSameAs; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @link http://schema.org |
32
|
|
|
*/ |
33
|
|
|
const CONTEXT = 'http://schema.org'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @link 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
|
|
|
* @link https://schema.org/Person |
50
|
|
|
* |
51
|
|
|
* @param string $name |
52
|
|
|
* @param string $image |
53
|
|
|
* @param array|null $sameAs |
54
|
|
|
* @return object |
55
|
|
|
*/ |
56
|
|
|
protected function person(string $name, string $image, ?array $sameAs = null): object |
57
|
|
|
{ |
58
|
|
|
$this->person = (object)[ |
59
|
|
|
'@type' => 'Person', |
60
|
|
|
'name' => $name, |
61
|
|
|
'image' => $image, |
62
|
|
|
'sameAs' => $sameAs |
63
|
|
|
]; |
64
|
|
|
return $this->person; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @link https://schema.org/Organization |
69
|
|
|
* |
70
|
|
|
* @param string $logo |
71
|
|
|
* @param string $image |
72
|
|
|
* @param array|null $address |
73
|
|
|
* @return object |
74
|
|
|
*/ |
75
|
|
|
protected function organization( |
76
|
|
|
string $logo, |
77
|
|
|
string $image, |
78
|
|
|
?array $address = null |
79
|
|
|
): object { |
80
|
|
|
$logo = $this->imageObject($logo, null, null); |
81
|
|
|
$image = $this->imageObject($image, null, null); |
82
|
|
|
$this->organization = [ |
83
|
|
|
'@type' => 'Organization', |
84
|
|
|
'name' => $this->webPageName, |
85
|
|
|
'url' => $this->webPageUrl, |
86
|
|
|
'logo' => $logo, |
87
|
|
|
'image' => $image, |
88
|
|
|
'sameAs' => ($this->webPageSameAs ?? null) |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
if (!is_null($address)) { |
92
|
|
|
$this->organization['address'] = $this->postalAddress( |
93
|
|
|
$address['addressLocality'], |
94
|
|
|
$address['addressRegion'], |
95
|
|
|
$address['postalCode'], |
96
|
|
|
$address['streetAddress'] |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
return (object)$this->organization; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @link https://schema.org/PostalAddress |
104
|
|
|
* |
105
|
|
|
* @param string $addressLocality |
106
|
|
|
* @param string $addressRegion |
107
|
|
|
* @param string $postalCode |
108
|
|
|
* @param string $streetAddress |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
protected function postalAddress( |
112
|
|
|
string $addressLocality, |
113
|
|
|
string $addressRegion, |
114
|
|
|
string $postalCode, |
115
|
|
|
string $streetAddress |
116
|
|
|
): array { |
117
|
|
|
$postalAddress = [ |
118
|
|
|
'@type' => 'PostalAddress', |
119
|
|
|
'addressLocality' => $addressLocality, |
120
|
|
|
'addressRegion' => $addressRegion, |
121
|
|
|
'postalCode' => $postalCode, |
122
|
|
|
'streetAddress' => $streetAddress |
123
|
|
|
]; |
124
|
|
|
return $postalAddress; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @link https://schema.org/ImageObject |
129
|
|
|
* |
130
|
|
|
* @param string $url |
131
|
|
|
* @param int|null $width |
132
|
|
|
* @param int|null $height |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
protected function imageObject(string $url, ?int $width = 1280, ?int $height = 720): array |
136
|
|
|
{ |
137
|
|
|
$this->imageObject = [ |
138
|
|
|
'@type' => 'ImageObject', |
139
|
|
|
'url' => $url, |
140
|
|
|
'width' => $width, |
141
|
|
|
'height' => $height, |
142
|
|
|
'caption' => $this->webPageName |
143
|
|
|
]; |
144
|
|
|
return $this->imageObject; |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param array $schema |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
protected function json(array $schema): string |
153
|
|
|
{ |
154
|
|
|
return json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $date |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
protected function setDate(string $date): string |
162
|
|
|
{ |
163
|
|
|
$date = (strpos($date, ' ') ? explode(' ', $date)[0] : $date); |
164
|
|
|
|
165
|
|
|
$date = str_replace('/', '-', $date); |
166
|
|
|
$date = explode('-', $date); |
167
|
|
|
|
168
|
|
|
if (strlen($date[0]) == 4) { |
169
|
|
|
$date = "{$date[0]}-{$date[1]}-$date[2]"; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (strlen($date[2]) == 4) { |
173
|
|
|
$date = "{$date[2]}-{$date[1]}-$date[0]"; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $date; |
|
|
|
|
177
|
|
|
} |
178
|
|
|
} |