DefaultRenderer::initPreprocessors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 27
cts 27
cp 1
rs 9.408
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace SeoHelper\Renderer;
4
5
class DefaultRenderer extends AbstractRenderer
6
{
7
    protected $types = [
8
        'default' => '<meta name="{$key}" content="{$value}">',
9
        'title' => '<title>{$value}</title>',
10
        'canonical' => '<link rel="canonical" href="{$value}">',
11
        'next' => '<link rel="next" href="{$value}">',
12
        'prev' => '<link rel="prev" href="{$value}">',
13
    ];
14
15 27
    protected function getPattern($type)
16
    {
17 27
        return parent::getPattern($type) ?: $this->types['default'];
18
    }
19
20 9
    protected function initPreprocessors()
21
    {
22
        $this->setPreprocessor('default', function ($value) {
23 4
            return array_map(function ($val) {
24 6
                return htmlspecialchars(strip_tags($val));
25 6
            }, (array)$value);
26 27
        });
27 18
        $this->setPreprocessor('title', function ($value) {
28 9
            return strip_tags(implode(' | ', array_reverse($value)));
29 27
        });
30 18
        $this->setPreprocessor('description', function ($value) {
31 9
            return htmlspecialchars(strip_tags(implode(' ', $value)));
32 27
        });
33 18
        $this->setPreprocessor('keywords', function ($value) {
34 3
            return htmlspecialchars(strip_tags(implode(', ', array_unique($value))));
35 27
        });
36
        $this->setPreprocessor('canonical', function ($value) {
37 2
            return array_map(function ($val) {
38 3
                return htmlspecialchars(strip_tags($val));
39 3
            }, $value);
40 27
        });
41
        $this->setPreprocessor('next', function ($value) {
42 2
            return array_map(function ($val) {
43 3
                return htmlspecialchars(strip_tags($val));
44 3
            }, $value);
45 27
        });
46
        $this->setPreprocessor('prev', function ($value) {
47 3
            return array_map(function ($val) {
48 3
                return htmlspecialchars(strip_tags($val));
49 3
            }, $value);
50 27
        });
51 27
    }
52
53 27
    protected function preprocessValue($key, $value)
54
    {
55 27
        $preprocessor = isset($this->preprocessors[$key]) ? $this->preprocessors[$key] : $this->preprocessors['default'];
56 27
        return $preprocessor($value);
57
    }
58
}
59