1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2017–2018 Ryan Parman <http://ryanparman.com>. |
4
|
|
|
* Copyright (c) 2017–2018 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\Exception\SimplePieException; |
18
|
|
|
use SimplePie\Mixin\LoggerTrait; |
19
|
|
|
use SimplePie\Parser\Date as DateParser; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A type model for an Entry element. |
23
|
|
|
* |
24
|
|
|
* @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-Atom-1.0#412-the-atomentry-element |
25
|
|
|
* @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-1.0#535-items |
26
|
|
|
* @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-1.0#55-item |
27
|
|
|
* @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-RSS-2.0#elements-of-item |
28
|
|
|
* @see https://github.com/simplepie/simplepie-ng/wiki/Spec%3A-JSON-Feed-v1#items |
29
|
|
|
*/ |
30
|
|
|
class Entry extends AbstractType implements TypeInterface, C\SetLoggerInterface |
31
|
|
|
{ |
32
|
|
|
use LoggerTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The DOMNode element to parse. |
36
|
|
|
* |
37
|
|
|
* @var DOMNode |
38
|
|
|
*/ |
39
|
|
|
protected $node; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructs a new instance of this class. |
43
|
|
|
* |
44
|
|
|
* @param DOMNode|null $node The `DOMNode` element to parse. |
45
|
|
|
* @param LoggerInterface $logger The PSR-3 logger. |
46
|
|
|
*/ |
47
|
|
|
public function __construct(?DOMNode $node = null, LoggerInterface $logger = null) |
48
|
|
|
{ |
49
|
|
|
if ($node) { |
50
|
|
|
$this->logger = $logger ?? new NullLogger(); |
51
|
|
|
$this->node = $node; |
52
|
|
|
|
53
|
|
|
// foreach ($this->node->attributes as $attribute) { |
54
|
|
|
// $this->{$attribute->name} = new Node($attribute); |
55
|
|
|
// } |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Converts this object into a string representation. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function __toString(): string |
65
|
|
|
{ |
66
|
|
|
return \sprintf('<%s: resource %s>', \get_called_class(), \md5(\spl_object_hash($this))); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Gets the DOMNode element. |
71
|
|
|
* |
72
|
|
|
* @return DOMNode|null |
73
|
|
|
*/ |
74
|
|
|
public function getNode(): ?DOMNode |
75
|
|
|
{ |
76
|
|
|
return $this->node; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Finds the common internal alias for a given method name. |
81
|
|
|
* |
82
|
|
|
* @param string $nodeName The name of the method being called. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getAlias(string $nodeName): string |
87
|
|
|
{ |
88
|
|
|
switch ($nodeName) { |
89
|
|
|
case 'categories': |
90
|
|
|
return 'category'; |
91
|
|
|
|
92
|
|
|
case 'contributors': |
93
|
|
|
return 'contributor'; |
94
|
|
|
|
95
|
|
|
case 'language': |
96
|
|
|
return 'lang'; |
97
|
|
|
|
98
|
|
|
case 'links': |
99
|
|
|
return 'link'; |
100
|
|
|
|
101
|
|
|
case 'pubDate': |
102
|
|
|
case 'publishDate': |
103
|
|
|
case 'publishedDate': |
104
|
|
|
return 'published'; |
105
|
|
|
|
106
|
|
|
default: |
107
|
|
|
return $nodeName; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the correct handler for a whitelisted method name. |
113
|
|
|
* |
114
|
|
|
* @param string $nodeName The name of the method being called. |
115
|
|
|
* @param array $args Any arguments passed into that method. |
116
|
|
|
* |
117
|
|
|
* @throws SimplePieException |
118
|
|
|
* |
119
|
|
|
* @return mixed |
120
|
|
|
* |
121
|
|
|
* phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded |
122
|
|
|
*/ |
123
|
|
|
protected function getHandler(string $nodeName, array $args) |
124
|
|
|
{ |
125
|
|
|
switch ($nodeName) { |
126
|
|
|
case 'id': |
127
|
|
|
case 'lang': |
128
|
|
|
case 'rights': |
129
|
|
|
case 'subtitle': |
130
|
|
|
case 'summary': |
131
|
|
|
case 'title': |
132
|
|
|
return $this->getScalarSingleValue($nodeName, $args[0]); |
133
|
|
|
|
134
|
|
|
case 'published': |
135
|
|
|
case 'updated': |
136
|
|
|
return (new DateParser( |
137
|
|
|
$this->getScalarSingleValue($nodeName, $args[0])->getValue(), |
138
|
|
|
$this->outputTimezone, |
|
|
|
|
139
|
|
|
$this->createFromFormat |
|
|
|
|
140
|
|
|
))->getDateTime(); |
141
|
|
|
|
142
|
|
|
case 'author': |
143
|
|
|
return $this->getComplexSingleValue($nodeName, Person::class, $args[0]); |
144
|
|
|
|
145
|
|
|
case 'generator': |
146
|
|
|
return $this->getComplexSingleValue($nodeName, Generator::class, $args[0]); |
147
|
|
|
|
148
|
|
|
case 'category': |
149
|
|
|
case 'contributor': |
150
|
|
|
case 'link': |
151
|
|
|
return $this->getComplexMultipleValues($nodeName, $args[0]); |
152
|
|
|
|
153
|
|
|
default: |
154
|
|
|
throw new SimplePieException( |
155
|
|
|
$this->getUnresolvableMessage($nodeName) |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// phpcs:enable |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Retrieves nodes that are simple scalars, and there is only one allowed value. |
164
|
|
|
* |
165
|
|
|
* @param string $nodeName The name of the tree node to retrieve. Available tree nodes can be viewed by |
166
|
|
|
* looking at the response from `getRoot()`. |
167
|
|
|
* @param string|null $namespaceAlias The XML namespace alias to apply. |
168
|
|
|
* |
169
|
|
|
* @return Node |
170
|
|
|
*/ |
171
|
|
|
protected function getScalarSingleValue(string $nodeName, ?string $namespaceAlias = null): Node |
172
|
|
|
{ |
173
|
|
|
$alias = $namespaceAlias ?? $this->namespaceAlias; |
|
|
|
|
174
|
|
|
|
175
|
|
|
if (isset($this->getRoot()->item->{$nodeName}[$alias])) { |
|
|
|
|
176
|
|
|
return $this->getRoot()->item->{$nodeName}[$alias]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return new Node(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Retrieves nodes that are complex types, and there is only one allowed value. |
184
|
|
|
* |
185
|
|
|
* @param string $nodeName The name of the tree node to retrieve. Available tree nodes can be viewed by |
186
|
|
|
* looking at the response from `getRoot()`. |
187
|
|
|
* @param string $className The class name to instantiate when there is not a defined value. |
188
|
|
|
* @param string|null $namespaceAlias The XML namespace alias to apply. |
189
|
|
|
* |
190
|
|
|
* @return TypeInterface |
191
|
|
|
* |
192
|
|
|
* phpcs:disable Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine |
193
|
|
|
*/ |
194
|
|
|
protected function getComplexSingleValue( |
195
|
|
|
string $nodeName, |
196
|
|
|
string $className, |
197
|
|
|
?string $namespaceAlias = null |
198
|
|
|
): TypeInterface { |
199
|
|
|
// phpcs:enable |
200
|
|
|
|
201
|
|
|
$alias = $namespaceAlias ?? $this->namespaceAlias; |
|
|
|
|
202
|
|
|
|
203
|
|
|
if (isset($this->getRoot()->item->{$nodeName}[$alias])) { |
|
|
|
|
204
|
|
|
return new $className($this->getRoot()->item->{$nodeName}[$alias]->getNode()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return new $className(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Retrieves nodes that are complex types, and there may be are more than one value. |
212
|
|
|
* |
213
|
|
|
* @param string $nodeName The name of the tree node to retrieve. Available tree nodes can be viewed by |
214
|
|
|
* looking at the response from `getRoot()`. |
215
|
|
|
* @param string|null $namespaceAlias The XML namespace alias to apply. |
216
|
|
|
* |
217
|
|
|
* @return iterable |
218
|
|
|
*/ |
219
|
|
|
protected function getComplexMultipleValues(string $nodeName, ?string $namespaceAlias = null): iterable |
220
|
|
|
{ |
221
|
|
|
$alias = $namespaceAlias ?? $this->namespaceAlias; |
|
|
|
|
222
|
|
|
|
223
|
|
|
if (isset($this->getRoot()->item->{$nodeName}[$alias])) { |
|
|
|
|
224
|
|
|
return $this->getRoot()->item->{$nodeName}[$alias]; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return []; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|