1 | <?php |
||
2 | |||
3 | |||
4 | namespace ElePHPant\StructuredData; |
||
5 | |||
6 | /** |
||
7 | * Class WebPage |
||
8 | * @package ElePHPant\StructuredData |
||
9 | */ |
||
10 | class WebPage extends Schema |
||
11 | { |
||
12 | /** |
||
13 | * @var \stdClass |
||
14 | */ |
||
15 | protected $data; |
||
16 | |||
17 | /** |
||
18 | * @link https://schema.org/WebPage |
||
19 | */ |
||
20 | const TYPE = 'WebPage'; |
||
21 | |||
22 | /** |
||
23 | * WebPage constructor. |
||
24 | */ |
||
25 | public function __construct() |
||
26 | { |
||
27 | $this->data = new \stdClass(); |
||
28 | parent::__construct(); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @return object |
||
33 | */ |
||
34 | public function header(): object |
||
35 | { |
||
36 | return $this->data->header; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @return string |
||
41 | */ |
||
42 | public function name(): string |
||
43 | { |
||
44 | return $this->data->name; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @return string |
||
49 | */ |
||
50 | public function url(): string |
||
51 | { |
||
52 | return $this->data->url; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return string |
||
57 | */ |
||
58 | public function description(): string |
||
59 | { |
||
60 | return $this->data->description; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return array|null |
||
65 | */ |
||
66 | public function sameAs(): ?array |
||
67 | { |
||
68 | return $this->webPageSameAs; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param string $name |
||
73 | * @param string $description |
||
74 | * @param string $image |
||
75 | * @param string $url |
||
76 | * @param string $inLanguage |
||
77 | * @return WebPage |
||
78 | */ |
||
79 | public function start( |
||
80 | string $name, |
||
81 | string $description, |
||
82 | string $image, |
||
83 | string $url, |
||
84 | string $inLanguage = 'en-US' |
||
85 | ): WebPage { |
||
86 | $this->data->name = $name; |
||
87 | $this->data->url = $url; |
||
88 | $this->data->description = $description; |
||
89 | $this->data->image = $image; |
||
90 | $this->data->inLanguage = $inLanguage; |
||
91 | |||
92 | $this->webPageName = $this->data->name; |
||
93 | $this->webPageUrl = $this->data->url; |
||
94 | |||
95 | $this->data->header = (object)[ |
||
96 | '@context' => Schema::CONTEXT, |
||
97 | '@type' => self::TYPE, |
||
98 | 'name' => $this->data->name, |
||
99 | 'description' => $this->data->description, |
||
100 | 'image' => $this->data->image, |
||
101 | 'url' => $this->data->url, |
||
102 | 'inLanguage' => $this->data->inLanguage |
||
103 | ]; |
||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @link https://schema.org/isPartOf |
||
109 | * |
||
110 | * @param string $logo |
||
111 | * @param string $image |
||
112 | * @param string $type |
||
113 | * @param array|null $sameAs |
||
114 | * @return WebPage |
||
115 | */ |
||
116 | public function isPartOf(string $logo, string $image, string $type = 'WebSite', ?array $sameAs = null): WebPage |
||
117 | { |
||
118 | $this->webPageSameAs = $sameAs; |
||
119 | |||
120 | $this->data->isPartOf = (object)[ |
||
121 | '@type' => $type, |
||
122 | 'name' => $this->data->name, |
||
123 | 'url' => $this->data->url, |
||
124 | 'publisher' => $this->organization($logo, $image) |
||
125 | ]; |
||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @link https://schema.org/about |
||
131 | * |
||
132 | * @param string $image |
||
133 | * @return WebPage |
||
134 | */ |
||
135 | public function about(string $image): WebPage |
||
136 | { |
||
137 | $this->data->about = $this->organization($image, $image); |
||
138 | return $this; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @link https://schema.org/creator |
||
143 | * |
||
144 | * @param string $organizationImage |
||
145 | * @param string $addressLocality |
||
146 | * @param string $addressRegion |
||
147 | * @param string $postalCode |
||
148 | * @param string $streetAddress |
||
149 | * @param string $personName |
||
150 | * @param string $personImage |
||
151 | * @param array|null $sameAs |
||
152 | * @return WebPage |
||
153 | */ |
||
154 | public function creator( |
||
155 | string $organizationImage, |
||
156 | string $addressLocality, |
||
157 | string $addressRegion, |
||
158 | string $postalCode, |
||
159 | string $streetAddress, |
||
160 | string $personName, |
||
161 | string $personImage, |
||
162 | ?array $sameAs = null |
||
163 | ): WebPage { |
||
164 | $address = [ |
||
165 | 'addressLocality' => $addressLocality, |
||
166 | 'addressRegion' => $addressRegion, |
||
167 | 'postalCode' => $postalCode, |
||
168 | 'streetAddress' => $streetAddress |
||
169 | ]; |
||
170 | |||
171 | $this->data->organization = $this->organization($organizationImage, $organizationImage, $address); |
||
172 | $this->data->person = $this->person($personName, $personImage, $sameAs); |
||
173 | |||
174 | $this->data->creator = [ |
||
175 | 'organization' => $this->data->organization, |
||
176 | 'person' => $this->data->person |
||
177 | ]; |
||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return object |
||
183 | */ |
||
184 | public function data(): object |
||
185 | { |
||
186 | $this->data = (object)[ |
||
187 | 'header' => $this->header(), |
||
188 | 'isPartOf' => (object)$this->data->isPartOf, |
||
189 | 'about' => (object)$this->data->about, |
||
190 | 'creator' => $this->data->creator, |
||
191 | 'organization' => $this->organization, |
||
192 | 'image' => $this->imageObject |
||
193 | ]; |
||
194 | return $this->data; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param bool $tag |
||
199 | * @return string |
||
200 | */ |
||
201 | public function render(bool $tag = false): string |
||
202 | { |
||
203 | $render = [ |
||
204 | '@context' => Schema::CONTEXT, |
||
205 | '@type' => self::TYPE, |
||
206 | 'name' => $this->data->name, |
||
207 | 'description' => $this->data->description, |
||
208 | 'image' => $this->data->image, |
||
209 | 'url' => $this->data->url, |
||
210 | 'inLanguage' => $this->data->inLanguage, |
||
211 | 'isPartOf' => [$this->data->isPartOf], |
||
212 | 'about' => [$this->data->about], |
||
213 | 'creator' => [$this->data->organization, $this->data->person], |
||
214 | ]; |
||
215 | |||
216 | if ($tag) { |
||
217 | return '<script type="application/ld+json">' . $this->json($render) . '</script>'; |
||
218 | } |
||
219 | |||
220 | return $this->json($render); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Responsible method for debug all data |
||
225 | */ |
||
226 | public function debug(): void |
||
227 | { |
||
228 | var_dump($this->data()); |
||
0 ignored issues
–
show
Security
Debugging Code
introduced
by
![]() |
|||
229 | } |
||
230 | } |