1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\SeoBundle\Seo; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* http://en.wikipedia.org/wiki/Meta_element. |
18
|
|
|
*/ |
19
|
|
|
class SeoPage implements SeoPageInterface, PageWithStructuredData |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $title; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $metas; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $htmlAttributes; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $linkCanonical; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $separator; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $headAttributes; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $langAlternates; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $oembedLinks; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* A JSON-LD string, which can be served as structured data. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private $structuredData; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $title |
70
|
|
|
*/ |
71
|
|
|
public function __construct($title = '') |
72
|
|
|
{ |
73
|
|
|
$this->title = $title; |
74
|
|
|
$this->metas = [ |
75
|
|
|
'http-equiv' => [], |
76
|
|
|
'name' => [], |
77
|
|
|
'schema' => [], |
78
|
|
|
'charset' => [], |
79
|
|
|
'property' => [], |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$this->htmlAttributes = []; |
83
|
|
|
$this->headAttributes = []; |
84
|
|
|
$this->linkCanonical = ''; |
85
|
|
|
$this->separator = ' '; |
86
|
|
|
$this->langAlternates = []; |
87
|
|
|
$this->oembedLinks = []; |
88
|
|
|
$this->structuredData = ''; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function setTitle($title) |
95
|
|
|
{ |
96
|
|
|
$this->title = $title; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function addTitle($title) |
105
|
|
|
{ |
106
|
|
|
$this->title = $title.$this->separator.$this->title; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function getTitle() |
115
|
|
|
{ |
116
|
|
|
return $this->title; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function getMetas() |
123
|
|
|
{ |
124
|
|
|
return $this->metas; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function addMeta($type, $name, $content, array $extras = []) |
131
|
|
|
{ |
132
|
|
|
if (!isset($this->metas[$type])) { |
133
|
|
|
$this->metas[$type] = []; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->metas[$type][$name] = [$content, $extras]; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $type |
143
|
|
|
* @param string $name |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function hasMeta($type, $name) |
148
|
|
|
{ |
149
|
|
|
return isset($this->metas[$type][$name]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $type |
154
|
|
|
* @param string $name |
155
|
|
|
* |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
|
|
public function removeMeta($type, $name) |
159
|
|
|
{ |
160
|
|
|
unset($this->metas[$type][$name]); |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
|
|
public function setMetas(array $metadatas) |
169
|
|
|
{ |
170
|
|
|
$this->metas = []; |
171
|
|
|
|
172
|
|
|
foreach ($metadatas as $type => $metas) { |
173
|
|
|
if (!\is_array($metas)) { |
174
|
|
|
throw new \RuntimeException('$metas must be an array'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
foreach ($metas as $name => $meta) { |
178
|
|
|
list($content, $extras) = $this->normalize($meta); |
179
|
|
|
|
180
|
|
|
$this->addMeta($type, $name, $content, $extras); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* {@inheritdoc} |
189
|
|
|
*/ |
190
|
|
|
public function setHtmlAttributes(array $attributes) |
191
|
|
|
{ |
192
|
|
|
$this->htmlAttributes = $attributes; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
|
|
public function addHtmlAttributes($name, $value) |
201
|
|
|
{ |
202
|
|
|
$this->htmlAttributes[$name] = $value; |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string $name |
209
|
|
|
* |
210
|
|
|
* @return $this |
211
|
|
|
*/ |
212
|
|
|
public function removeHtmlAttributes($name) |
213
|
|
|
{ |
214
|
|
|
unset($this->htmlAttributes[$name]); |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* {@inheritdoc} |
221
|
|
|
*/ |
222
|
|
|
public function getHtmlAttributes() |
223
|
|
|
{ |
224
|
|
|
return $this->htmlAttributes; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param string $name |
229
|
|
|
* |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
public function hasHtmlAttribute($name) |
233
|
|
|
{ |
234
|
|
|
return isset($this->htmlAttributes[$name]); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param array $attributes |
239
|
|
|
* |
240
|
|
|
* @return SeoPageInterface |
241
|
|
|
*/ |
242
|
|
|
public function setHeadAttributes(array $attributes) |
243
|
|
|
{ |
244
|
|
|
$this->headAttributes = $attributes; |
245
|
|
|
|
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param string $name |
251
|
|
|
* @param string $value |
252
|
|
|
* |
253
|
|
|
* @return SeoPageInterface |
254
|
|
|
*/ |
255
|
|
|
public function addHeadAttribute($name, $value) |
256
|
|
|
{ |
257
|
|
|
$this->headAttributes[$name] = $value; |
258
|
|
|
|
259
|
|
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param string $name |
264
|
|
|
* |
265
|
|
|
* @return $this |
266
|
|
|
*/ |
267
|
|
|
public function removeHeadAttribute($name) |
268
|
|
|
{ |
269
|
|
|
unset($this->headAttributes[$name]); |
270
|
|
|
|
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @return array |
276
|
|
|
*/ |
277
|
|
|
public function getHeadAttributes() |
278
|
|
|
{ |
279
|
|
|
return $this->headAttributes; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param string $name |
284
|
|
|
* |
285
|
|
|
* @return array |
286
|
|
|
*/ |
287
|
|
|
public function hasHeadAttribute($name) |
288
|
|
|
{ |
289
|
|
|
return isset($this->headAttributes[$name]); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* {@inheritdoc} |
294
|
|
|
*/ |
295
|
|
|
public function setLinkCanonical($link) |
296
|
|
|
{ |
297
|
|
|
$this->linkCanonical = $link; |
298
|
|
|
|
299
|
|
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* {@inheritdoc} |
304
|
|
|
*/ |
305
|
|
|
public function getLinkCanonical() |
306
|
|
|
{ |
307
|
|
|
return $this->linkCanonical; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* {@inheritdoc} |
312
|
|
|
*/ |
313
|
|
|
public function removeLinkCanonical() |
314
|
|
|
{ |
315
|
|
|
$this->linkCanonical = ''; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* {@inheritdoc} |
320
|
|
|
*/ |
321
|
|
|
public function setSeparator($separator) |
322
|
|
|
{ |
323
|
|
|
$this->separator = $separator; |
324
|
|
|
|
325
|
|
|
return $this; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* {@inheritdoc} |
330
|
|
|
*/ |
331
|
|
|
public function setLangAlternates(array $langAlternates) |
332
|
|
|
{ |
333
|
|
|
$this->langAlternates = $langAlternates; |
334
|
|
|
|
335
|
|
|
return $this; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* {@inheritdoc} |
340
|
|
|
*/ |
341
|
|
|
public function addLangAlternate($href, $hrefLang) |
342
|
|
|
{ |
343
|
|
|
$this->langAlternates[$href] = $hrefLang; |
344
|
|
|
|
345
|
|
|
return $this; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @param string $href |
350
|
|
|
* |
351
|
|
|
* @return $this |
352
|
|
|
*/ |
353
|
|
|
public function removeLangAlternate($href) |
354
|
|
|
{ |
355
|
|
|
unset($this->langAlternates[$href]); |
356
|
|
|
|
357
|
|
|
return $this; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @param string $href |
362
|
|
|
* |
363
|
|
|
* @return $this |
364
|
|
|
*/ |
365
|
|
|
public function hasLangAlternate($href) |
366
|
|
|
{ |
367
|
|
|
return isset($this->langAlternates[$href]); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* {@inheritdoc} |
372
|
|
|
*/ |
373
|
|
|
public function getLangAlternates() |
374
|
|
|
{ |
375
|
|
|
return $this->langAlternates; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @param $title |
380
|
|
|
* @param $link |
381
|
|
|
* |
382
|
|
|
* @return SeoPageInterface |
383
|
|
|
*/ |
384
|
|
|
public function addOEmbedLink($title, $link) |
385
|
|
|
{ |
386
|
|
|
$this->oembedLinks[$title] = $link; |
387
|
|
|
|
388
|
|
|
return $this; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @return array |
393
|
|
|
*/ |
394
|
|
|
public function getOEmbedLinks() |
395
|
|
|
{ |
396
|
|
|
return $this->oembedLinks; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* {@inheritdoc} |
401
|
|
|
*/ |
402
|
|
|
public function getStructuredData() |
403
|
|
|
{ |
404
|
|
|
return $this->structuredData; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
public function setStructuredData($structuredData) |
408
|
|
|
{ |
409
|
|
|
$this->structuredData = $structuredData; |
410
|
|
|
|
411
|
|
|
return $this; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* @param mixed $meta |
416
|
|
|
* |
417
|
|
|
* @return array |
418
|
|
|
*/ |
419
|
|
|
private function normalize($meta) |
420
|
|
|
{ |
421
|
|
|
if (\is_string($meta)) { |
422
|
|
|
return [$meta, []]; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
return $meta; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* @param mixed $meta |
430
|
|
|
* |
431
|
|
|
* @return array |
432
|
|
|
*/ |
433
|
|
|
private function normalize($meta) |
434
|
|
|
{ |
435
|
|
|
if (\is_string($meta)) { |
436
|
|
|
return [$meta, []]; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
return $meta; |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
|