Skip to content

Commit

Permalink
Merge pull request #230 from sylvainlg/feature/add-image-support-for-…
Browse files Browse the repository at this point in the history
…feed-interface

Add support for image property on FeedInterface Rss / Atom (logo)
  • Loading branch information
alexdebril authored Nov 5, 2019
2 parents 33bbaa0 + a873043 commit 8a8db91
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 42 deletions.
80 changes: 40 additions & 40 deletions doc/specifications-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,48 @@ This document explains which attributes are supported by feed-io and how to acce

interface : FeedInterface

| atom | rss | getter | setter |
| ---- | --- | ------ | ------ |
| title | title | getTitle | setTitle |
| link | link | getLink | setLink |
| updated | pubDate / lastBuildDate | getLastModified | setLastModified |
| id | N/A | getPublicId | setPublicId |
| N/A | description | getDescription | setDescription |
| category | category | getCategories | addCategory |
| author | author | getAuthor | setAuthor |
| contributor | N/A | not supported | not supported |
| icon / logo | image | not supported | not supported |
| rights | copyright | not supported | not supported |
| subtitle | N/A | not supported | not supported |
| lang | language | getLanguage | setLanguage |
| base | N/A | not supported | not supported |
| generator | generator | not supported | not supported |
| N/A | managingEditor | not supported | not supported |
| N/A | webMaster | not supported | not supported |
| N/A | docs | not supported | not supported |
| N/A | cloud | not supported | not supported |
| N/A | ttl | not supported | not supported |
| N/A | rating | not supported | not supported |
| N/A | textInput | not supported | not supported |
| N/A | skipdays | not supported | not supported |
| N/A | skipHours | not supported | not supported |
| atom | rss | getter | setter |
| ----------- | ----------------------- | --------------- | --------------- |
| title | title | getTitle | setTitle |
| link | link | getLink | setLink |
| updated | pubDate / lastBuildDate | getLastModified | setLastModified |
| id | N/A | getPublicId | setPublicId |
| N/A | description | getDescription | setDescription |
| category | category | getCategories | addCategory |
| author | author | getAuthor | setAuthor |
| contributor | N/A | not supported | not supported |
| logo | image | getLogo | setLogo |
| rights | copyright | not supported | not supported |
| subtitle | N/A | not supported | not supported |
| lang | language | getLanguage | setLanguage |
| base | N/A | not supported | not supported |
| generator | generator | not supported | not supported |
| N/A | managingEditor | not supported | not supported |
| N/A | webMaster | not supported | not supported |
| N/A | docs | not supported | not supported |
| N/A | cloud | not supported | not supported |
| N/A | ttl | not supported | not supported |
| N/A | rating | not supported | not supported |
| N/A | textInput | not supported | not supported |
| N/A | skipdays | not supported | not supported |
| N/A | skipHours | not supported | not supported |

## entry (atom) / item (rss)

Interface : ItemInterface

| atom | rss | getter | setter |
| ---- | --- | ------ | ------ |
| title | title | getTitle | setTitle |
| link | link | getLink | setLink |
| link | enclosure | getMedias | addMedia |
| updated / published | pubDate | getLastModified | setLastModified |
| id | guid | getPublicId | setPublicId |
| content | description | getDescription | setDescription |
| summary | N/A | not supported | not supported |
| source | source | not supported | not supported |
| category | category | getCategories | addCategory |
| author | N/A | getAuthor | setAuthor |
| contributor | N/A | not supported | not supported |
| N/A | comments | not supported | not supported |
| rights | N/A | not supported | not supported |
| atom | rss | getter | setter |
| ------------------- | ----------- | --------------- | --------------- |
| title | title | getTitle | setTitle |
| link | link | getLink | setLink |
| link | enclosure | getMedias | addMedia |
| updated / published | pubDate | getLastModified | setLastModified |
| id | guid | getPublicId | setPublicId |
| content | description | getDescription | setDescription |
| summary | N/A | not supported | not supported |
| source | source | not supported | not supported |
| category | category | getCategories | addCategory |
| author | N/A | getAuthor | setAuthor |
| contributor | N/A | not supported | not supported |
| N/A | comments | not supported | not supported |
| rights | N/A | not supported | not supported |
24 changes: 24 additions & 0 deletions src/FeedIo/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class Feed extends Node implements FeedInterface, \JsonSerializable
*/
protected $language;

/**
* @var string $logo
*/
protected $logo;

protected $ns;

public function __construct()
Expand Down Expand Up @@ -79,6 +84,25 @@ public function setLanguage(string $language = null): FeedInterface
return $this;
}

/**
* @return string
*/
public function getLogo() : ? string
{
return $this->logo;
}

/**
* @param string $logo
* @return NodeInterface
*/
public function setLogo(string $logo = null) : FeedInterface
{
$this->logo = $logo;

return $this;
}




Expand Down
11 changes: 11 additions & 0 deletions src/FeedIo/FeedInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public function getLanguage(): ? string ;
*/
public function setLanguage(string $language = null): FeedInterface;

/**
* @return string
*/
public function getLogo() : ? string ;

/**
* @param string $logo
* @return NodeInterface
*/
public function setLogo(string $logo = null) : FeedInterface;

