Completed
Push — master ( 7fe6b0...d8a5ea )
by Paweł
02:19
created

HrefLang   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 72
ccs 19
cts 19
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A toArray() 0 19 2
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
/**
17
 * Class HrefLang.
18
 *
19
 * @package Wszetko\Sitemap\Items
20
 *
21
 * @method addHrefLang($hrefLang, $href)
22
 * @method setHrefLang($hrefLang, $href)
23
 * @method getHrefLang()
24
 */
25
class HrefLang extends Extension
26
{
27
    /**
28
     * Name of Namescapce.
29
     */
30
    public const NAMESPACE_NAME = 'xhtml';
31
32
    /**
33
     * Namespace URL.
34
     */
35
    public const NAMESPACE_URL = 'http://www.w3.org/1999/xhtml';
36
37
    /**
38
     * Element name.
39
     */
40
    public const ELEMENT_NAME = 'link';
41
42
    /**
43
     * @dataType \Wszetko\Sitemap\Items\DataTypes\StringType
44
     * @attribute href
45
     * @attributeDataType \Wszetko\Sitemap\Items\DataTypes\URLType
46
     *
47
     * @var \Wszetko\Sitemap\Items\DataTypes\ArrayType
48
     */
49
    protected $hrefLang;
50
51
    /**
52
     * @param string $hrefLang
53
     * @param string $href
54
     *
55
     * @throws \ReflectionException
56
     */
57 18
    public function __construct(string $hrefLang, string $href)
58
    {
59 18
        parent::__construct();
60
61 18
        $hrefLangValue = $this->hrefLang
62 18
            ->getBaseDataType()
63
        ;
64
        /* @var $hrefLangValue \Wszetko\Sitemap\Items\DataTypes\StringType */
65
        $hrefLangValue
66 18
            ->setRequired(true)
67 18
            ->setValueRegex("/^(?'hreflang'([a-z]{2}|(x))((-)([A-Za-z]{2}|[A-Z]([a-z]|[a-z]{3})|(default)))?)$/", 'hreflang')
68 18
            ->getAttribute('href')
69 18
            ->setRequired(true)
70
        ;
71
72 18
        $this->addHrefLang($hrefLang, $href);
73 16
    }
74
75
    /**
76
     * @return array
77
     */
78 2
    public function toArray(): array
79
    {
80
        $array = [
81 2
            '_namespace' => static::NAMESPACE_NAME,
82 2
            '_element' => 'link',
83
            'link' => [],
84
        ];
85
86 2
        foreach ($this->getHrefLang() as $hreflang => $lang) {
87 2
            $array['link'][] = [
88
                '_attributes' => [
89 2
                    'rel' => 'alternate',
90 2
                    'hreflang' => $hreflang,
91 2
                    'href' => $lang['href'],
92
                ],
93
            ];
94
        }
95
96 2
        return $array;
97
    }
98
}
99