Completed
Pull Request — master (#116)
by
unknown
02:33
created

SeoPage::getLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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