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