Overview

Namespaces

  • FastFeed
    • Aggregator
    • Cache
    • Exception
    • Logger
    • Parser
    • Processor
  • PHP

Classes

  • AbstractDomParser
  • AbstractParser
  • AtomParser
  • RSSParser

Interfaces

  • ParserInterface
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * This file is part of the FastFeed package.
  4:  *
  5:  * (c) Daniel González <daniel@desarrolla2.com>
  6:  *
  7:  * For the full copyright and license information, please view the LICENSE
  8:  * file that was distributed with this source code.
  9:  */
 10: namespace FastFeed\Parser;
 11: 
 12: use DOMElement;
 13: use DateTime;
 14: use FastFeed\Item;
 15: use FastFeed\Exception\RuntimeException;
 16: 
 17: /**
 18:  * RSSParser
 19:  */
 20: class RSSParser extends AbstractParser implements ParserInterface
 21: {
 22: 
 23:     /**
 24:      * Retrieve a Items's array
 25:      *
 26:      * @param $content
 27:      *
 28:      * @return array
 29:      * @throws \FastFeed\Exception\RuntimeException
 30:      */
 31:     public function getNodes($content)
 32:     {
 33:         $items = array();
 34:         $document = $this->createDocumentFromXML($content);
 35:         $nodes = $document->getElementsByTagName('item');
 36:         if ($nodes->length) {
 37:             foreach ($nodes as $node) {
 38:                 try {
 39:                     $item = $this->create($node);
 40:                     $items[] = $item;
 41:                 } catch (\Exception $e) {
 42:                     throw new RuntimeException($e->getMessage());
 43:                 }
 44:             }
 45:         }
 46: 
 47:         return $items;
 48:     }
 49: 
 50:     /**
 51:      * @param DOMElement $node
 52:      *
 53:      * @return Item
 54:      */
 55:     public function create(DOMElement $node)
 56:     {
 57:         $item = new Item();
 58:         $this->setProperties($node, $item);
 59:         $this->setDate($node, $item);
 60:         $this->setTags($node, $item);
 61:         $this->executeAggregators($node, $item);
 62: 
 63:         return $item;
 64:     }
 65: 
 66:     /**
 67:      * @return array
 68:      */
 69:     protected function getPropertiesMapping()
 70:     {
 71:         return array(
 72:             'setId' => 'link',
 73:             'setName' => 'title',
 74:             'setIntro' => 'description',
 75:             'setContent' => 'description',
 76:             'setSource' => 'link',
 77:             'setAuthor' => 'author'
 78:         );
 79:     }
 80: 
 81:     /**
 82:      * @param DOMElement $node
 83:      * @param Item       $item
 84:      */
 85:     protected function setDate(DOMElement $node, Item $item)
 86:     {
 87:         $value = $this->getNodeValueByTagName($node, 'pubDate');
 88:         if ($value) {
 89:             if (strtotime($value)) {
 90:                 $item->setDate(new DateTime($value));
 91:             }
 92:         }
 93:     }
 94: 
 95:     /**
 96:      * @param DOMElement $node
 97:      * @param Item       $item
 98:      */
 99:     protected function setTags(DOMElement $node, Item $item)
100:     {
101:         $tags = $this->getNodeValuesByTagName($node, 'category');
102:         foreach ($tags as $tag) {
103:             $item->addTag($tag);
104:         }
105:     }
106: }
107: 
API documentation generated by ApiGen 2.8.0