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:  * AtomParser
 19:  */
 20: class AtomParser 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('entry');
 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->setLink($node, $item);
 60:         $this->setAuthor($node, $item);
 61:         $this->setDate($node, $item);
 62:         $this->setTags($node, $item);
 63:         $this->executeAggregators($node, $item);
 64: 
 65:         return $item;
 66:     }
 67: 
 68:     /**
 69:      * @return array
 70:      */
 71:     protected function getPropertiesMapping()
 72:     {
 73:         return array(
 74:             'setId' => 'id',
 75:             'setName' => 'title',
 76:             'setIntro' => 'content',
 77:             'setContent' => 'content',
 78:         );
 79:     }
 80: 
 81:     /**
 82:      * @param DOMElement $node
 83:      * @param Item       $item
 84:      */
 85:     protected function setLink(DOMElement $node, Item $item)
 86:     {
 87:         $nodeList = $node->getElementsByTagName('link');
 88:         if ($nodeList->length) {
 89:             foreach ($nodeList as $nodeResult) {
 90:                 if ($nodeResult->getAttribute('type') != 'text/html') {
 91:                     continue;
 92:                 }
 93:                 $item->setSource($nodeResult->getAttribute('href'));
 94: 
 95:                 break;
 96:             }
 97:         }
 98:     }
 99: 
100:     /**
101:      * @param DOMElement $node
102:      * @param Item       $item
103:      */
104:     protected function setAuthor(DOMElement $node, Item $item)
105:     {
106:         $nodeList = $node->getElementsByTagName('author');
107:         if ($nodeList->length) {
108:             foreach ($nodeList->item(0)->childNodes as $nodeResult) {
109:                 if ($nodeResult->nodeName != 'email') {
110:                     continue;
111:                 }
112:                 $item->setAuthor($nodeResult->nodeValue);
113: 
114:                 break;
115:             }
116:         }
117:     }
118: 
119:     /**
120:      * @param DOMElement $node
121:      * @param Item       $item
122:      */
123:     protected function setDate(DOMElement $node, Item $item)
124:     {
125:         $value = $this->getNodeValueByTagName($node, 'published');
126:         if ($value) {
127:             if (strtotime($value)) {
128:                 $item->setDate(new DateTime($value));
129:             }
130:         }
131:     }
132: 
133:     /**
134:      * @param DOMElement $node
135:      * @param Item       $item
136:      */
137:     protected function setTags(DOMElement $node, Item $item)
138:     {
139:         $tags = $this->getNodePropertyByTagName($node, 'category', 'term');
140:         foreach ($tags as $tag) {
141:             $item->addTag($tag);
142:         }
143:     }
144: }
145: 
API documentation generated by ApiGen 2.8.0