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

SeoPage::removeLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
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
        $canonical = current($canonical) ? current($canonical) : array();
310
311
        if (array_key_exists('href', $canonical)) {
312
            return $canonical['href'];
313
        }
314
315
        return '';
316
    }
317
318
    /**
319
     * {@inheritdoc}
320
     */
321
    public function removeLinkCanonical()
322
    {
323
        $this->removeLink('canonical');
324
    }
325
326
    /**
327
     * {@inheritdoc}
328
     */
329
    public function setSeparator($separator)
330
    {
331
        $this->separator = $separator;
332
333
        return $this;
334
    }
335
336
    /**
337
     * {@inheritdoc}
338
     */
339
    public function setLangAlternates(array $langAlternates)
340
    {
341
        $this->langAlternates = $langAlternates;
342
343
        return $this;
344
    }
345
346
    /**
347
     * {@inheritdoc}
348
     */
349
    public function addLangAlternate($href, $hrefLang)
350
    {
351
        $this->langAlternates[$href] = $hrefLang;
352
353
        return $this;
354
    }
355
356
    /**
357
     * @param string $href
358
     *
359
     * @return $this
360
     */
361
    public function removeLangAlternate($href)
362
    {
363
        unset($this->langAlternates[$href]);
364
365
        return $this;
366
    }
367
368
    /**
369
     * @param string $href
370
     *
371
     * @return $this
372
     */
373
    public function hasLangAlternate($href)
374
    {
375
        return isset($this->langAlternates[$href]);
376
    }
377
378
    /**
379
     * {@inheritdoc}
380
     */
381
    public function getLangAlternates()
382
    {
383
        return  $this->langAlternates;
384
    }
385
386
    /**
387
     * @param $title
388
     * @param $link
389
     *
390
     * @return SeoPageInterface
391
     */
392
    public function addOEmbedLink($title, $link)
393
    {
394
        $this->oembedLinks[$title] = $link;
395
396
        return $this;
397
    }
398
399
    /**
400
     * @return array
401
     */
402
    public function getOEmbedLinks()
403
    {
404
        return $this->oembedLinks;
405
    }
406
407
    /**
408
     * {@inheritdoc}
409
     */
410
    public function addLink($type, array $attributes)
411
    {
412
        $this->links[$type][] = $attributes;
413
414
        return $this;
415
    }
416
417
    /**
418
     * {@inheritdoc}
419
     */
420
    public function setLink($type, array $attributes)
421
    {
422
        $this->links[$type] = array();
423
424
        return $this->addLink($type, $attributes);
425
    }
426
427
    /**
428
     * {@inheritdoc}
429
     */
430
    public function getLink($type)
431
    {
432
        if (array_key_exists($type, $this->links)) {
433
            return $this->links[$type];
434
        }
435
436
        return array();
437
    }
438
439
    /**
440
     * {@inheritdoc}
441
     */
442
    public function getLinks()
443
    {
444
        return $this->links;
445
    }
446
447
    /**
448
     * {@inheritdoc}
449
     */
450
    public function removeLink($type)
451
    {
452
        if (array_key_exists($type, $this->links)) {
453
            unset($this->links[$type]);
454
455
            return true;
456
        }
457
458
        return false;
459
    }
460
}
461