|
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
|
|
|
use RuntimeException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* http://en.wikipedia.org/wiki/Meta_element. |
|
20
|
|
|
*/ |
|
21
|
|
|
final class SeoPage implements SeoPageInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $title; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $metas; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $htmlAttributes; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $linkCanonical; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $separator; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $headAttributes; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $langAlternates; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $oembedLinks; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $title |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct($title = '') |
|
67
|
|
|
{ |
|
68
|
|
|
$this->title = $title; |
|
69
|
|
|
$this->metas = [ |
|
70
|
|
|
'http-equiv' => [], |
|
71
|
|
|
'name' => [], |
|
72
|
|
|
'schema' => [], |
|
73
|
|
|
'charset' => [], |
|
74
|
|
|
'property' => [], |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
$this->htmlAttributes = []; |
|
78
|
|
|
$this->headAttributes = []; |
|
79
|
|
|
$this->linkCanonical = ''; |
|
80
|
|
|
$this->separator = ' '; |
|
81
|
|
|
$this->langAlternates = []; |
|
82
|
|
|
$this->oembedLinks = []; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function setTitle($title) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->title = $title; |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function addTitle($title) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->title = $title.$this->separator.$this->title; |
|
95
|
|
|
|
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getTitle() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->title; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getMetas() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->metas; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function addMeta($type, $name, $content, array $extras = []) |
|
110
|
|
|
{ |
|
111
|
|
|
if (!\is_string($content)) { |
|
112
|
|
|
@trigger_error(sprintf( |
|
|
|
|
|
|
113
|
|
|
'Passing meta content of type %s in %s is deprecated since version 2.8 and will be unsupported in version 3. Please cast the value to a string first.', |
|
114
|
|
|
\gettype($content), |
|
115
|
|
|
__METHOD__ |
|
116
|
|
|
), E_USER_DEPRECATED); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if (!isset($this->metas[$type])) { |
|
120
|
|
|
$this->metas[$type] = []; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$this->metas[$type][$name] = [$content, $extras]; |
|
124
|
|
|
|
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function hasMeta($type, $name) |
|
129
|
|
|
{ |
|
130
|
|
|
return isset($this->metas[$type][$name]); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function removeMeta($type, $name) |
|
134
|
|
|
{ |
|
135
|
|
|
unset($this->metas[$type][$name]); |
|
136
|
|
|
|
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function setMetas(array $metadatas) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->metas = []; |
|
143
|
|
|
|
|
144
|
|
|
foreach ($metadatas as $type => $metas) { |
|
145
|
|
|
if (!\is_array($metas)) { |
|
146
|
|
|
throw new RuntimeException('$metas must be an array'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
foreach ($metas as $name => $meta) { |
|
150
|
|
|
list($content, $extras) = $this->normalize($meta); |
|
151
|
|
|
|
|
152
|
|
|
$this->addMeta($type, $name, $content, $extras); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function setHtmlAttributes(array $attributes) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->htmlAttributes = $attributes; |
|
162
|
|
|
|
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function addHtmlAttributes($name, $value) |
|
167
|
|
|
{ |
|
168
|
|
|
$this->htmlAttributes[$name] = $value; |
|
169
|
|
|
|
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function removeHtmlAttributes($name) |
|
174
|
|
|
{ |
|
175
|
|
|
unset($this->htmlAttributes[$name]); |
|
176
|
|
|
|
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function getHtmlAttributes() |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->htmlAttributes; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function hasHtmlAttribute($name) |
|
186
|
|
|
{ |
|
187
|
|
|
return isset($this->htmlAttributes[$name]); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public function setHeadAttributes(array $attributes) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->headAttributes = $attributes; |
|
193
|
|
|
|
|
194
|
|
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function addHeadAttribute($name, $value) |
|
198
|
|
|
{ |
|
199
|
|
|
$this->headAttributes[$name] = $value; |
|
200
|
|
|
|
|
201
|
|
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function removeHeadAttribute($name) |
|
205
|
|
|
{ |
|
206
|
|
|
unset($this->headAttributes[$name]); |
|
207
|
|
|
|
|
208
|
|
|
return $this; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @return array |
|
213
|
|
|
*/ |
|
214
|
|
|
public function getHeadAttributes() |
|
215
|
|
|
{ |
|
216
|
|
|
return $this->headAttributes; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function hasHeadAttribute($name) |
|
220
|
|
|
{ |
|
221
|
|
|
return isset($this->headAttributes[$name]); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function setLinkCanonical($link) |
|
225
|
|
|
{ |
|
226
|
|
|
$this->linkCanonical = $link; |
|
227
|
|
|
|
|
228
|
|
|
return $this; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function getLinkCanonical() |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->linkCanonical; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function removeLinkCanonical(): void |
|
237
|
|
|
{ |
|
238
|
|
|
$this->linkCanonical = ''; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
public function setSeparator($separator) |
|
242
|
|
|
{ |
|
243
|
|
|
$this->separator = $separator; |
|
244
|
|
|
|
|
245
|
|
|
return $this; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
public function setLangAlternates(array $langAlternates) |
|
249
|
|
|
{ |
|
250
|
|
|
$this->langAlternates = $langAlternates; |
|
251
|
|
|
|
|
252
|
|
|
return $this; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
public function addLangAlternate($href, $hrefLang) |
|
256
|
|
|
{ |
|
257
|
|
|
$this->langAlternates[$href] = $hrefLang; |
|
258
|
|
|
|
|
259
|
|
|
return $this; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
public function removeLangAlternate($href) |
|
263
|
|
|
{ |
|
264
|
|
|
unset($this->langAlternates[$href]); |
|
265
|
|
|
|
|
266
|
|
|
return $this; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function hasLangAlternate($href) |
|
270
|
|
|
{ |
|
271
|
|
|
return isset($this->langAlternates[$href]); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
public function getLangAlternates() |
|
275
|
|
|
{ |
|
276
|
|
|
return $this->langAlternates; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
public function addOEmbedLink($title, $link) |
|
280
|
|
|
{ |
|
281
|
|
|
$this->oembedLinks[$title] = $link; |
|
282
|
|
|
|
|
283
|
|
|
return $this; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @return array |
|
288
|
|
|
*/ |
|
289
|
|
|
public function getOEmbedLinks() |
|
290
|
|
|
{ |
|
291
|
|
|
return $this->oembedLinks; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* @param mixed $meta |
|
296
|
|
|
* |
|
297
|
|
|
* @return array |
|
298
|
|
|
*/ |
|
299
|
|
|
private function normalize($meta) |
|
300
|
|
|
{ |
|
301
|
|
|
if (\is_string($meta)) { |
|
302
|
|
|
return [$meta, []]; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
return $meta; |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: