1 | <?php |
||
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) |
||
140 | |||
141 | /** |
||
142 | * @param string $type |
||
143 | * @param string $name |
||
144 | * |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function removeMeta($type, $name) |
||
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) |
||
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) |
||
225 | |||
226 | /** |
||
227 | * @param array $attributes |
||
228 | * |
||
229 | * @return SeoPageInterface |
||
230 | */ |
||
231 | public function setHeadAttributes(array $attributes) |
||
237 | |||
238 | /** |
||
239 | * @param string $name |
||
240 | * @param string $value |
||
241 | * |
||
242 | * @return SeoPageInterface |
||
243 | */ |
||
244 | public function addHeadAttribute($name, $value) |
||
250 | |||
251 | /** |
||
252 | * @param string $name |
||
253 | * |
||
254 | * @return $this |
||
255 | */ |
||
256 | public function removeHeadAttribute($name) |
||
262 | |||
263 | /** |
||
264 | * @return array |
||
265 | */ |
||
266 | public function getHeadAttributes() |
||
270 | |||
271 | /** |
||
272 | * @param string $name |
||
273 | * |
||
274 | * @return array |
||
275 | */ |
||
276 | public function hasHeadAttribute($name) |
||
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() |
||
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | */ |
||
310 | public function setSeparator($separator) |
||
316 | |||
317 | /** |
||
318 | * {@inheritdoc} |
||
319 | */ |
||
320 | public function setLangAlternates(array $langAlternates) |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | public function addLangAlternate($href, $hrefLang) |
||
336 | |||
337 | /** |
||
338 | * @param string $href |
||
339 | * |
||
340 | * @return $this |
||
341 | */ |
||
342 | public function removeLangAlternate($href) |
||
348 | |||
349 | /** |
||
350 | * @param string $href |
||
351 | * |
||
352 | * @return $this |
||
353 | */ |
||
354 | public function hasLangAlternate($href) |
||
358 | |||
359 | /** |
||
360 | * {@inheritdoc} |
||
361 | */ |
||
362 | public function getLangAlternates() |
||
366 | |||
367 | /** |
||
368 | * @param $title |
||
369 | * @param $link |
||
370 | * |
||
371 | * @return SeoPageInterface |
||
372 | */ |
||
373 | public function addOEmbedLink($title, $link) |
||
379 | |||
380 | /** |
||
381 | * @return array |
||
382 | */ |
||
383 | public function getOEmbedLinks() |
||
387 | |||
388 | /** |
||
389 | * @param mixed $meta |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | private function normalize($meta) |
||
401 | } |
||
402 |