Passed
Push — master ( f57d20...5d5950 )
by Paweł
02:41
created

Hreflang   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 29
c 0
b 0
f 0
dl 0
loc 85
ccs 21
cts 21
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 3
A toArray() 0 21 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Wszetko Sitemap.
7
 *
8
 * (c) Paweł Kłopotek-Główczewski <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Wszetko\Sitemap\Items;
15
16
use InvalidArgumentException;
17
use Wszetko\Sitemap\Items\DataTypes\StringType;
18
19
/**
20
 * Class Hreflang.
21
 *
22
 * @package Wszetko\Sitemap\Items
23
 *
24
 * @method addHreflang($hreflang, $href)
25
 * @method setHreflang($hreflang, $href)
26
 * @method getHreflang()
27
 */
28
class Hreflang extends Extension
29
{
30
    /**
31
     * Name of Namescapce.
32
     */
33
    public const NAMESPACE_NAME = 'xhtml';
34
35
    /**
36
     * Namespace URL.
37
     */
38
    public const NAMESPACE_URL = 'http://www.w3.org/1999/xhtml';
39
40
    /**
41
     * Element name.
42
     */
43
    public const ELEMENT_NAME = 'link';
44
45
    /**
46
     * @dataType \Wszetko\Sitemap\Items\DataTypes\StringType
47
     * @attribute href
48
     * @attributeDataType \Wszetko\Sitemap\Items\DataTypes\URLType
49
     *
50
     * @var \Wszetko\Sitemap\Items\DataTypes\ArrayType
51
     */
52
    protected $hreflang;
53
54
    /**
55
     * @param string $hreflang
56
     * @param string $href
57
     *
58
     * @throws \ReflectionException
59
     * @throws \InvalidArgumentException
60
     * @throws \Error
61
     */
62 18
    public function __construct(string $hreflang, string $href)
63
    {
64 18
        parent::__construct();
65
66 18
        $baseType = $this->hreflang->getBaseDataType();
67
68 18
        if ($baseType instanceof StringType) {
69
            $baseType
70 18
                ->setValueRegex("/^(([a-z]{2}|(x))((-)([A-Za-z]{2}|[A-Z]([a-z]|[a-z]{3})|(default)))?)$/")
71 18
                ->setRequired(true)
72
            ;
73 18
            $hrefAttribute =  $baseType->getAttribute('href');
74
75 18
            if (null !== $hrefAttribute) {
76 18
                $hrefAttribute->setRequired(true);
77
            }
78
        } else {
79
            // @codeCoverageIgnoreStart
80
            throw new InvalidArgumentException('Class is missconfigured.');
81
            // @codeCoverageIgnoreEnd
82
        }
83
84 18
        $this->addHreflang($hreflang, $href);
85 16
    }
86
87
    /**
88
     * @return array
89
     *
90
     * @throws \Error
91
     */
92 2
    public function toArray(): array
93
    {
94
        $array = [
95 2
            '_namespace' => static::NAMESPACE_NAME,
96 2
            '_element' => 'link',
97
            'link' => [],
98
        ];
99
100 2
        foreach ($this->getHreflang() as $links) {
101 2
            foreach ($links as $hreflang => $lang) {
102 2
                $array['link'][] = [
103
                    '_attributes' => [
104 2
                        'rel' => 'alternate',
105 2
                        'hreflang' => $hreflang,
106 2
                        'href' => $lang['href'],
107
                    ],
108
                ];
109
            }
110
        }
111
112 2
        return $array;
113
    }
114
}
115