1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bhaktaraz\RSSGenerator; |
4
|
|
|
|
5
|
|
|
use Bhaktaraz\RSSGenerator\ItemInterface; |
6
|
|
|
use Bhaktaraz\RSSGenerator\ChannelInterface; |
7
|
|
|
use Bhaktaraz\RSSGenerator\SimpleXMLElement; |
8
|
|
|
|
9
|
|
|
class Item implements ItemInterface |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
protected $title; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
protected $url; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $description; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
protected $content; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $creator; |
26
|
|
|
|
27
|
|
|
/** @var array */ |
28
|
|
|
protected $categories = []; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
protected $guid; |
32
|
|
|
|
33
|
|
|
/** @var bool */ |
34
|
|
|
protected $isPermalink; |
35
|
|
|
|
36
|
|
|
/** @var int */ |
37
|
|
|
protected $pubDate; |
38
|
|
|
|
39
|
|
|
/** @var array */ |
40
|
|
|
protected $enclosure; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set item title |
44
|
|
|
* @param string $title |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function title($title) |
48
|
|
|
{ |
49
|
|
|
$this->title = $title; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set item URL |
56
|
|
|
* @param string $url |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function url($url) |
60
|
|
|
{ |
61
|
|
|
$this->url = $url; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set item description |
68
|
|
|
* @param string $description |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function description($description) |
72
|
|
|
{ |
73
|
|
|
$this->description = $description; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set item category |
80
|
|
|
* @param string $name Category name |
81
|
|
|
* @param string $domain Category URL |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function category($name, $domain = null) |
85
|
|
|
{ |
86
|
|
|
$this->categories[] = [$name, $domain]; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set GUID |
93
|
|
|
* @param string $guid |
94
|
|
|
* @param bool $isPermalink |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function guid($guid, $isPermalink = false) |
98
|
|
|
{ |
99
|
|
|
$this->guid = $guid; |
100
|
|
|
$this->isPermalink = $isPermalink; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set published date |
107
|
|
|
* @param int $pubDate Unix timestamp |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function pubDate($pubDate) |
111
|
|
|
{ |
112
|
|
|
$this->pubDate = $pubDate; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set enclosure |
119
|
|
|
* @param string $url Url to media file |
120
|
|
|
* @param int $length Length in bytes of the media file |
121
|
|
|
* @param string $type Media type, default is audio/mpeg |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function enclosure($url, $length = 0, $type = 'audio/mpeg') |
125
|
|
|
{ |
126
|
|
|
$this->enclosure = ['url' => $url, 'length' => $length, 'type' => $type]; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Append item to the channel |
133
|
|
|
* @param ChannelInterface $channel |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function appendTo(ChannelInterface $channel) |
137
|
|
|
{ |
138
|
|
|
$channel->addItem($this); |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set author name for article |
145
|
|
|
* |
146
|
|
|
* @param $creator |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function creator($creator) |
150
|
|
|
{ |
151
|
|
|
$this->creator = $creator; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $content |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
|
public function content($content) |
161
|
|
|
{ |
162
|
|
|
$this->content = $content; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Return XML object |
169
|
|
|
* @return SimpleXMLElement |
170
|
|
|
*/ |
171
|
|
|
public function asXML() |
172
|
|
|
{ |
173
|
|
|
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><item></item>', |
174
|
|
|
LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL); |
175
|
|
|
$xml->addChild('title', $this->title); |
176
|
|
|
$xml->addChild('link', $this->url); |
177
|
|
|
if ($this->pubDate !== null) { |
178
|
|
|
$xml->addChild('pubDate', date(DATE_RSS, $this->pubDate)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($this->creator) { |
182
|
|
|
$xml->addChildCData("xmlns:dc:creator", $this->creator); |
183
|
|
|
} |
184
|
|
|
if ($this->guid) { |
185
|
|
|
$guid = $xml->addChild('guid', $this->guid); |
186
|
|
|
|
187
|
|
|
if ($this->isPermalink) { |
188
|
|
|
$guid->addAttribute('isPermaLink', 'true'); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
foreach ($this->categories as $category) { |
193
|
|
|
$element = $xml->addChild('category', $category[0]); |
194
|
|
|
|
195
|
|
|
if (isset($category[1])) { |
196
|
|
|
$element->addAttribute('domain', $category[1]); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$xml->addChild('description', $this->description); |
201
|
|
|
$xml->addChildCData('xmlns:content:encoded', $this->content); |
202
|
|
|
|
203
|
|
|
if (is_array($this->enclosure) && (count($this->enclosure) == 3)) { |
204
|
|
|
$element = $xml->addChild('enclosure'); |
205
|
|
|
$element->addAttribute('url', $this->enclosure['url']); |
206
|
|
|
$element->addAttribute('type', $this->enclosure['type']); |
207
|
|
|
|
208
|
|
|
if ($this->enclosure['length']) { |
209
|
|
|
$element->addAttribute('length', $this->enclosure['length']); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $xml; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|