Skip to content

Commit

Permalink
Merge pull request #86 from ruudvdd/element-aware-interface-refactor
Browse files Browse the repository at this point in the history
Refactor node and element implement a ElementsAwareInterface interface for type checking
  • Loading branch information
alexdebril authored Jun 3, 2017
2 parents 6bc11dd + b20784f commit 19deb31
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 99 deletions.
57 changes: 57 additions & 0 deletions src/FeedIo/Feed/ElementsAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\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();
}
13 changes: 0 additions & 13 deletions src/FeedIo/Feed/ElementsAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions src/FeedIo/Feed/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
3 changes: 2 additions & 1 deletion src/FeedIo/Feed/Node/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
38 changes: 0 additions & 38 deletions src/FeedIo/Feed/Node/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
45 changes: 0 additions & 45 deletions src/FeedIo/Feed/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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();
}

0 comments on commit 19deb31

Please sign in to comment.