-
Notifications
You must be signed in to change notification settings - Fork 0
/
Feed.php
237 lines (193 loc) · 5.93 KB
/
Feed.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
declare(strict_types = 1);
namespace AtomGenerator;
use DOMDocument;
use LibXMLError;
use SimpleXMLElement;
use Webmozart\Assert\Assert;
class Feed extends AbstractElement
{
/** @var Entry[] */
protected $entries = [];
/** @var bool */
protected $prettify = false;
/** @var null|string */
protected $language;
/** @var null|string */
protected $subtitle;
/** @var null|string */
protected $icon;
/** @var null|string */
protected $logo;
/** @var null|string */
protected $generator;
/** @var null|string */
protected $generatorVersion;
/** @var null|string */
protected $generatorUri;
/**
* @var array[]
*
* @phpstan-var array<array{ns: string, uri: string, name: string, value: string, attributes: string[]}>
*/
protected $customElements = [];
/**
* Feed constructor.
*/
public function __construct()
{
parent::__construct();
$this->setPrettify(true);
}
public function setPrettify(bool $prettify): void
{
$this->prettify = $prettify;
}
public function setLanguage(?string $language): void
{
$this->language = $language;
}
public function setSubtitle(?string $subtitle): void
{
$this->subtitle = $subtitle;
}
public function setIconUri(?string $uri): void
{
if (null !== $uri) {
self::assertURL($uri);
}
$this->icon = $uri;
}
public function setLogoUri(?string $uri): void
{
if (null !== $uri) {
self::assertURL($uri);
}
$this->logo = $uri;
}
public function setGenerator(?string $generator, ?string $uri = null, ?string $version = null): void
{
Assert::true((null !== $generator) || ((null === $uri) && (null === $version)));
if (null !== $uri) {
self::assertURL($uri);
}
$this->generator = $generator;
$this->generatorVersion = $version;
$this->generatorUri = $uri;
}
public function addEntry(Entry $entry): void
{
$this->entries[] = $entry;
}
/**
* @param Entry[] $entries
*/
public function setEntries(array $entries): void
{
$this->entries = $entries;
}
/**
* @return Entry[]
*/
public function getEntries(): array
{
return $this->entries;
}
/**
* @param null|string[] $attributes
*/
public function addCustomElement(string $ns, string $uri, string $name, string $value, ?array $attributes = null): void
{
self::assertURL($uri);
$this->customElements[] = [
'ns' => $ns,
'uri' => $uri,
'name' => $name,
'value' => $value,
'attributes' => $attributes ?? [],
];
}
public function addChildrenTo(SimpleXMLElement $parent): void
{
parent::addChildrenTo($parent);
if (null !== $this->subtitle) {
$parent->addChild('subtitle', htmlspecialchars($this->subtitle));
}
if (null !== $this->logo) {
$parent->addChild('logo', htmlspecialchars($this->logo));
}
if (null !== $this->icon) {
$parent->addChild('icon', htmlspecialchars($this->icon));
}
if (null !== $this->generator) {
$generator = $parent->addChild('generator', htmlspecialchars($this->generator));
assert(null !== $generator);
if (null !== $this->generatorVersion) {
$generator->addAttribute('version', $this->generatorVersion);
}
if (null !== $this->generatorUri) {
$generator->addAttribute('uri', $this->generatorUri);
}
}
foreach ($this->customElements as $customElement) {
$element = $parent->addChild($customElement['name'], htmlspecialchars($customElement['value']), $customElement['uri']);
assert(null !== $element);
foreach ($customElement['attributes'] as $name => $value) {
$element->addAttribute($name, $value);
}
}
foreach ($this->entries as $entry) {
$entry->addChildrenTo($parent);
}
}
public function getSimpleXML(): SimpleXMLElement
{
$attributes = [];
if (null !== $this->language) {
$attributes['xml:lang'] = $this->language;
}
foreach ($this->customElements as $customElement) {
$attributes['xmlns:'.$customElement['ns']] = $customElement['uri'];
}
$attributesString = '';
foreach ($attributes as $name => $attribute) {
$attributesString .= ' '.$name.'="'.htmlspecialchars($attribute).'"';
}
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"'.$attributesString.' />');
$this->addChildrenTo($xml);
return $xml;
}
public function getDocument(): DOMDocument
{
$node = dom_import_simplexml($this->getSimpleXML());
$no = $node->ownerDocument;
assert(null !== $no);
return $no;
}
/**
* @return false|string
*/
public function saveXML()
{
$dom = $this->getDocument();
$dom->preserveWhiteSpace = !$this->prettify;
$dom->formatOutput = $this->prettify;
return $dom->saveXML();
}
/**
* Validate XML DOMDocument against an ATOM Relax NG schema.
* RNG schema coming from https://validator.w3.org/feed/docs/rfc4287.html#schema.
*
* @see https://cweiske.de/tagebuch/atom-validation.htm
*
* @param null|LibXMLError[] $errors
*/
public static function validate(DOMDocument $document, ?array &$errors = null): bool
{
libxml_use_internal_errors(true);
$valid = $document->relaxNGValidate(__DIR__.'/atom.rng');
$errors = libxml_get_errors();
libxml_clear_errors();
return $valid;
}
}