From b20784ffa03c533ce2c8400d9a4680b994480596 Mon Sep 17 00:00:00 2001 From: Ruud Van den Dungen Date: Fri, 26 May 2017 18:17:33 +0200 Subject: [PATCH] refactor node and element to implement a ElementsAwareInterface to easily use it for typehinting etc --- src/FeedIo/Feed/ElementsAwareInterface.php | 57 ++++++++++++++++++++++ src/FeedIo/Feed/ElementsAwareTrait.php | 13 ----- src/FeedIo/Feed/Node.php | 17 ++++++- src/FeedIo/Feed/Node/Element.php | 3 +- src/FeedIo/Feed/Node/ElementInterface.php | 38 --------------- src/FeedIo/Feed/NodeInterface.php | 45 ----------------- 6 files changed, 74 insertions(+), 99 deletions(-) create mode 100644 src/FeedIo/Feed/ElementsAwareInterface.php diff --git a/src/FeedIo/Feed/ElementsAwareInterface.php b/src/FeedIo/Feed/ElementsAwareInterface.php new file mode 100644 index 00000000..c1ef5c4b --- /dev/null +++ b/src/FeedIo/Feed/ElementsAwareInterface.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FeedIo\Feed; + +use FeedIo\Feed\Node\ElementInterface; +use FeedIo\Feed\Node\ElementIterator; + +interface ElementsAwareInterface +{ + /** + * @return ElementInterface + */ + public function newElement(); + + /** + * @param string $name element name + * @return ElementIterator + */ + public function getElementIterator($name); + + /** + * @param string $name element name + * @return boolean true if the element exists + */ + public function hasElement($name); + + /** + * @param ElementInterface $element + * @return $this + */ + public function addElement(ElementInterface $element); + + /** + * Returns all the item's optional elements + * @return \ArrayIterator + */ + public function getAllElements(); + + /** + * Returns the item's optional elements tag names + * @return array + */ + public function listElements(); + + /** + * @return \Generator + */ + public function getElementsGenerator(); +} diff --git a/src/FeedIo/Feed/ElementsAwareTrait.php b/src/FeedIo/Feed/ElementsAwareTrait.php index c741d00b..dbc01d64 100644 --- a/src/FeedIo/Feed/ElementsAwareTrait.php +++ b/src/FeedIo/Feed/ElementsAwareTrait.php @@ -37,19 +37,6 @@ public function newElement() return new Element(); } - /** - * @param string $name element name - * @return mixed - */ - public function getValue($name) - { - foreach ($this->getElementIterator($name) as $element) { - return $element->getValue(); - } - - return; - } - /** * @param string $name element name * @return ElementIterator diff --git a/src/FeedIo/Feed/Node.php b/src/FeedIo/Feed/Node.php index 11ea3b85..fed9cd92 100644 --- a/src/FeedIo/Feed/Node.php +++ b/src/FeedIo/Feed/Node.php @@ -13,10 +13,10 @@ use FeedIo\Feed\Node\Category; use FeedIo\Feed\Node\CategoryInterface; -class Node implements NodeInterface +class Node implements NodeInterface, ElementsAwareInterface { use ElementsAwareTrait; - + /** * @var \ArrayIterator */ @@ -208,6 +208,19 @@ public function setLink($link) return $this; } + /** + * @param string $name element name + * @return mixed + */ + public function getValue($name) + { + foreach ($this->getElementIterator($name) as $element) { + return $element->getValue(); + } + + return; + } + /** * @return array */ diff --git a/src/FeedIo/Feed/Node/Element.php b/src/FeedIo/Feed/Node/Element.php index 864829aa..9460c766 100644 --- a/src/FeedIo/Feed/Node/Element.php +++ b/src/FeedIo/Feed/Node/Element.php @@ -10,9 +10,10 @@ namespace FeedIo\Feed\Node; +use FeedIo\Feed\ElementsAwareInterface; use FeedIo\Feed\ElementsAwareTrait; -class Element implements ElementInterface +class Element implements ElementInterface, ElementsAwareInterface { use ElementsAwareTrait; diff --git a/src/FeedIo/Feed/Node/ElementInterface.php b/src/FeedIo/Feed/Node/ElementInterface.php index eecfcc38..e3084fb9 100644 --- a/src/FeedIo/Feed/Node/ElementInterface.php +++ b/src/FeedIo/Feed/Node/ElementInterface.php @@ -73,42 +73,4 @@ public function getAttributes(); * @return $this */ public function setAttribute($name, $value); - - /** - * returns the ElementIterator to iterate over ElementInterface instances called $name - * - * @param string $name element name - * @return \FeedIo\Feed\Node\ElementIterator - */ - public function getElementIterator($name); - - /** - * returns true if an ElementInterface instance called $name exists - * - * @param string $name element name - * @return boolean true if the element exists - */ - public function hasElement($name); - - /** - * adds $element to the object's attributes - * - * @param ElementInterface $element - * @return $this - */ - public function addElement(ElementInterface $element); - - /** - * Returns all the item's elements - * - * @return \ArrayIterator - */ - public function getAllElements(); - - /** - * Returns the item's elements tag names - * - * @return array - */ - public function listElements(); } diff --git a/src/FeedIo/Feed/NodeInterface.php b/src/FeedIo/Feed/NodeInterface.php index f5931a92..dfcc84f8 100644 --- a/src/FeedIo/Feed/NodeInterface.php +++ b/src/FeedIo/Feed/NodeInterface.php @@ -117,13 +117,6 @@ public function addCategory(CategoryInterface $category); */ public function newCategory(); - /** - * returns a new ElementInterface - * - * @return \FeedIo\Feed\Node\ElementInterface - */ - public function newElement(); - /** * returns an element's value * @@ -140,42 +133,4 @@ public function getValue($name); * @return $this */ public function set($name, $value); - - /** - * returns the ElementIterator to iterate over ElementInterface instances called $name - * - * @param string $name element name - * @return \FeedIo\Feed\Node\ElementIterator - */ - public function getElementIterator($name); - - /** - * returns true if an ElementInterface instance called $name exists - * - * @param string $name element name - * @return boolean true if the element exists - */ - public function hasElement($name); - - /** - * adds $element to the object's attributes - * - * @param ElementInterface $element - * @return $this - */ - public function addElement(ElementInterface $element); - - /** - * Returns all the item's elements - * - * @return \ArrayIterator - */ - public function getAllElements(); - - /** - * Returns the item's elements tag names - * - * @return array - */ - public function listElements(); }