Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00%
0 / 1
85.71%
6 / 7
CRAP
98.25%
56 / 57
AtomParser
0.00%
0 / 1
85.71%
6 / 7
19
98.25%
56 / 57
 getNodes($content)
0.00%
0 / 1
4.01
91.67%
11 / 12
 create(DOMElement $node)
100.00%
1 / 1
1
100.00%
8 / 8
 getPropertiesMapping()
100.00%
1 / 1
1
100.00%
5 / 5
 setLink(DOMElement $node, Item $item)
100.00%
1 / 1
4
100.00%
10 / 10
 setAuthor(DOMElement $node, Item $item)
100.00%
1 / 1
4
100.00%
10 / 10
 setDate(DOMElement $node, Item $item)
100.00%
1 / 1
3
100.00%
7 / 7
 setTags(DOMElement $node, Item $item)
100.00%
1 / 1
2
100.00%
5 / 5
<?php
/**
* This file is part of the FastFeed package.
*
* (c) Daniel González <daniel@desarrolla2.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FastFeed\Parser;
use DOMElement;
use DateTime;
use FastFeed\Item;
use FastFeed\Exception\RuntimeException;
/**
* AtomParser
*/
class AtomParser extends AbstractParser implements ParserInterface
{
/**
* Retrieve a Items's array
*
* @param $content
*
* @return array
* @throws \FastFeed\Exception\RuntimeException
*/
public function getNodes($content)
{
$items = array();
$document = $this->createDocumentFromXML($content);
$nodes = $document->getElementsByTagName('entry');
if ($nodes->length) {
foreach ($nodes as $node) {
try {
$item = $this->create($node);
$items[] = $item;
} catch (\Exception $e) {
throw new RuntimeException($e->getMessage());
}
}
}
return $items;
}
/**
* @param DOMElement $node
*
* @return Item
*/
public function create(DOMElement $node)
{
$item = new Item();
$this->setProperties($node, $item);
$this->setLink($node, $item);
$this->setAuthor($node, $item);
$this->setDate($node, $item);
$this->setTags($node, $item);
$this->executeAggregators($node, $item);
return $item;
}
/**
* @return array
*/
protected function getPropertiesMapping()
{
return array(
'setId' => 'id',
'setName' => 'title',
'setIntro' => 'content',
'setContent' => 'content',
);
}
/**
* @param DOMElement $node
* @param Item $item
*/
protected function setLink(DOMElement $node, Item $item)
{
$nodeList = $node->getElementsByTagName('link');
if ($nodeList->length) {
foreach ($nodeList as $nodeResult) {
if ($nodeResult->getAttribute('type') != 'text/html') {
continue;
}
$item->setSource($nodeResult->getAttribute('href'));
break;
}
}
}
/**
* @param DOMElement $node
* @param Item $item
*/
protected function setAuthor(DOMElement $node, Item $item)
{
$nodeList = $node->getElementsByTagName('author');
if ($nodeList->length) {
foreach ($nodeList->item(0)->childNodes as $nodeResult) {
if ($nodeResult->nodeName != 'email') {
continue;
}
$item->setAuthor($nodeResult->nodeValue);
break;
}
}
}
/**
* @param DOMElement $node
* @param Item $item
*/
protected function setDate(DOMElement $node, Item $item)
{
$value = $this->getNodeValueByTagName($node, 'published');
if ($value) {
if (strtotime($value)) {
$item->setDate(new DateTime($value));
}
}
}
/**
* @param DOMElement $node
* @param Item $item
*/
protected function setTags(DOMElement $node, Item $item)
{
$tags = $this->getNodePropertyByTagName($node, 'category', 'term');
foreach ($tags as $tag) {
$item->addTag($tag);
}
}
}