Skip to content

Commit

Permalink
JSONFeed Attachments support. Close #336
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdebril committed Mar 10, 2021
1 parent e6d6e73 commit 402f804
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
6 changes: 5 additions & 1 deletion examples/jsonfeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ function getItem($title, $description)
$item = new \FeedIo\Feed\Item();
$item->setTitle($title);
$item->setDescription($description);
$media = new \FeedIo\Feed\Item\Media();
$media->setUrl('http://localhost/some-resource.jpg');
$media->setType('image/jpeg');
$item->addMedia($media);

return $item;
}
}
13 changes: 11 additions & 2 deletions src/FeedIo/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,18 @@ public function handleAuthor(Feed\NodeInterface $node, array &$array) : array
public function handleMedia(Feed\ItemInterface $item, array &$array) : array
{
if ($item->hasMedia()) {
$array['image'] = $item->getMedias()->current()->getUrl();
$attachments = [];
/** @var Feed\Item\MediaInterface $media */
foreach ($item->getMedias() as $media) {
$attachments[] = array_filter([
'url' => $media->getUrl(),
'mime_type' => $media->getType(),
'title' => $media->getTitle(),
'size_in_bytes' => $media->getLength(),
]);
}
$array['attachments'] = $attachments;
}

return $array;
}

Expand Down
17 changes: 17 additions & 0 deletions src/FeedIo/Parser/JsonParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use FeedIo\Feed\Item;
use FeedIo\Feed\Item\Author;
use FeedIo\Feed\ItemInterface;
use FeedIo\Feed\NodeInterface;
use FeedIo\FeedInterface;
use FeedIo\ParserAbstract;
Expand Down Expand Up @@ -77,6 +78,7 @@ public function parseItems(iterable $items, FeedInterface $feed) : JsonParser
$item->setDescription($this->readOffset($dataItem, 'content_text', $contentHtml));
$item->setLink($this->readOffset($dataItem, 'url'));
$this->readAuthor($item, $dataItem);
$this->readMedias($item, $dataItem);
$feed->add($item);
}

Expand All @@ -98,6 +100,21 @@ public function readOffset(array $data, string $offsetName, string $default = nu
return $default;
}

protected function readMedias(ItemInterface $item, array $data): void
{
if (array_key_exists('attachments', $data)) {
foreach ($data['attachments'] as $attachment) {
$media = new Item\Media();
$media
->setType($attachment['mime_type'])
->setUrl($attachment['url'])
->setLength($attachment['size_in_bytes'] ?? null)
->setTitle($attachment['title'] ?? null);
$item->addMedia($media);
}
}
}

protected function readAuthor(NodeInterface $node, array $data): void
{
if (array_key_exists('author', $data)) {
Expand Down
4 changes: 4 additions & 0 deletions tests/FeedIo/Parser/JsonParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public function testParseContent()
$this->assertNull($items[0]['author']);
$this->assertNull($items[1]['author']);

$this->assertCount(1, $items[0]['medias']);
$this->assertEquals('http://localhost/some-resource.jpg', $items[0]['medias'][0]['url']);
$this->assertEquals('image/jpg', $items[0]['medias'][0]['type']);

$this->assertEquals('Manton Reece', $items[2]['author']['name']);
$this->assertNull($items[2]['author']['uri']);
$this->assertNull($items[2]['author']['email']);
Expand Down
10 changes: 8 additions & 2 deletions tests/samples/feed.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
"url": "https://jsonfeed.org/2017/05/17/announcing_json_feed",
"title": "Announcing JSON Feed",
"content_html": "<p>We — Manton Reece and Brent Simmons — have noticed that JSON has become the developers’ choice for APIs, and that developers will often go out of their way to avoid XML. JSON is simpler to read and write, and it’s less prone to bugs.</p>\n\n<p>So we developed JSON Feed, a format similar to <a href=\"http://cyber.harvard.edu/rss/rss.html\">RSS</a> and <a href=\"https://tools.ietf.org/html/rfc4287\">Atom</a> but in JSON. It reflects the lessons learned from our years of work reading and publishing feeds.</p>\n\n<p><a href=\"https://jsonfeed.org/version/1\">See the spec</a>. It’s at version 1, which may be the only version ever needed. If future versions are needed, version 1 feeds will still be valid feeds.</p>\n\n<h4>Notes</h4>\n\n<p>We have a <a href=\"https://github.com/manton/jsonfeed-wp\">WordPress plugin</a> and, coming soon, a JSON Feed Parser for Swift. As more code is written, by us and others, we’ll update the <a href=\"https://jsonfeed.org/code\">code</a> page.</p>\n\n<p>See <a href=\"https://jsonfeed.org/mappingrssandatom\">Mapping RSS and Atom to JSON Feed</a> for more on the similarities between the formats.</p>\n\n<p>This website — the Markdown files and supporting resources — <a href=\"https://github.com/brentsimmons/JSONFeed\">is up on GitHub</a>, and you’re welcome to comment there.</p>\n\n<p>This website is also a blog, and you can subscribe to the <a href=\"https://jsonfeed.org/xml/rss.xml\">RSS feed</a> or the <a href=\"https://jsonfeed.org/feed.json\">JSON feed</a> (if your reader supports it).</p>\n\n<p>We worked with a number of people on this over the course of several months. We list them, and thank them, at the bottom of the <a href=\"https://jsonfeed.org/version/1\">spec</a>. But — most importantly — <a href=\"http://furbo.org/\">Craig Hockenberry</a> spent a little time making it look pretty. :)</p>",
"date_published": "2017-05-17T08:02:12-07:00"
"date_published": "2017-05-17T08:02:12-07:00",
"attachments": [
{
"url": "http://localhost/some-resource.jpg",
"mime_type": "image/jpg"
}
]
},
{
"id": "https://jsonfeed.org/2017/05/17/announcing_json_feed",
Expand Down Expand Up @@ -45,4 +51,4 @@
}
}
]
}
}

0 comments on commit 402f804

Please sign in to comment.