Passed
Push — master ( c5d212...10120e )
by jelmer
300:24 queued 293:08
created

MetaLink::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 11
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Frontend\Core\Header;
4
5
use InvalidArgumentException;
6
7
final class MetaLink
8
{
9
    /** @var array */
10
    private $attributes;
11
12
    /** @var string */
13
    private $uniqueKey;
14
15
    /**
16
     * @param string $href since we always need a href we've added it as a separate parameter
17
     * @param string[] $attributes
18
     * @param string[] $uniqueAttributeKeys
19
     *
20
     * @throws InvalidArgumentException when the content is empty
21
     */
22 24
    public function __construct(
23
        string $href,
24
        array $attributes,
25
        array $uniqueAttributeKeys = ['rel', 'hreflang', 'type', 'title']
26
    ) {
27 24
        if (empty($href)) {
28
            throw new InvalidArgumentException('The href can not be empty');
29
        }
30
31 24
        $this->attributes = ['href' => $href] + $attributes;
32 24
        $this->createUniqueKey($uniqueAttributeKeys);
33 24
    }
34
35
    /**
36
     * @param string[] $uniqueAttributeKeys
37
     */
38 24
    private function createUniqueKey(array $uniqueAttributeKeys): void
39
    {
40
        // make sure the keys are sorted alphabetically
41 24
        sort($uniqueAttributeKeys);
42
43 24
        $this->uniqueKey = implode(
44 24
            '|',
45 24
            array_filter(
46 24
                array_map(
47 24
                    function (string $attributeKey) {
48 24
                        return $this->attributes[$attributeKey] ?? '';
49 24
                    },
50 24
                    $uniqueAttributeKeys
51
                )
52
            )
53
        );
54 24
    }
55
56 24
    public function getUniqueKey(): string
57
    {
58 24
        return $this->uniqueKey;
59
    }
60
61
    public function hasAttributeWithValue(string $attributeKey, string $attributeValue): bool
62
    {
63
        return isset($this->attributes[$attributeKey]) && $this->attributes[$attributeKey] === $attributeValue;
64
    }
65
66 24
    public static function canonical(string $href): self
67
    {
68 24
        return new self(
69 24
            $href,
70 24
            ['rel' => 'canonical']
71
        );
72
    }
73
74 6
    public static function rss(string $href, string $title): self
75
    {
76 6
        return new self(
77 6
            $href,
78 6
            ['rel' => 'alternate', 'type' => 'application/rss+xml', 'title' => $title]
79
        );
80
    }
81
82
    public static function alternateLanguage(string $href, string $language): self
83
    {
84
        return new self(
85
            $href,
86
            ['rel' => 'alternate', 'hreflang' => $language]
87
        );
88
    }
89
90
    public static function next(string $href): self
91
    {
92
        return new self(
93
            $href,
94
            ['rel' => 'next']
95
        );
96
    }
97
98
    public static function previous(string $href): self
99
    {
100
        return new self(
101
            $href,
102
            ['rel' => 'prev']
103
        );
104
    }
105
106 24
    public function __toString(): string
107
    {
108 24
        $html = '<link ';
109 24
        $html .= implode(
110 24
            ' ',
111 24
            array_map(
112 24
                function (string $parameterKey, string $parameterValue) {
113 24
                    return $parameterKey . '="' . $parameterValue . '"';
114 24
                },
115
                array_keys($this->attributes),
116
                $this->attributes
117
            )
118
        );
119
        $html .= '>';
120
121
        return $html;
122
    }
123
}
124