diff --git a/src/FeedIo/Feed/Item.php b/src/FeedIo/Feed/Item.php index 518595f2..6be45c7a 100644 --- a/src/FeedIo/Feed/Item.php +++ b/src/FeedIo/Feed/Item.php @@ -12,6 +12,8 @@ use FeedIo\Feed\Item\Media; use FeedIo\Feed\Item\MediaInterface; +use FeedIo\Feed\Item\Author; +use FeedIo\Feed\Item\AuthorInterface; class Item extends Node implements ItemInterface { @@ -21,6 +23,11 @@ class Item extends Node implements ItemInterface */ protected $medias; + /** + * @var AuthorInterface + */ + protected $author; + public function __construct() { $this->medias = new \ArrayIterator(); @@ -62,4 +69,31 @@ public function newMedia() { return new Media(); } + + /** + * @return AuthorInterface + */ + public function getAuthor() + { + return $this->author; + } + + /** + * @param AuthorInterface $author + * @return $this + */ + public function setAuthor(AuthorInterface $author) + { + $this->author = $author; + + return $this; + } + + /** + * @return AuthorInterface + */ + public function newAuthor() + { + return new Author(); + } } diff --git a/src/FeedIo/Feed/Item/Author.php b/src/FeedIo/Feed/Item/Author.php new file mode 100644 index 00000000..295224ac --- /dev/null +++ b/src/FeedIo/Feed/Item/Author.php @@ -0,0 +1,88 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FeedIo\Feed\Item; + +class Author implements AuthorInterface +{ + + /** + * @var string + */ + protected $name; + + /** + * @var string + */ + protected $uri; + + /** + * @var int + */ + protected $email; + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @param string $name + * @return $this + */ + public function setName($name) + { + $this->name = $name; + + return $this; + } + + /** + * @return string + */ + public function getUri() + { + return $this->uri; + } + + /** + * @param string $uri + * @return $this + */ + public function setUri($uri) + { + $this->uri = $uri; + + return $this; + } + + /** + * @return string + */ + public function getEmail() + { + return $this->email; + } + + /** + * @param string $email + * @return $this + */ + public function setEmail($email) + { + $this->email = $email; + + return $this; + } + +} diff --git a/src/FeedIo/Feed/Item/AuthorInterface.php b/src/FeedIo/Feed/Item/AuthorInterface.php new file mode 100644 index 00000000..4a2f458d --- /dev/null +++ b/src/FeedIo/Feed/Item/AuthorInterface.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FeedIo\Feed\Item; + +/** + * Describe a Author instance + * + */ +interface AuthorInterface +{ + + /** + * @return string + */ + public function getName(); + + /** + * @param string $name + * @return $this + */ + public function setName($name); + + /** + * @return string + */ + public function getUri(); + + /** + * @param string $uri + * @return $this + */ + public function setUri($uri); + + /** + * @return string + */ + public function getEmail(); + + /** + * @param string $email + * @return $this + */ + public function setEmail($email); + +} diff --git a/src/FeedIo/Feed/ItemInterface.php b/src/FeedIo/Feed/ItemInterface.php index 2a5cf40f..88a57112 100644 --- a/src/FeedIo/Feed/ItemInterface.php +++ b/src/FeedIo/Feed/ItemInterface.php @@ -11,6 +11,7 @@ namespace FeedIo\Feed; use FeedIo\Feed\Item\MediaInterface; +use FeedIo\Feed\Item\AuthorInterface; /** * Describes an Item instance @@ -57,4 +58,27 @@ public function hasMedia(); * @return MediaInterface */ public function newMedia(); + + /** + * returns the author attribute + * + * @return AuthorInterface + */ + public function getAuthor(); + + /** + * sets $author to the object's attributes + * + * @param AuthorInterface $author + * @return $this + */ + public function setAuthor(AuthorInterface $author); + + /** + * returns a new AuthorInterface + * + * @return AuthorInterface + */ + public function newAuthor(); + } diff --git a/src/FeedIo/Rule/Atom/Author.php b/src/FeedIo/Rule/Atom/Author.php new file mode 100644 index 00000000..2f54ce10 --- /dev/null +++ b/src/FeedIo/Rule/Atom/Author.php @@ -0,0 +1,61 @@ + + * + * 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\AuthorInterface; +use FeedIo\Feed\ItemInterface; +use FeedIo\Feed\NodeInterface; +use FeedIo\RuleAbstract; + +class Author extends RuleAbstract +{ + + const NODE_NAME = 'author'; + + /** + * @param NodeInterface $node + * @param \DOMElement $element + * @return mixed + */ + 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')); + $node->setAuthor($author); + } + + return $this; + } + + /** + * creates the accurate DomElement content according to the $item's property + * + * @param \DomDocument $document + * @param NodeInterface $node + * @return \DomElement + */ + 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()); + + return $element; + } + + return; + } +} diff --git a/src/FeedIo/Rule/Author.php b/src/FeedIo/Rule/Author.php new file mode 100644 index 00000000..4ede42f0 --- /dev/null +++ b/src/FeedIo/Rule/Author.php @@ -0,0 +1,55 @@ + + * + * 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\AuthorInterface; +use FeedIo\Feed\ItemInterface; +use FeedIo\Feed\NodeInterface; +use FeedIo\RuleAbstract; + +class Author extends RuleAbstract +{ + + const NODE_NAME = 'author'; + + /** + * @param NodeInterface $node + * @param \DOMElement $element + * @return mixed + */ + public function setProperty(NodeInterface $node, \DOMElement $element) + { + if ($node instanceof ItemInterface) { + $author = $node->newAuthor(); + $author->setName($element->nodeValue); + $node->setAuthor($author); + } + + return $this; + } + + /** + * creates the accurate DomElement content according to the $item's property + * + * @param \DomDocument $document + * @param NodeInterface $node + * @return \DomElement|null + */ + public function createElement(\DomDocument $document, NodeInterface $node) + { + if ($node instanceof ItemInterface && !is_null($node->getAuthor())) { + return $document->createElement($this->getNodeName(), $node->getAuthor()->getName()); + } + + return; + } + +} diff --git a/src/FeedIo/Standard/Atom.php b/src/FeedIo/Standard/Atom.php index 5f92cf98..3a692577 100644 --- a/src/FeedIo/Standard/Atom.php +++ b/src/FeedIo/Standard/Atom.php @@ -11,6 +11,7 @@ namespace FeedIo\Standard; use DOMDocument; +use FeedIo\Rule\Atom\Author; use FeedIo\Rule\Atom\LinkNode; use FeedIo\Rule\Description; use FeedIo\Rule\PublicId; @@ -82,11 +83,13 @@ public function buildFeedRuleSet() public function buildItemRuleSet() { $ruleSet = $this->buildFeedRuleSet(); - $ruleSet->add(new Description('content'), ['summary']); + $ruleSet + ->add(new Author()) + ->add(new Description('content'), ['summary']); return $ruleSet; - } - + } + /** * @return RuleSet */ diff --git a/src/FeedIo/Standard/Rss.php b/src/FeedIo/Standard/Rss.php index a50e86da..f09e7933 100644 --- a/src/FeedIo/Standard/Rss.php +++ b/src/FeedIo/Standard/Rss.php @@ -11,6 +11,7 @@ namespace FeedIo\Standard; use DOMDocument; +use FeedIo\Rule\Author; use FeedIo\Rule\Description; use FeedIo\Rule\Link; use FeedIo\Rule\PublicId; @@ -97,6 +98,7 @@ public function buildItemRuleSet() { $ruleSet = $this->buildBaseRuleSet(); $ruleSet + ->add(new Author()) ->add(new Link()) ->add(new PublicId()) ->add(new Description()) @@ -104,8 +106,8 @@ public function buildItemRuleSet() ->add($this->getModifiedSinceRule(static::DATE_NODE_TAGNAME)); return $ruleSet; - } - + } + /** * @return RuleSet */ @@ -116,5 +118,5 @@ protected function buildBaseRuleSet() return $ruleSet; } - + } diff --git a/tests/FeedIo/Feed/Item/AuthorTest.php b/tests/FeedIo/Feed/Item/AuthorTest.php new file mode 100644 index 00000000..4c0d5383 --- /dev/null +++ b/tests/FeedIo/Feed/Item/AuthorTest.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FeedIo\Feed\Item; + +class AuthorTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \FeedIo\Feed\Item\Author + */ + protected $object; + + protected function setUp() + { + $this->object = new Author(); + } + + public function testSetName() + { + $this->object->setName('John Doe'); + $this->assertEquals('John Doe', $this->object->getName()); + } + + public function testSetUri() + { + $this->object->setUri('http://localhost'); + $this->assertEquals('http://localhost', $this->object->getUri()); + } + + public function testSetEmail() + { + $this->object->setEmail('john@localhost'); + $this->assertEquals('john@localhost', $this->object->getEmail()); + } +} diff --git a/tests/FeedIo/Feed/ItemTest.php b/tests/FeedIo/Feed/ItemTest.php index 0c87182a..594c5d98 100644 --- a/tests/FeedIo/Feed/ItemTest.php +++ b/tests/FeedIo/Feed/ItemTest.php @@ -12,6 +12,7 @@ use FeedIo\Feed\Node\Element; use FeedIo\Feed\Item\Media; +use FeedIo\Feed\Item\Author; class ItemTest extends \PHPUnit_Framework_TestCase { @@ -164,4 +165,18 @@ public function testGetMedias() $this->assertEquals(1, $count); } + + public function testSetAuthor() + { + $author = new Author(); + $author->setName('test'); + + $this->object->setAuthor($author); + $this->assertEquals($author->getName(), $this->object->getAuthor()->getName()); + } + + public function testNewAuthor() + { + $this->assertInstanceOf('\FeedIo\Feed\Item\AuthorInterface', $this->object->newAuthor()); + } } diff --git a/tests/FeedIo/Parser/ParserTestAbstract.php b/tests/FeedIo/Parser/ParserTestAbstract.php index 521b8393..0ff6637a 100644 --- a/tests/FeedIo/Parser/ParserTestAbstract.php +++ b/tests/FeedIo/Parser/ParserTestAbstract.php @@ -77,7 +77,7 @@ public function testParseBody() $this->assertNotEmpty($item->getLastModified()); $this->assertNotEmpty($item->getLink()); $this->assertCount(1, $item->getAllElements()); - $this->assertTrue($item->hasElement('author')); + $this->assertTrue($item->hasElement('extra')); $this->runCategoriesTest($item); } } @@ -86,10 +86,10 @@ protected function runCategoriesTest(\FeedIo\Feed\NodeInterface $node) { $categories = $node->getCategories(); $this->assertCount(1, $categories); - + $category = $categories->current(); $this->assertInstanceOf('\FeedIo\Feed\Node\CategoryInterface', $category); - + $this->assertNotEmpty($category->getTerm()); $this->assertNotEmpty($category->getLabel()); } diff --git a/tests/FeedIo/Rule/Atom/AuthorTest.php b/tests/FeedIo/Rule/Atom/AuthorTest.php new file mode 100644 index 00000000..bacc1dd9 --- /dev/null +++ b/tests/FeedIo/Rule/Atom/AuthorTest.php @@ -0,0 +1,61 @@ +object = new Author(); + } + + public function testGetNodeName() + { + $this->assertEquals('author', $this->object->getNodeName()); + } + + public function testSet() + { + $item = new Item(); + + $document = new \DOMDocument(); + + $author = $document->createElement('author'); + $author->setAttribute('name', 'John Doe'); + $author->setAttribute('uri', 'http://localhost'); + $author->setAttribute('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 testCreateElement() + { + $item = new Item(); + $author = new \FeedIo\Feed\Item\Author; + $author->setName('John Doe'); + $author->setUri('http://localhost'); + $author->setEmail('john@localhost'); + $item->setAuthor($author); + + $element = $this->object->createElement(new \DOMDocument(), $item); + $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')); + } +} diff --git a/tests/FeedIo/Rule/AuthorTest.php b/tests/FeedIo/Rule/AuthorTest.php new file mode 100644 index 00000000..427d7f88 --- /dev/null +++ b/tests/FeedIo/Rule/AuthorTest.php @@ -0,0 +1,50 @@ +object = new Author(); + } + + public function testGetNodeName() + { + $this->assertEquals('author', $this->object->getNodeName()); + } + + public function testSet() + { + $item = new Item(); + + $this->object->setProperty($item, new \DOMElement('author', self::AUTHOR)); + $this->assertEquals(self::AUTHOR, $item->getAuthor()->getName()); + } + + public function testCreateElement() + { + $item = new Item(); + $author = new \FeedIo\Feed\Item\Author; + $author->setName(self::AUTHOR); + $item->setAuthor($author); + + $element = $this->object->createElement(new \DOMDocument(), $item); + $this->assertInstanceOf('\DomElement', $element); + $this->assertEquals(self::AUTHOR, $element->nodeValue); + $this->assertEquals('author', $element->nodeName); + } +} diff --git a/tests/samples/rss/sample-rss.xml b/tests/samples/rss/sample-rss.xml index fab5fd86..98a4979f 100644 --- a/tests/samples/rss/sample-rss.xml +++ b/tests/samples/rss/sample-rss.xml @@ -3,7 +3,7 @@ RSS Title This is an example of an RSS feed - 1 + 1 sample http://www.someexamplerssdomain.com/main.html Mon, 06 Sep 2010 00:01:00 GMT @@ -18,6 +18,9 @@ unique string per item Mon, 06 Sep 2009 16:45:00 GMT John Doe + + Some content + diff --git a/tests/samples/sample-atom.xml b/tests/samples/sample-atom.xml index a302700a..a94dbef4 100644 --- a/tests/samples/sample-atom.xml +++ b/tests/samples/sample-atom.xml @@ -27,6 +27,9 @@ http://johndoe.com + + Some content +