Skip to content

Commit

Permalink
Merge pull request #47 from alexdebril/issue/46
Browse files Browse the repository at this point in the history
Issue/46
  • Loading branch information
alexdebril authored Sep 19, 2016
2 parents b55d691 + d459cf4 commit f7e3c4e
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/FeedIo/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use FeedIo\Feed\Item;
use FeedIo\Feed\ItemInterface;

class Feed extends Node implements FeedInterface
class Feed extends Node implements FeedInterface, \JsonSerializable
{
/**
* @var \ArrayIterator
Expand Down Expand Up @@ -126,4 +126,29 @@ public function newItem()
{
return new Item();
}

/**
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}

/**
* @return array
*/
public function toArray()
{
$items = [];

foreach( $this->items as $item ) {
$items[] = $item->toArray();
}

$properties = parent::toArray();
$properties['items'] = $items;

return $properties;
}
}
42 changes: 42 additions & 0 deletions src/FeedIo/Feed/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ public function getCategories()
return $this->categories;
}

/**
* @return \Generator
*/
public function getCategoriesGenerator()
{
foreach( $this->categories as $category ) {
yield $category->getlabel();
}
}

/**
* adds a category to the node
*
Expand Down Expand Up @@ -183,6 +193,18 @@ public function listElements()
}
}

/**
* @return \Generator
*/
public function getElementsGenerator()
{
$elements = $this->getAllElements();

foreach( $elements as $element ) {
yield $element->getName() => $element->getValue();
}
}

/**
* @return string
*/
Expand Down Expand Up @@ -277,4 +299,24 @@ public function setLink($link)

return $this;
}

/**
* @return array
*/
public function toArray()
{
$properties = get_object_vars($this);

foreach( $properties as $name => $property ) {
if ( $property instanceof \DateTime ) {
$properties[$name] = $property->format(\DateTime::ATOM);
}
}

$properties['elements'] = iterator_to_array($this->getElementsGenerator());
$properties['categories'] = iterator_to_array($this->getCategoriesGenerator());

return $properties;
}

}
45 changes: 44 additions & 1 deletion tests/FeedIo/Feed/NodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace FeedIo\Feed;

use FeedIo\Feed\Node\Category;

class NodeTest extends \PHPUnit_Framework_TestCase
{
/**
Expand Down Expand Up @@ -62,7 +64,48 @@ public function testNewCategory()
{
$this->assertInstanceOf('\FeedIo\Feed\Node\CategoryInterface', $this->object->newCategory());
}


public function testGetCategoryAsGenerator()
{
$category = new Category();
$category->setLabel('test');

$this->object->addCategory($category);

$categories = $this->object->getCategoriesGenerator();

$this->assertEquals('test', $categories->current());
}

public function testGetElementsAsGenerator()
{
$this->object->set('foo', 'bar');

$elements = $this->object->getElementsGenerator();

$this->assertEquals('foo', $elements->key());
$this->assertEquals('bar', $elements->current());
}

public function testToArray()
{
$category = new Category();
$category->setLabel('test');
$this->object->set('foo', 'bar')
->setLastModified(new \DateTime())
->setTitle('my title')
->addCategory($category)
->setDescription('lorem ipsum');

$out = $this->object->toArray();

$this->assertEquals('lorem ipsum', $out['description']);
$this->assertEquals('my title', $out['title']);
$this->assertEquals('bar', $out['elements']['foo']);
$this->assertEquals('test', $out['categories'][0]);
$this->assertInternalType('string', $out['lastModified']);
}

public function testAddCategory()
{
$category = new \FeedIo\Feed\Node\Category;
Expand Down
25 changes: 25 additions & 0 deletions tests/FeedIo/FeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,29 @@ public function testUrl()

$this->assertEquals($url, $feed->getUrl());
}

public function testToArray()
{
$item = new Feed\Item();
$item->setTitle('foo-bar');
$this->object->add($item);

$out = $this->object->toArray();

$this->assertEquals('foo-bar', $out['items'][0]['title']);
}

public function testJsonSerialize()
{
$item = new Feed\Item();
$item->setTitle('foo-bar');
$this->object->add($item);
$this->object->setTitle('hello');
$this->object->setLastModified(new \DateTime());

$json = json_encode($this->object);

$this->assertInternalType('string', $json);
$this->assertInstanceOf('stdClass', json_decode($json));
}
}

0 comments on commit f7e3c4e

Please sign in to comment.