-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from sylvainlg/feature/add-image-support-for-…
…feed-interface Add support for image property on FeedInterface Rss / Atom (logo)
- Loading branch information
Showing
9 changed files
with
353 additions
and
42 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
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
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,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())); | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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
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,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() | ||
); | ||
} | ||
} |
Oops, something went wrong.