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
|
|
|
/** |
42
|
|
|
* Schema constructor. |
43
|
|
|
*/ |
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return Schema |
50
|
|
|
*/ |
51
|
|
|
protected function companyUrl(): string |
52
|
|
|
{ |
53
|
|
|
return $this->companyUrl; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Schema |
58
|
|
|
*/ |
59
|
|
|
protected function companyName(): string |
60
|
|
|
{ |
61
|
|
|
return $this->companyName; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @see https://schema.org/Person |
66
|
|
|
* |
67
|
|
|
* @param string $name |
68
|
|
|
* @param string $image |
69
|
|
|
* @param array $sameAs |
70
|
|
|
* @return Schema |
71
|
|
|
*/ |
72
|
|
|
protected function person(string $name, string $image, ?array $sameAs = null): Schema |
73
|
|
|
{ |
74
|
|
|
$this->person = (object)[ |
75
|
|
|
'@type' => 'Person', |
76
|
|
|
'name' => $name, |
77
|
|
|
'image' => $image, |
78
|
|
|
'sameAs' => $sameAs |
79
|
|
|
]; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @see https://schema.org/Organization |
85
|
|
|
*/ |
86
|
|
|
protected function organization(string $logo, string $image): Schema |
87
|
|
|
{ |
88
|
|
|
$logo = $this->imageObject($logo, null, null); |
89
|
|
|
$image = $this->imageObject($image, null, null); |
90
|
|
|
$this->organization = (object)[ |
91
|
|
|
'@type' => 'Organization', |
92
|
|
|
'name' => $this->companyName, |
93
|
|
|
'url' => $this->companyUrl, |
94
|
|
|
'logo' => $logo, |
95
|
|
|
'image' => $image, |
96
|
|
|
'sameAs' => ($this->companySameAs ?? null) |
97
|
|
|
]; |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @see https://schema.org/ImageObject |
103
|
|
|
*/ |
104
|
|
|
protected function imageObject(string $url, ?int $width = 1280, ?int $height = 720): array |
105
|
|
|
{ |
106
|
|
|
$this->imageObject = [ |
107
|
|
|
'@type' => 'ImageObject', |
108
|
|
|
'url' => $url, |
109
|
|
|
'width' => $width, |
110
|
|
|
'height' => $height, |
111
|
|
|
'caption' => $this->companyName |
112
|
|
|
]; |
113
|
|
|
return $this->imageObject; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $schema |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
protected function json(array $schema): string |
121
|
|
|
{ |
122
|
|
|
return json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $date |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
protected function setDate(string $date): string |
130
|
|
|
{ |
131
|
|
|
$date = (strpos($date, ' ') ? explode(' ', $date)[0] : $date); |
132
|
|
|
|
133
|
|
|
$date = str_replace('/', '-', $date); |
134
|
|
|
$date = explode('-', $date); |
135
|
|
|
|
136
|
|
|
if (strlen($date[0]) == 4) { |
137
|
|
|
$date = "{$date[0]}-{$date[1]}-$date[2]"; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (strlen($date[2]) == 4) { |
141
|
|
|
$date = "{$date[2]}-{$date[1]}-$date[0]"; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $date; |
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |