Completed
Pull Request — 2.x (#266)
by Maximilian
01:30
created

SeoPage::setStructuredData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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