-
Notifications
You must be signed in to change notification settings - Fork 2
/
RSS.php
57 lines (52 loc) · 1.58 KB
/
RSS.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
<?php
namespace Lucinda\RSS;
/**
* Encapsulates a RSS feed itself according to specifications:
* https://www.rssboard.org/rss-profile#element-rss
*/
class RSS implements Tag
{
private Channel $channel;
/**
* @var array<string,string>
*/
private array $namespaces = [];
/**
* Constructs feed based on mandatory RSS channel described by specification:
* https://www.rssboard.org/rss-profile#element-channel
*
* @param Channel $channel Encapsulated channel RSS tag
*/
public function __construct(Channel $channel)
{
$this->channel = $channel;
}
/**
* Adds a RSS namespace able to be used in defining custom functionality to feed, as exemplified by specification:
* https://www.rssboard.org/rss-profile#namespace-elements
*
* @param string $name Name of custom RSS namespace (without 'xmlns:')
* @param string $url URL where specification is defined
* @throws Exception
*/
public function addNamespace(string $name, string $url): void
{
if (!filter_var($url, FILTER_VALIDATE_URL)) {
throw new Exception("Docs is invalid");
}
$this->namespaces[$name] = $url;
}
/**
* {@inheritDoc}
*
* @see Tag::__toString()
*/
public function __toString(): string
{
$attributes = 'version="2.0"';
foreach ($this->namespaces as $name=>$url) {
$attributes.=' xmlns:'.$name.'="'.$url.'"';
}
return '<?xml version="1.0" encoding="UTF-8"?><rss '.$attributes.'>'.$this->channel.'</rss>';
}
}