Skip to content

Commit

Permalink
Merge pull request #132 from alexdebril/issue/131
Browse files Browse the repository at this point in the history
Issue/131
  • Loading branch information
alexdebril authored Aug 2, 2017
2 parents 80ad665 + a63de6e commit e7a3ff3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
3 changes: 1 addition & 2 deletions examples/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@
$logger = new \Psr\Log\NullLogger();

$feedIo = new \FeedIo\FeedIo($client, $logger);
$domDocument = $feedIo->toAtom($result->getFeed());
echo $domDocument->saveXML();
echo $feedIo->toAtom($feed);
14 changes: 11 additions & 3 deletions src/FeedIo/Rule/Atom/LinkNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

namespace FeedIo\Rule\Atom;

use FeedIo\Feed\ItemInterface;
use FeedIo\Feed\NodeInterface;
use FeedIo\RuleAbstract;
use FeedIo\RuleSet;
use FeedIo\Rule\Media;

class LinkNode extends RuleAbstract
{
Expand All @@ -33,7 +33,7 @@ public function __construct($nodeName = null)
$mediaRule = new Media();
$mediaRule->setUrlAttributeName('href');
$this->ruleSet = new RuleSet(new Link('related'));
$this->ruleSet->add($mediaRule);
$this->ruleSet->add($mediaRule, ['media', 'enclosure']);
}

/**
Expand Down Expand Up @@ -61,6 +61,14 @@ public function setProperty(NodeInterface $node, \DOMElement $element)
*/
public function createElement(\DomDocument $document, NodeInterface $node)
{
return $this->ruleSet->getDefault()->createElement($document, $node);
$output = new \ArrayIterator();
if ($node instanceof ItemInterface) {
foreach ($node->getMedias() as $media) {
$output->append($this->ruleSet->get('media')->createElement($document, $node));
}
}
$output->append($this->ruleSet->getDefault()->createElement($document, $node));

return $output;
}
}
27 changes: 27 additions & 0 deletions src/FeedIo/Rule/Atom/Media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\Item\MediaInterface;
use FeedIo\Rule\Media as BaseMedia;

class Media extends BaseMedia
{
const NODE_NAME = 'link';

public function createMediaElement(\DomDocument $document, MediaInterface $media)
{
$element = parent::createMediaElement($document, $media);
$element->setAttribute('rel', 'enclosure');

return $element;
}
}
15 changes: 11 additions & 4 deletions tests/FeedIo/Rule/Atom/LinkNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,16 @@ public function testCreateElement()
$item = new Item();
$item->setLink(self::LINK);

$element = $this->object->createElement(new \DOMDocument(), $item);
$this->assertInstanceOf('\DomElement', $element);
$this->assertEquals(self::LINK, $element->getAttribute('href'));
$this->assertEquals('link', $element->nodeName);
$elements = $this->object->createElement(new \DOMDocument(), $item);

$count = 0;
foreach ($elements as $element) {
$count++;
$this->assertInstanceOf('\DomElement', $element);
$this->assertEquals(self::LINK, $element->getAttribute('href'));
$this->assertEquals('link', $element->nodeName);
}

$this->assertEquals(1, $count);
}
}

0 comments on commit e7a3ff3

Please sign in to comment.