-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Implementation for JSON Feed 1.1
- Loading branch information
1 parent
3abf36e
commit 599c600
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
|
||
class JSONCreator extends FeedCreator | ||
{ | ||
|
||
public function createFeed() | ||
{ | ||
$data = array(); | ||
|
||
$data['version'] = 'https://jsonfeed.org/version/1.1'; | ||
$data['title'] = (string)$this->title; | ||
$data['home_page_url'] = (string)$this->link; | ||
$data['feed_url'] = (string)$this->syndicationURL; | ||
$data['description'] = (string)$this->description; | ||
$data['user_comment'] = 'Created by ' . FEEDCREATOR_VERSION; | ||
if ($this->image != null) { | ||
$data['icon'] = $this->image->url; | ||
} | ||
if ($this->language != '') { | ||
$data['language'] = $this->language; | ||
} | ||
|
||
$data['items'] = array(); | ||
foreach ($this->items as $item) { | ||
$entry = array(); | ||
$entry['id'] = $item->guid ? (string)$item->guid : (string)$item->link; | ||
$entry['url'] = (string)$item->link; | ||
if ($item->source) { | ||
$entry['external_url'] = (string)$item->source; | ||
} | ||
$entry['title'] = strip_tags((string)$item->title); | ||
$entry['content_text'] = strip_tags((string)$item->description); | ||
$entry['content_html'] = (string)$item->description; | ||
$entry['date_published'] = (new FeedDate($item->date))->iso8601(); | ||
if ($item->author) { | ||
// We only support one author, JSONFeed 1.1 accepts multiple | ||
$entry['authors'] = array(array('name' => (string)$item->author)); | ||
// 1.0 only supported one, for compatibility we set it as well | ||
$entry['author'] = array('name' => (string)$item->author); | ||
} | ||
if ($item->category) { | ||
$entry['tags'] = (array)$item->category; | ||
} | ||
if($item->enclosure) { | ||
// We only support one enclosure, JSONFeed 1.1 accepts multiple | ||
$entry['attachments'] = array( | ||
array( | ||
'url' => $item->enclosure['url'], | ||
'mime_type' => $item->enclosure['type'], | ||
'size_in_bytes' => $item->enclosure['length'] | ||
) | ||
); | ||
} | ||
|
||
$data['items'][] = $entry; | ||
} | ||
|
||
return json_encode($data, JSON_PRETTY_PRINT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Creator; | ||
use FeedItem; | ||
use PHPUnit_Framework_TestCase; | ||
use UniversalFeedCreator; | ||
|
||
class JSONCreatorTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function test_create_empty_feed() | ||
{ | ||
$creator = new UniversalFeedCreator; | ||
$creator->description = 'Feed Description'; | ||
$item = new FeedItem(); | ||
$item->date = time(); | ||
$item->category = array('1', '2'); | ||
$creator->addItem($item); | ||
|
||
$feed = $creator->createFeed('JSON'); | ||
|
||
$parsed = json_decode($feed, true); | ||
$this->assertEquals('Feed Description', $parsed['description']); | ||
} | ||
} |