/**
* Atom : feed.entry <feed><entry>
* Rss : rss.channel.item <rss><channel><item>
Expand Down
55 changes: 55 additions & 0 deletions src/FeedIo/Rule/Atom/Logo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types=1);
/*
* This file is part of the feed-io package.
*
* (c) Alexandre Debril <alex.debril@gmail.com>
* (c) Sylvain Le Gleau <syl@sylvainlg.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FeedIo\Rule\Atom;

use FeedIo\Feed\Item;
use FeedIo\Feed\Item\MediaInterface;
use FeedIo\Feed\ItemInterface;
use FeedIo\Feed\NodeInterface;
use FeedIo\RuleAbstract;
use FeedIo\FeedInterface;

class Logo extends RuleAbstract
{
// https://tools.ietf.org/html/rfc4287#section-4.2.8
const NODE_NAME = 'logo';

/**
* @param NodeInterface $node
* @param \DOMElement $element
*/
public function setProperty(NodeInterface $node, \DOMElement $element) : void
{
if ($node instanceof FeedInterface) {
$node->setLogo($element->nodeValue);
}
}

/**
* @inheritDoc
*/
protected function hasValue(NodeInterface $node) : bool
{
return $node instanceof FeedInterface && !! $node->getLogo();
}

/**
* @inheritDoc
*/
protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
{
if (!($node instanceof FeedInterface) || is_null($node->getLogo())) {
return;
}
$rootElement->appendChild($document->createElement($this->getNodeName(), $node->getLogo()));
}
}
78 changes: 78 additions & 0 deletions src/FeedIo/Rule/Logo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php declare(strict_types=1);
/*
* This file is part of the feed-io package.
*
* (c) Alexandre Debril <alex.debril@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FeedIo\Rule;

use FeedIo\Feed\Item;
use FeedIo\FeedInterface;
use FeedIo\Feed\NodeInterface;
use FeedIo\RuleAbstract;

class Logo extends RuleAbstract
{
const NODE_NAME = 'image';

protected $urlAttributeName = 'url';

/**
* @return string
*/
public function getUrlAttributeName() : string
{
return $this->urlAttributeName;
}

/**
* @param string $name
*/
public function setUrlAttributeName(string $name) : void
{
$this->urlAttributeName = $name;
}

/**
* @param NodeInterface $node
* @param \DOMElement $element
*/
public function setProperty(NodeInterface $node, \DOMElement $element) : void
{
if ($node instanceof FeedInterface) {
for ($i = $element->childNodes->length; --$i >= 0;) {
$child = $element->childNodes->item($i);
if ($child instanceof \DOMElement && $child->tagName === $this->getUrlAttributeName()) {
$node->setLogo($child->textContent);
}
}
}
}

/**
* @inheritDoc
*/
protected function hasValue(NodeInterface $node) : bool
{
return $node instanceof FeedInterface && !! $node->getLogo();
}

/**
* @inheritDoc
*/
protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
{
if ($node instanceof FeedInterface) {
$element = $document->createElement(static::NODE_NAME);
$this->appendNonEmptyChild($document, $element, 'url', $node->getLogo());
$this->appendNonEmptyChild($document, $element, 'title', $node->getTitle());
$this->appendNonEmptyChild($document, $element, 'link', $node->getLink());

$rootElement->appendChild($element);
}
}
}
4 changes: 3 additions & 1 deletion src/FeedIo/Standard/Atom.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use FeedIo\Reader\Document;
use FeedIo\Rule\Atom\Author;
use FeedIo\Rule\Atom\LinkNode;
use FeedIo\Rule\Atom\Logo;
use FeedIo\Rule\Description;
use FeedIo\Rule\Language;
use FeedIo\Rule\PublicId;
Expand Down Expand Up @@ -104,7 +105,8 @@ public function buildItemRuleSet() : RuleSet
protected function buildBaseRuleSet() : RuleSet
{
$ruleSet = parent::buildBaseRuleSet();
$ruleSet->add(new Category());
$ruleSet->add(new Category())
->add(new Logo());

return $ruleSet;
}
Expand Down
4 changes: 3 additions & 1 deletion src/FeedIo/Standard/Rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use FeedIo\Rule\PublicId;
use FeedIo\Rule\Media;
use FeedIo\Rule\Category;
use FeedIo\Rule\Logo;
use FeedIo\RuleSet;

class Rss extends XmlAbstract
Expand Down Expand Up @@ -121,7 +122,8 @@ protected function buildBaseRuleSet() : RuleSet
->add(new Link())
->add(new Description())
->add($this->getModifiedSinceRule(static::DATE_NODE_TAGNAME), ['lastBuildDate', 'lastPubDate'])
->add(new Category());
->add(new Category())
->add(new Logo());

return $ruleSet;
}
Expand Down
64 changes: 64 additions & 0 deletions tests/FeedIo/Rule/Atom/LogoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/*
* This file is part of the feed-io package.
*
* (c) Alexandre Debril <alex.debril@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FeedIo\Rule\Atom;

use FeedIo\Feed;

use \PHPUnit\Framework\TestCase;

class LogoTest extends TestCase
{

/**
* @var Logo
*/
protected $object;

const LOGO = 'http://localhost/logo.jpeg';

protected function setUp()
{
$this->object = new Logo();
}

public function testSet()
{
$feed = new Feed();
$document = new \DOMDocument();

$logo = $document->createElement('logo', self::LOGO);
$this->object->setProperty($feed, $logo);
$this->assertEquals('http://localhost/logo.jpeg', $feed->getLogo());
}

public function testCreateElement()
{
$feed = new Feed();
$feed->setLogo(self::LOGO);

$document = new \DOMDocument();
$rootElement = $document->createElement('feed');
$this->object->apply($document, $rootElement, $feed);

$addedElement = $rootElement->firstChild;

$this->assertInstanceOf('\DomElement', $addedElement);
$this->assertEquals(self::LOGO, $addedElement->nodeValue);
$this->assertEquals('logo', $addedElement->nodeName);

$document->appendChild($rootElement);

$this->assertXmlStringEqualsXmlString(
'<feed><logo>http://localhost/logo.jpeg</logo></feed>',
$document->saveXML()
);
}
}
Loading

0 comments on commit 8a8db91

Please sign in to comment.