Completed
Push — master ( b035c8...ddcf44 )
by Paweł
02:10
created

HrefLang::addHrefLang()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 2
rs 9.8333
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Wszetko\Sitemap\Items;
5
6
use InvalidArgumentException;
7
8
/**
9
 * Class HrefLang
10
 *
11
 * @package Wszetko\Sitemap\Items
12
 * @method addHrefLang($hrefLang, $href)
13
 * @method setHrefLang($hrefLang, $href)
14
 * @method getHrefLang()
15
 */
16
class HrefLang extends Extension
17
{
18
    /**
19
     * Name of Namescapce
20
     */
21
    const NAMESPACE_NAME = 'xhtml';
22
23
    /**
24
     * Namespace URL
25
     */
26
    const NAMESPACE_URL = 'http://www.w3.org/1999/xhtml';
27
28
    /**
29
     * Element name
30
     */
31
    const ELEMENT_NAME = 'link';
32
33
    /**
34
     * @dataType \Wszetko\Sitemap\Items\DataTypes\StringType
35
     * @attribute href
36
     * @attributeDataType \Wszetko\Sitemap\Items\DataTypes\URLType
37
     * @var \Wszetko\Sitemap\Items\DataTypes\ArrayType
38
     */
39
    protected $hrefLang;
40
41
    /**
42
     * @param string $hrefLang
43
     * @param string $href
44
     *
45
     * @return self
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
46
     * @throws \ReflectionException
47
     */
48 3
    public function __construct(string $hrefLang, string $href)
49
    {
50 3
        parent::__construct();
51
52 3
        $this->hrefLang
0 ignored issues
show
Bug introduced by
The method setValueRegex() does not exist on Wszetko\Sitemap\Items\DataTypes\AbstractDataType. Did you maybe mean setValue()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
53 3
            ->getBaseDataType()
54 3
            ->setRequired(true)
55 3
            ->setValueRegex("/^(?'hreflang'([a-z]{2}|(x))((-)([A-Za-z]{2}|[A-Z]([a-z]|[a-z]{3})|(default)))?)$/", 'hreflang');
56 3
        $this->hrefLang
57 3
            ->getBaseDataType()
58 3
            ->getAttribute('href')
59 3
            ->setRequired(true);
60
61 3
        $this->addHrefLang($hrefLang, $href);
62 3
    }
63
64
    /**
65
     * @return array
66
     */
67 1
    public function toArray(): array
68
    {
69
        $array = [
70 1
            '_namespace' => static::NAMESPACE_NAME,
71 1
            '_element' => 'link',
72
            'link' => []
73
        ];
74
75 1
        foreach ($this->getHrefLang() as $hreflang => $lang) {
76 1
            $array['link'][] = [
77
                '_attributes' => [
78 1
                    'rel' => 'alternate',
79 1
                    'hreflang' => $hreflang,
80 1
                    'href' => $lang['href']
81
                ]
82
            ];
83
        }
84
85 1
        return $array;
86
    }
87
}
88