Skip to content

Commit

Permalink
Merge pull request #241 from itsgoingd/multiple-custom-fields
Browse files Browse the repository at this point in the history
Added support for multiple custom fields with the same name
  • Loading branch information
alexdebril authored Nov 27, 2019
2 parents 5ac713a + 66a1842 commit 52eb1b3
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/FeedIo/Formatter/XmlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getAllRules(RuleSet $ruleSet, NodeInterface $node) : iterable
$rules = $ruleSet->getRules();
$optionalFields = $node->listElements();
foreach ($optionalFields as $optionalField) {
$rules[] = new OptionalField($optionalField);
$rules[$optionalField] = new OptionalField($optionalField);
}

return $rules;
Expand Down
14 changes: 12 additions & 2 deletions src/FeedIo/Rule/OptionalField.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,23 @@ protected function hasValue(NodeInterface $node) : bool
*/
protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
{
$domElement = $document->createElement($this->getNodeName());
$addedElementsCount = 0;

if ($node instanceof ElementsAwareInterface) {
foreach ($node->getElementIterator($this->getNodeName()) as $element) {
$domElement = $document->createElement($this->getNodeName());

$this->buildDomElement($domElement, $element);

$rootElement->appendChild($domElement);

$addedElementsCount++;
}
}

$rootElement->appendChild($domElement);
if (! $addedElementsCount) {
// add an implicit empty element if the node had no elements matching this rule
$rootElement->appendChild($document->createElement($this->getNodeName()));
}
}
}
6 changes: 4 additions & 2 deletions tests/FeedIo/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ public function testGetAllRules()
$item = new Item();
$item->set('title', 'the title');
$item->set('description', 'the description');
$item->set('custom', 'a custom value');
$item->set('custom', 'another custom value');

$rules = $this->object->getAllRules(new RuleSet(), $item);
$this->assertCount(2, $rules);
$this->assertCount(3, $rules);

$ruleNames = array('title', 'description');
$ruleNames = array('title', 'description', 'custom');
foreach ($rules as $rule) {
$this->assertEquals(current($ruleNames), $rule->getNodeName());
next($ruleNames);
Expand Down
24 changes: 22 additions & 2 deletions tests/FeedIo/Rule/OptionalFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public function testSetProperty()

$this->assertTrue($item->hasElement('test'));
$this->assertEquals('a test value', $item->getValue('test'));

$itemElements = $item->getElementIterator('test');

$count = 0;
/** @var Element $itemElement */
foreach ($itemElements as $itemElement) {
Expand Down Expand Up @@ -172,6 +172,26 @@ public function testCreateElementWithAttributes()
$this->assertTrue($domElement->hasAttribute('foo'));
}

public function testCreateMultipleElements()
{
$item = new Item();
$item->set('default', 'first value');
$item->set('default', 'second value');

$document = new \DOMDocument();
$rootElement = $document->createElement('feed');

$this->object->apply($document, $rootElement, $item);

$this->assertEquals(2, $rootElement->childNodes->count());

$this->assertEquals('default', $rootElement->childNodes->item(0)->nodeName);
$this->assertEquals('first value', $rootElement->childNodes->item(0)->nodeValue);

$this->assertEquals('default', $rootElement->childNodes->item(1)->nodeName);
$this->assertEquals('second value', $rootElement->childNodes->item(1)->nodeValue);
}

public function testDontCreateElement()
{
$item = new Item();
Expand Down
3 changes: 3 additions & 0 deletions tests/FeedIo/StandardFormatter/FormatterTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public function testFormat()
$item->setLink('http://localhost/item/1');
$item->set('author', 'name@domain.tld');
$item->addCategory($category);
$item->set('custom', 'a sample value');
$item->set('custom', 'another sample value');

$feed->add($item);

$formatter = new XmlFormatter($this->standard);
Expand Down
4 changes: 3 additions & 1 deletion tests/samples/expected-atom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
<link href="http://localhost/item/1"/>
<id>http://localhost/item/1</id>
<updated>2014-12-01T00:00:00+00:00</updated>
<content>A great description</content>
<author>name@domain.tld</author>
<content>A great description</content>
<custom>a sample value</custom>
<custom>another sample value</custom>
</entry>
</feed>
4 changes: 3 additions & 1 deletion tests/samples/rss/expected-rss.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
<description>A great description</description>
<pubDate>Mon, 01 Dec 2014 00:00:00 +0000</pubDate>
<category domain="http://localhost">sample</category>
<guid>http://localhost/item/1</guid>
<author>name@domain.tld</author>
<guid>http://localhost/item/1</guid>
<custom>a sample value</custom>
<custom>another sample value</custom>
</item>
</channel>
</rss>

0 comments on commit 52eb1b3

Please sign in to comment.