Skip to content

Commit c55f908

Browse files
authored
Merge pull request #18 from splitbrain-forks/jsonfeed
Initial Implementation for JSON Feed 1.1
2 parents 3abf36e + e44440e commit c55f908

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

lib/Creator/JSONCreator.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* JSONCreator is a FeedCreator that implements the JSON Feed specification,
5+
* as in https://jsonfeed.org/version/1.1
6+
*
7+
* @author Andreas Gohr <andi@splitbrain.org>
8+
*/
9+
class JSONCreator extends FeedCreator
10+
{
11+
/** @inheritdoc */
12+
public function createFeed()
13+
{
14+
$data = array();
15+
16+
$data['version'] = 'https://jsonfeed.org/version/1.1';
17+
$data['title'] = (string)$this->title;
18+
$data['home_page_url'] = (string)$this->link;
19+
$data['feed_url'] = (string)$this->syndicationURL;
20+
$data['description'] = (string)$this->description;
21+
$data['user_comment'] = 'Created by ' . FEEDCREATOR_VERSION;
22+
if ($this->image != null) {
23+
$data['icon'] = $this->image->url;
24+
}
25+
if ($this->language != '') {
26+
$data['language'] = $this->language;
27+
}
28+
29+
$data['items'] = array();
30+
foreach ($this->items as $item) {
31+
$entry = array();
32+
$entry['id'] = $item->guid ? (string)$item->guid : (string)$item->link;
33+
$entry['url'] = (string)$item->link;
34+
if ($item->source) {
35+
$entry['external_url'] = (string)$item->source;
36+
}
37+
$entry['title'] = strip_tags((string)$item->title);
38+
$entry['content_text'] = strip_tags((string)$item->description);
39+
$entry['content_html'] = (string)$item->description;
40+
$entry['date_published'] = (new FeedDate($item->date))->iso8601();
41+
if ($item->author) {
42+
// We only support one author, JSONFeed 1.1 accepts multiple
43+
$entry['authors'] = array(array('name' => (string)$item->author));
44+
// 1.0 only supported one, for compatibility we set it as well
45+
$entry['author'] = array('name' => (string)$item->author);
46+
}
47+
if ($item->category) {
48+
$entry['tags'] = (array)$item->category;
49+
}
50+
if ($item->enclosure) {
51+
// We only support one enclosure, JSONFeed 1.1 accepts multiple
52+
$entry['attachments'] = array(
53+
array(
54+
'url' => $item->enclosure['url'],
55+
'mime_type' => $item->enclosure['type'],
56+
'size_in_bytes' => $item->enclosure['length']
57+
)
58+
);
59+
}
60+
61+
$data['items'][] = $entry;
62+
}
63+
64+
return json_encode($data);
65+
}
66+
}

lib/UniversalFeedCreator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ protected function _setFormat($format)
8888
$this->_feed = new JSCreator();
8989
break;
9090

91+
case "JSON":
92+
$this->_feed = new JSONCreator();
93+
break;
94+
9195
default:
9296
$this->_feed = new RSSCreator091();
9397
break;

test/Creator/JSONCreatorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Creator;
4+
use FeedItem;
5+
use PHPUnit_Framework_TestCase;
6+
use UniversalFeedCreator;
7+
8+
class JSONCreatorTest extends PHPUnit_Framework_TestCase
9+
{
10+
public function test_create_empty_feed()
11+
{
12+
$creator = new UniversalFeedCreator;
13+
$creator->description = 'Feed Description';
14+
$item = new FeedItem();
15+
$item->date = time();
16+
$item->category = array('1', '2');
17+
$creator->addItem($item);
18+
19+
$feed = $creator->createFeed('JSON');
20+
21+
$parsed = json_decode($feed, true);
22+
$this->assertEquals('Feed Description', $parsed['description']);
23+
}
24+
}

0 commit comments

Comments
 (0)