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
|
|
|
* @throws \InvalidArgumentException |
57
|
|
|
* @throws \Error |
58
|
|
|
*/ |
59
|
18 |
|
public function __construct(string $hreflang, string $href) |
60
|
|
|
{ |
61
|
18 |
|
parent::__construct(); |
62
|
|
|
|
63
|
|
|
/** @var \Wszetko\Sitemap\Items\DataTypes\StringType $baseType */ |
64
|
18 |
|
$baseType = $this->hreflang->getBaseDataType(); |
65
|
|
|
$baseType |
66
|
18 |
|
->setValueRegex("/^(([a-z]{2}|(x))((-)([A-Za-z]{2}|[A-Z]([a-z]|[a-z]{3})|(default)))?)$/") |
67
|
18 |
|
->setRequired(true) |
68
|
|
|
; |
69
|
|
|
/** @var \Wszetko\Sitemap\Items\DataTypes\URLType $hrefAttribute */ |
70
|
18 |
|
$hrefAttribute = $baseType->getAttribute('href'); |
71
|
18 |
|
$hrefAttribute->setRequired(true); |
72
|
18 |
|
$this->addHreflang($hreflang, $href); |
73
|
16 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array |
77
|
|
|
* |
78
|
|
|
* @throws \Error |
79
|
|
|
*/ |
80
|
2 |
|
public function toArray(): array |
81
|
|
|
{ |
82
|
|
|
$array = [ |
83
|
2 |
|
'_namespace' => static::NAMESPACE_NAME, |
84
|
2 |
|
'_element' => 'link', |
85
|
|
|
'link' => [], |
86
|
|
|
]; |
87
|
|
|
|
88
|
2 |
|
foreach ($this->getHreflang() as $links) { |
89
|
2 |
|
foreach ($links as $hreflang => $lang) { |
90
|
2 |
|
$array['link'][] = [ |
91
|
|
|
'_attributes' => [ |
92
|
2 |
|
'rel' => 'alternate', |
93
|
2 |
|
'hreflang' => $hreflang, |
94
|
2 |
|
'href' => $lang['href'], |
95
|
|
|
], |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
return $array; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|