Skip to content

Commit

Permalink
Merge branch 'master' of github.com:alexdebril/feed-io into issue/333
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdebril committed Mar 9, 2021
2 parents 1651259 + 80ad861 commit 709e833
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 99 deletions.
34 changes: 0 additions & 34 deletions src/FeedIo/Feed/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

use FeedIo\Feed\Item\Media;
use FeedIo\Feed\Item\MediaInterface;
use FeedIo\Feed\Item\Author;
use FeedIo\Feed\Item\AuthorInterface;

class Item extends Node implements ItemInterface
{
Expand All @@ -23,11 +21,6 @@ class Item extends Node implements ItemInterface
*/
protected $medias;

/**
* @var AuthorInterface
*/
protected $author;

/**
* @var string
*/
Expand Down Expand Up @@ -118,31 +111,4 @@ public function setContent(string $content = null): ItemInterface

return $this;
}

/**
* @return AuthorInterface
*/
public function getAuthor() : ? AuthorInterface
{
return $this->author;
}

/**
* @param AuthorInterface $author
* @return ItemInterface
*/
public function setAuthor(AuthorInterface $author = null) : ItemInterface
{
$this->author = $author;

return $this;
}

/**
* @return AuthorInterface
*/
public function newAuthor() : AuthorInterface
{
return new Author();
}
}
25 changes: 0 additions & 25 deletions src/FeedIo/Feed/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

namespace FeedIo\Feed;

use FeedIo\Feed\Item\Author;
use FeedIo\Feed\Item\MediaInterface;
use FeedIo\Feed\Item\AuthorInterface;

/**
* Describes an Item instance
Expand Down Expand Up @@ -85,27 +83,4 @@ public function getContent(): ?string;
* @return ItemInterface
*/
public function setContent(string $content = null): ItemInterface;

/**
* returns the author attribute
*
* @return AuthorInterface
*/
public function getAuthor() : ? AuthorInterface;

/**
* sets $author to the object's attributes
*
* @param AuthorInterface $author
* @return ItemInterface
*/
public function setAuthor(AuthorInterface $author = null) : ItemInterface;


/**
* returns a new AuthorInterface
*
* @return AuthorInterface
*/
public function newAuthor() : AuthorInterface;
}
34 changes: 34 additions & 0 deletions src/FeedIo/Feed/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@

namespace FeedIo\Feed;

use FeedIo\Feed\Item\Author;
use FeedIo\Feed\Item\AuthorInterface;
use FeedIo\Feed\Node\Category;
use FeedIo\Feed\Node\CategoryInterface;

class Node implements NodeInterface, ElementsAwareInterface, ArrayableInterface
{
use ElementsAwareTrait;

/**
* @var AuthorInterface
*/
protected $author;

/**
* @var \ArrayIterator
*/
Expand Down Expand Up @@ -75,6 +82,33 @@ public function set(string $name, string $value = null) : NodeInterface
return $this;
}

/**
* @return AuthorInterface
*/
public function getAuthor() : ? AuthorInterface
{
return $this->author;
}

/**
* @param AuthorInterface $author
* @return ItemInterface
*/
public function setAuthor(AuthorInterface $author = null) : NodeInterface
{
$this->author = $author;

return $this;
}

/**
* @return AuthorInterface
*/
public function newAuthor() : AuthorInterface
{
return new Author();
}

/**
* returns node's categories
*
Expand Down
24 changes: 24 additions & 0 deletions src/FeedIo/Feed/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace FeedIo\Feed;

use FeedIo\Feed\Item\AuthorInterface;
use FeedIo\Feed\Node\CategoryInterface;

/**
Expand All @@ -19,6 +20,29 @@
*/
interface NodeInterface
{

/**
* returns the author attribute
*
* @return AuthorInterface
*/
public function getAuthor() : ? AuthorInterface;

/**
* sets $author to the object's attributes
*
* @param AuthorInterface $author
* @return NodeInterface
*/
public function setAuthor(AuthorInterface $author = null) : NodeInterface;

/**
* returns a new AuthorInterface
*
* @return AuthorInterface
*/
public function newAuthor() : AuthorInterface;

/**
* Returns node's title
*
Expand Down
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);
}
}
}
40 changes: 23 additions & 17 deletions src/FeedIo/Rule/Atom/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

namespace FeedIo\Rule\Atom;

use FeedIo\Feed\ItemInterface;
use FeedIo\Feed\NodeInterface;
use FeedIo\Rule\Author as BaseAuthor;
use FeedIo\RuleAbstract;

class Author extends BaseAuthor
class Author extends RuleAbstract
{
const NODE_NAME = 'author';

Expand All @@ -25,27 +24,34 @@ class Author extends BaseAuthor
*/
public function setProperty(NodeInterface $node, \DOMElement $element) : void
{
if ($node instanceof ItemInterface) {
$author = $node->newAuthor();
$author->setName($this->getChildValue($element, 'name'));
$author->setUri($this->getChildValue($element, 'uri'));
$author->setEmail($this->getChildValue($element, 'email'));
$node->setAuthor($author);
}
$author = $node->newAuthor();
$author->setName($this->getChildValue($element, 'name'));
$author->setUri($this->getChildValue($element, 'uri'));
$author->setEmail($this->getChildValue($element, 'email'));
$node->setAuthor($author);
}

/**
* Tells if the node contains the expected value
*
* @param NodeInterface $node
* @return bool
*/
protected function hasValue(NodeInterface $node) : bool
{
return !! $node->getAuthor();
}

/**
* @inheritDoc
*/
protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
{
if ($node instanceof ItemInterface) {
$element = $document->createElement(static::NODE_NAME);
$this->appendNonEmptyChild($document, $element, 'name', $node->getAuthor()->getName());
$this->appendNonEmptyChild($document, $element, 'uri', $node->getAuthor()->getUri());
$this->appendNonEmptyChild($document, $element, 'email', $node->getAuthor()->getEmail());
$element = $document->createElement(static::NODE_NAME);
$this->appendNonEmptyChild($document, $element, 'name', $node->getAuthor()->getName());
$this->appendNonEmptyChild($document, $element, 'uri', $node->getAuthor()->getUri());
$this->appendNonEmptyChild($document, $element, 'email', $node->getAuthor()->getEmail());

$rootElement->appendChild($element);
}
$rootElement->appendChild($element);
}
}
2 changes: 1 addition & 1 deletion src/FeedIo/Standard/Atom.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public function buildItemRuleSet() : RuleSet
{
$ruleSet = $this->buildBaseRuleSet();
$ruleSet
->add(new Author())
->add(new Content())
->add(new Summary())
->add(new Media(), ['media:group'])
Expand All @@ -112,6 +111,7 @@ protected function buildBaseRuleSet() : RuleSet
$ruleSet = parent::buildBaseRuleSet();
$ruleSet
->add(new Category())
->add(new Author())
->add(new LinkNode())
->add(new PublicId('id'))
->add(new Language('lang'))
Expand Down
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
Loading

0 comments on commit 709e833

Please sign in to comment.