Skip to content

Commit

Permalink
Author support in JSONFeed format
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdebril committed Mar 9, 2021
1 parent 98beaf5 commit 80ad861
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
18 changes: 8 additions & 10 deletions src/FeedIo/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function toString(FeedInterface $feed) : string
*/
public function toArray(FeedInterface $feed) : array
{
return array_filter([
$out = array_filter([
'version' => 'https://jsonfeed.org/version/1',
'title' => $feed->getTitle(),
'description' => $feed->getDescription(),
Expand All @@ -42,6 +42,9 @@ public function toArray(FeedInterface $feed) : array
'icon' => $feed->getLogo(),
'items' => iterator_to_array($this->itemsToArray($feed)),
]);
$this->handleAuthor($feed, $out);

return $out;
}

/**
Expand Down Expand Up @@ -93,17 +96,12 @@ public function isHtml(string $string) : bool
return $string !== strip_tags($string);
}

/**
* @param Feed\ItemInterface $item
* @param array $array
* @return array
*/
public function handleAuthor(Feed\ItemInterface $item, array &$array) : array
public function handleAuthor(Feed\NodeInterface $node, array &$array) : array
{
if (! is_null($item->getAuthor())) {
if (! is_null($node->getAuthor())) {
$array['author'] = array_filter([
'name' => $item->getAuthor()->getName(),
'url' => $item->getAuthor()->getUri(),
'name' => $node->getAuthor()->getName(),
'url' => $node->getAuthor()->getUri(),
]);
}

Expand Down
24 changes: 15 additions & 9 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\NodeInterface;
use FeedIo\FeedInterface;
use FeedIo\ParserAbstract;
use FeedIo\Reader\Document;
Expand All @@ -32,6 +33,7 @@ public function parseContent(Document $document, FeedInterface $feed) : FeedInte
$feed->setLink($this->readOffset($data, 'feed_url'));
$feed->setUrl($this->readOffset($data, 'home_page_url'));
$feed->setLogo($this->readOffset($data, 'icon'));
$this->readAuthor($feed, $data);

if (array_key_exists('items', $data)) {
$this->parseItems($data['items'], $feed);
Expand Down Expand Up @@ -74,15 +76,7 @@ public function parseItems(iterable $items, FeedInterface $feed) : JsonParser
$contentHtml = $this->readOffset($dataItem, 'content_html');
$item->setDescription($this->readOffset($dataItem, 'content_text', $contentHtml));
$item->setLink($this->readOffset($dataItem, 'url'));

if (array_key_exists('author', $dataItem)) {
$authorItem = $dataItem['author'];
$author = new Author();
$author->setName($this->readOffset($authorItem, 'name'));
$author->setUri($this->readOffset($authorItem, 'url'));
$author->setEmail($this->readOffset($authorItem, 'email'));
$item->setAuthor($author);
}
$this->readAuthor($item, $dataItem);
$feed->add($item);
}

Expand All @@ -103,4 +97,16 @@ public function readOffset(array $data, string $offsetName, string $default = nu

return $default;
}

protected function readAuthor(NodeInterface $node, array $data): void
{
if (array_key_exists('author', $data)) {
$authorItem = $data['author'];
$author = new Author();
$author->setName($this->readOffset($authorItem, 'name'));
$author->setUri($this->readOffset($authorItem, 'url'));
$author->setEmail($this->readOffset($authorItem, 'email'));
$node->setAuthor($author);
}
}
}
4 changes: 4 additions & 0 deletions tests/FeedIo/JsonFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function testToString()
$feed = new Feed();
$feed->setTitle('feed title');
$feed->setLogo(self::LOGO);
$author = new Item\Author();
$author->setName('alex');
$feed->setAuthor($author);

foreach ($items as $item) {
$feed->add($item);
Expand All @@ -41,6 +44,7 @@ public function testToString()
$this->assertJson($string);
$json = json_decode($string, true);

$this->assertEquals('alex', $json['author']['name']);
$this->assertEquals('feed title', $json['title']);
$this->assertEquals('http://localhost/logo.jpeg', $json['icon']);
$this->assertCount(2, $json['items']);
Expand Down
1 change: 1 addition & 0 deletions tests/FeedIo/Parser/JsonParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testParseContent()

$this->assertEquals('JSON Feed', $feed->getTitle());
$this->assertEquals('https://jsonfeed.org/graphics/icon.png', $feed->getLogo());
$this->assertEquals('Brent Simmons and Manton Reece', $feed->getAuthor()->getName());

$items = $feed->toArray()['items'];

Expand Down

0 comments on commit 80ad861

Please sign in to comment.