1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JDecool\JsonFeed; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* The JSON Feed format is a pragmatic syndication format, like [RSS](http://cyber.harvard.edu/rss/rss.html) and |
7
|
|
|
* [Atom](https://tools.ietf.org/html/rfc4287), but with one big difference: it’s JSON instead of XML. |
8
|
|
|
*/ |
9
|
|
|
class Feed |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
private $title; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
private $homepageUrl; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
private $feedUrl; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $description; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $userComment; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $nextUrl; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
private $icon; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
private $favicon; |
34
|
|
|
|
35
|
|
|
/** @var Author */ |
36
|
|
|
private $author; |
37
|
|
|
|
38
|
|
|
/** @var bool */ |
39
|
|
|
private $expired; |
40
|
|
|
|
41
|
|
|
/** @var Hub[] */ |
42
|
|
|
private $hubs; |
43
|
|
|
|
44
|
|
|
/** @var Item[] */ |
45
|
|
|
private $items; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Constructor |
49
|
|
|
* |
50
|
|
|
* @param string $title |
51
|
|
|
*/ |
52
|
|
|
public function __construct($title) |
53
|
|
|
{ |
54
|
|
|
$this->title = $title; |
55
|
|
|
|
56
|
|
|
$this->hubs = []; |
57
|
|
|
$this->items = []; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the name of the feed |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getTitle() |
66
|
|
|
{ |
67
|
|
|
return $this->title; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the name of the feed |
72
|
|
|
* |
73
|
|
|
* @param string $title |
74
|
|
|
* @return Feed |
75
|
|
|
*/ |
76
|
|
|
public function setTitle($title) |
77
|
|
|
{ |
78
|
|
|
$this->title = $title; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the URL of the resource that the feed describes |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getHomepageUrl() |
89
|
|
|
{ |
90
|
|
|
return $this->homepageUrl; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the URL of the resource that the feed describes |
95
|
|
|
* |
96
|
|
|
* @param string $homepageUrl |
97
|
|
|
* @return Feed |
98
|
|
|
*/ |
99
|
|
|
public function setHomepageUrl($homepageUrl) |
100
|
|
|
{ |
101
|
|
|
$this->homepageUrl = $homepageUrl; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the URL of the feed, and serves as the unique identifier for the feed |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getFeedUrl() |
112
|
|
|
{ |
113
|
|
|
return $this->feedUrl; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set the URL of the feed, and serves as the unique identifier for the feed |
118
|
|
|
* |
119
|
|
|
* @param string $feedUrl |
120
|
|
|
* @return Feed |
121
|
|
|
*/ |
122
|
|
|
public function setFeedUrl($feedUrl) |
123
|
|
|
{ |
124
|
|
|
$this->feedUrl = $feedUrl; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get more detail, beyond the `title`, on what the feed is about |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getDescription() |
135
|
|
|
{ |
136
|
|
|
return $this->description; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set more detail, beyond the `title`, on what the feed is about |
141
|
|
|
* |
142
|
|
|
* @param string $description |
143
|
|
|
* @return Feed |
144
|
|
|
*/ |
145
|
|
|
public function setDescription($description) |
146
|
|
|
{ |
147
|
|
|
$this->description = $description; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get a description of the purpose of the feed |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function getUserComment() |
158
|
|
|
{ |
159
|
|
|
return $this->userComment; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set a description of the purpose of the feed |
164
|
|
|
* |
165
|
|
|
* @param string $userComment |
166
|
|
|
* @return Feed |
167
|
|
|
*/ |
168
|
|
|
public function setUserComment($userComment) |
169
|
|
|
{ |
170
|
|
|
$this->userComment = $userComment; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get the URL of a feed that provides the next n items, where n is determined by the publisher |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function getNextUrl() |
181
|
|
|
{ |
182
|
|
|
return $this->nextUrl; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Set the URL of a feed that provides the next n items, where n is determined by the publisher |
187
|
|
|
* |
188
|
|
|
* @param string $nextUrl |
189
|
|
|
* @return Feed |
190
|
|
|
*/ |
191
|
|
|
public function setNextUrl($nextUrl) |
192
|
|
|
{ |
193
|
|
|
$this->nextUrl = $nextUrl; |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get the URL of an image for the feed suitable to be used in a timeline, much the way an avatar might be used |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
|
|
public function getIcon() |
204
|
|
|
{ |
205
|
|
|
return $this->icon; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Set the URL of an image for the feed suitable to be used in a timeline, much the way an avatar might be used |
210
|
|
|
* |
211
|
|
|
* @param string $icon |
212
|
|
|
* @return Feed |
213
|
|
|
*/ |
214
|
|
|
public function setIcon($icon) |
215
|
|
|
{ |
216
|
|
|
$this->icon = $icon; |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get the URL of an image for the feed suitable to be used in a source list |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public function getFavicon() |
227
|
|
|
{ |
228
|
|
|
return $this->favicon; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Set the URL of an image for the feed suitable to be used in a source list |
233
|
|
|
* |
234
|
|
|
* @param string $favicon |
235
|
|
|
* @return Feed |
236
|
|
|
*/ |
237
|
|
|
public function setFavicon($favicon) |
238
|
|
|
{ |
239
|
|
|
$this->favicon = $favicon; |
240
|
|
|
|
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get the feed author |
246
|
|
|
* |
247
|
|
|
* @return Author |
248
|
|
|
*/ |
249
|
|
|
public function getAuthor() |
250
|
|
|
{ |
251
|
|
|
return $this->author; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Set the feed author |
256
|
|
|
* |
257
|
|
|
* @param Author $author |
258
|
|
|
* @return Feed |
259
|
|
|
*/ |
260
|
|
|
public function setAuthor(Author $author) |
261
|
|
|
{ |
262
|
|
|
$this->author = $author; |
263
|
|
|
|
264
|
|
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Set the feed author |
269
|
|
|
* |
270
|
|
|
* @param string $name |
271
|
|
|
* @param string $url |
272
|
|
|
* @return Feed |
273
|
|
|
*/ |
274
|
|
|
public function addAuthor($name, $url = null) |
275
|
|
|
{ |
276
|
|
|
$author = new Author($name); |
277
|
|
|
$author->setUrl($url); |
278
|
|
|
|
279
|
|
|
return $this->setAuthor($author); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Says whether or not the feed is finished |
284
|
|
|
* |
285
|
|
|
* @return bool |
286
|
|
|
*/ |
287
|
|
|
public function isExpired() |
288
|
|
|
{ |
289
|
|
|
return $this->expired; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Set the feed has expired |
294
|
|
|
* |
295
|
|
|
* @param bool $expired |
296
|
|
|
* @return Feed |
297
|
|
|
*/ |
298
|
|
|
public function setExpired($expired) |
299
|
|
|
{ |
300
|
|
|
$this->expired = $expired; |
301
|
|
|
|
302
|
|
|
return $this; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Get endpoints that can be used to subscribe to real-time notifications from the publisher of this feed |
307
|
|
|
* |
308
|
|
|
* @return Hub[] |
309
|
|
|
*/ |
310
|
|
|
public function getHubs() |
311
|
|
|
{ |
312
|
|
|
return $this->hubs; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Add endpoint that can be used to subscribe to real-time notifications from the publisher of this feed |
317
|
|
|
* |
318
|
|
|
* @param Hub $hub |
319
|
|
|
* @return Feed |
320
|
|
|
*/ |
321
|
|
|
public function addHub(Hub $hub) |
322
|
|
|
{ |
323
|
|
|
if (!in_array($hub, $this->hubs, true)) { |
324
|
|
|
$this->hubs[] = $hub; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
return $this; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Set endpoints that can be used to subscribe to real-time notifications from the publisher of this feed |
332
|
|
|
* |
333
|
|
|
* @param Hub[] $hubs |
334
|
|
|
* @return Feed |
335
|
|
|
*/ |
336
|
|
|
public function setHubs(array $hubs) |
337
|
|
|
{ |
338
|
|
|
$this->hubs = $hubs; |
339
|
|
|
|
340
|
|
|
return $this; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Get feed items |
345
|
|
|
* |
346
|
|
|
* @return Item[] |
347
|
|
|
*/ |
348
|
|
|
public function getItems() |
349
|
|
|
{ |
350
|
|
|
return $this->items; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Add feed item |
355
|
|
|
* |
356
|
|
|
* @param Item $item |
357
|
|
|
* @return Feed |
358
|
|
|
*/ |
359
|
|
|
public function addItem(Item $item) |
360
|
|
|
{ |
361
|
|
|
if (!in_array($item, $this->items, true)) { |
362
|
|
|
$this->items[] = $item; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
return $this; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Set feed items |
370
|
|
|
* |
371
|
|
|
* @param Item[] $items |
372
|
|
|
* @return Feed |
373
|
|
|
*/ |
374
|
|
|
public function setItems(array $items) |
375
|
|
|
{ |
376
|
|
|
$this->items = $items; |
377
|
|
|
|
378
|
|
|
return $this; |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
|