Skip to content

Commit

Permalink
Merge pull request #80 from alexdebril/issue/72
Browse files Browse the repository at this point in the history
Close #72 : author's properties are stored in nodes
  • Loading branch information
alexdebril authored May 16, 2017
2 parents f31f9da + e1abb7c commit 6d73134
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
13 changes: 7 additions & 6 deletions src/FeedIo/Rule/Atom/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public function setProperty(NodeInterface $node, \DOMElement $element)
{
if ($node instanceof ItemInterface) {
$author = $node->newAuthor();
$author->setName($this->getAttributeValue($element, 'name'));
$author->setUri($this->getAttributeValue($element, 'uri'));
$author->setEmail($this->getAttributeValue($element, 'email'));
$author->setName($this->getChildValue($element, 'name'));
$author->setUri($this->getChildValue($element, 'uri'));
$author->setEmail($this->getChildValue($element, 'email'));
$node->setAuthor($author);
}

Expand All @@ -48,13 +48,14 @@ public function createElement(\DomDocument $document, NodeInterface $node)
{
if ($node instanceof ItemInterface && !is_null($node->getAuthor())) {
$element = $document->createElement(static::NODE_NAME);
$element->setAttribute('name', $node->getAuthor()->getName());
$element->setAttribute('uri', $node->getAuthor()->getUri());
$element->setAttribute('email', $node->getAuthor()->getEmail());
$element->appendChild($document->createElement('name', $node->getAuthor()->getName()));
$element->appendChild($document->createElement('uri', $node->getAuthor()->getUri()));
$element->appendChild($document->createElement('email', $node->getAuthor()->getEmail()));

return $element;
}

return;
}

}
15 changes: 15 additions & 0 deletions src/FeedIo/RuleAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ public function getAttributeValue(\DOMElement $element, $name)
return;
}

/**
* @param \DOMElement $element
* @param string $name
* @return string|null
*/
public function getChildValue(\DOMElement $element, $name)
{
$list = $element->getElementsByTagName($name);
if ( $list->length > 0 ) {
return $list->item(0)->nodeValue;
}

return;
}

/**
* Sets the accurate $item property according to the DomElement content
*
Expand Down
28 changes: 21 additions & 7 deletions tests/FeedIo/Rule/Atom/AuthorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,27 @@ public function testSet()
$document = new \DOMDocument();

$author = $document->createElement('author');
$author->setAttribute('name', 'John Doe');
$author->setAttribute('uri', 'http://localhost');
$author->setAttribute('email', 'john@localhost');

$author->appendChild($document->createElement('name', 'John Doe'));
$author->appendChild($document->createElement('uri', 'http://localhost'));
$author->appendChild($document->createElement('email', 'john@localhost'));

$this->object->setProperty($item, $author);
$this->assertEquals('John Doe', $item->getAuthor()->getName());
$this->assertEquals('http://localhost', $item->getAuthor()->getUri());
$this->assertEquals('john@localhost', $item->getAuthor()->getEmail());
}

public function testGetChildValue()
{
$document = new \DOMDocument();

$author = $document->createElement('author');
$author->appendChild($document->createElement('name', 'John Doe'));

$this->assertEquals('John Doe', $this->object->getChildValue($author, 'name'));
}

public function testCreateElement()
{
$item = new Item();
Expand All @@ -51,11 +62,14 @@ public function testCreateElement()
$author->setEmail('john@localhost');
$item->setAuthor($author);

$element = $this->object->createElement(new \DOMDocument(), $item);
$document = new \DOMDocument();
$element = $this->object->createElement($document, $item);
$document->appendChild($element);
$this->assertInstanceOf('\DomElement', $element);
$this->assertEquals('author', $element->nodeName);
$this->assertEquals('John Doe', $element->getAttribute('name'));
$this->assertEquals('http://localhost', $element->getAttribute('uri'));
$this->assertEquals('john@localhost', $element->getAttribute('email'));

$this->assertXmlStringEqualsXmlString(
'<?xml version="1.0"?><author><name>John Doe</name><uri>http://localhost</uri><email>john@localhost</email></author>',
$document->saveXML());
}
}

0 comments on commit 6d73134

Please sign in to comment.