1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace WilderAmorim\StructuredData; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class BlogPosting |
8
|
|
|
* @package WilderAmorim\StructuredData |
9
|
|
|
*/ |
10
|
|
|
class BlogPosting extends Schema |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \stdClass |
14
|
|
|
*/ |
15
|
|
|
private $data; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @see https://schema.org/BlogPosting |
19
|
|
|
*/ |
20
|
|
|
const TYPE = 'BlogPosting'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* BlogPosting constructor. |
24
|
|
|
* @param Company $initialSchema |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Company $initialSchema) |
27
|
|
|
{ |
28
|
|
|
$this->data = new \stdClass(); |
29
|
|
|
|
30
|
|
|
$this->companyName = $initialSchema->company; |
31
|
|
|
$this->companyUrl = $initialSchema->url; |
32
|
|
|
$this->companySameAs = $initialSchema->sameAs; |
33
|
|
|
parent::__construct(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @see https://schema.org/mainEntityOfPage |
38
|
|
|
* @param string $url |
39
|
|
|
* @param string $type |
40
|
|
|
* @return BlogPosting |
41
|
|
|
*/ |
42
|
|
|
public function mainEntityOfPage(string $url, string $type = Schema::TYPE): BlogPosting |
43
|
|
|
{ |
44
|
|
|
$this->data->mainEntityOfPage = [ |
45
|
|
|
'@type' => $type, |
46
|
|
|
'@id' => $url |
47
|
|
|
]; |
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @see https://schema.org/author |
53
|
|
|
* |
54
|
|
|
* @param string $name |
55
|
|
|
* @param string $image |
56
|
|
|
* @param array|null $sameAs |
57
|
|
|
* @return Schema |
58
|
|
|
*/ |
59
|
|
|
public function author(string $name, string $image, array $sameAs = null): Schema |
60
|
|
|
{ |
61
|
|
|
return $this->person($name, $image, $sameAs); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @see https://schema.org/publisher |
66
|
|
|
* |
67
|
|
|
* @param string $image |
68
|
|
|
* @return Schema |
69
|
|
|
*/ |
70
|
|
|
public function publisher(string $image): Schema |
71
|
|
|
{ |
72
|
|
|
return $this->organization($image, $image); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @see https://schema.org/image |
77
|
|
|
* |
78
|
|
|
* @param string $url |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function image(string $url): array |
82
|
|
|
{ |
83
|
|
|
return $this->imageObject($url); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $headline |
88
|
|
|
* @param string $description |
89
|
|
|
* @param string $articleBody |
90
|
|
|
* @param string $datePublished |
91
|
|
|
* @param string|null $dateModified |
92
|
|
|
* @return BlogPosting |
93
|
|
|
*/ |
94
|
|
|
public function start( |
95
|
|
|
string $headline, |
96
|
|
|
string $description, |
97
|
|
|
string $articleBody, |
98
|
|
|
string $datePublished, |
99
|
|
|
?string $dateModified = null |
100
|
|
|
): BlogPosting { |
101
|
|
|
$this->data->header = (object)[ |
102
|
|
|
'headline' => $headline, |
103
|
|
|
'description' => $description, |
104
|
|
|
'articleBody' => strip_tags($articleBody), |
105
|
|
|
'datePublished' => $this->setDate($datePublished), |
106
|
|
|
'dateModified' => (!is_null($dateModified) ? $this->setDate($dateModified) : $this->setDate($datePublished)) |
107
|
|
|
]; |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return object |
113
|
|
|
*/ |
114
|
|
|
public function header(): object |
115
|
|
|
{ |
116
|
|
|
return $this->data->header; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return object |
121
|
|
|
*/ |
122
|
|
|
public function data(): object |
123
|
|
|
{ |
124
|
|
|
$this->data = (object)[ |
125
|
|
|
'header' => $this->header(), |
126
|
|
|
'mainEntityOfPage' => (object)$this->data->mainEntityOfPage, |
127
|
|
|
'author' => (object)$this->person, |
128
|
|
|
'image' => (object)$this->imageObject, |
129
|
|
|
'publisher' => (object)$this->organization |
130
|
|
|
]; |
131
|
|
|
return $this->data; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function render(): string |
138
|
|
|
{ |
139
|
|
|
$render = [ |
140
|
|
|
'@context' => self::CONTEXT, |
141
|
|
|
'@type' => self::TYPE, |
142
|
|
|
'headline' => $this->data->header->headline, |
143
|
|
|
'description' => $this->data->header->description, |
144
|
|
|
'datePublished' => $this->data->header->datePublished, |
145
|
|
|
'dateModified' => $this->data->header->dateModified, |
146
|
|
|
'articleBody' => $this->data->header->articleBody, |
147
|
|
|
'author' => $this->person, |
148
|
|
|
'mainEntityOfPage' => $this->data->mainEntityOfPage, |
149
|
|
|
'publisher' => [$this->organization], |
150
|
|
|
'image' => $this->imageObject |
151
|
|
|
]; |
152
|
|
|
return $this->json($render); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Responsible method for debug all data |
157
|
|
|
*/ |
158
|
|
|
public function debug(): void |
159
|
|
|
{ |
160
|
|
|
var_dump($this->data()); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
} |