|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2017 Ryan Parman <http://ryanparman.com>. |
|
4
|
|
|
* Copyright (c) 2017 Contributors. |
|
5
|
|
|
* |
|
6
|
|
|
* http://opensource.org/licenses/Apache2.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace SimplePie\Type; |
|
12
|
|
|
|
|
13
|
|
|
use DOMNode; |
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
use Psr\Log\NullLogger; |
|
16
|
|
|
use SimplePie\Configuration as C; |
|
17
|
|
|
use SimplePie\Mixin as T; |
|
18
|
|
|
use SimplePie\Type\Node; |
|
19
|
|
|
|
|
20
|
|
|
class Link extends AbstractType implements TypeInterface, C\SetLoggerInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use T\LoggerTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The DOMNode element to parse. |
|
26
|
|
|
* |
|
27
|
|
|
* @var DOMNode |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $node; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The link's remote location. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $href; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The link's relationship to the current document. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $rel; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The link's media type. |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $type; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The language of the link's remote location. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $hreflang; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The link's title. |
|
61
|
|
|
* |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $title; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* The link's length, in bytes (e.g., if it is a large file or direct download). |
|
68
|
|
|
* |
|
69
|
|
|
* @var int |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $length; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Constructs a new instance of this class. |
|
75
|
|
|
* |
|
76
|
|
|
* @param DOMNode|null $node The `DOMNode` element to parse. |
|
77
|
|
|
* @param LoggerInterface $logger The PSR-3 logger. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function __construct(?DOMNode $node = null, LoggerInterface $logger = null) |
|
80
|
|
|
{ |
|
81
|
|
|
if ($node) { |
|
82
|
|
|
$this->logger = $logger ?? new NullLogger(); |
|
83
|
|
|
$this->node = $node; |
|
84
|
|
|
$this->name = new Node($this->node); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
foreach ($this->node->attributes as $attribute) { |
|
87
|
|
|
$this->{$attribute->name} = new Node($attribute); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Converts this object into a string representation. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function __toString(): string |
|
98
|
|
|
{ |
|
99
|
|
|
return (string) $this->href ?? ''; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Gets the DOMNode element. |
|
104
|
|
|
* |
|
105
|
|
|
* @return DOMNode|null |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getNode(): ?DOMNode |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->node; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Finds the common internal alias for a given method name. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $nodeName The name of the method being called. |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function getAlias(string $nodeName): string |
|
120
|
|
|
{ |
|
121
|
|
|
switch ($nodeName) { |
|
122
|
|
|
case 'uri': |
|
123
|
|
|
case 'url': |
|
124
|
|
|
return 'href'; |
|
125
|
|
|
|
|
126
|
|
|
case 'relationship': |
|
127
|
|
|
return 'rel'; |
|
128
|
|
|
|
|
129
|
|
|
case 'mediaType': |
|
130
|
|
|
return 'type'; |
|
131
|
|
|
|
|
132
|
|
|
case 'lang': |
|
133
|
|
|
case 'language': |
|
134
|
|
|
return 'hreflang'; |
|
135
|
|
|
|
|
136
|
|
|
default: |
|
137
|
|
|
return $nodeName; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get the correct handler for a whitelisted method name. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $nodeName The name of the method being called. |
|
145
|
|
|
* |
|
146
|
|
|
* @throws SimplePieException |
|
147
|
|
|
* |
|
148
|
|
|
* @return Node |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function getHandler(string $nodeName): Node |
|
151
|
|
|
{ |
|
152
|
|
|
switch ($nodeName) { |
|
153
|
|
|
case 'href': |
|
154
|
|
|
case 'hreflang': |
|
155
|
|
|
case 'length': |
|
156
|
|
|
case 'rel': |
|
157
|
|
|
case 'title': |
|
158
|
|
|
case 'type': |
|
159
|
|
|
return $this->{$nodeName} ?? new Node(); |
|
160
|
|
|
|
|
161
|
|
|
default: |
|
162
|
|
|
throw new SimplePieException( |
|
|
|
|
|
|
163
|
|
|
\sprintf('%s is an unresolvable method.') |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|