Esempio n. 1
0
 /**
  * Get/load the feed.
  *
  * @param string url The feed url.
  * @param string category An optional category; default is <code>null</code>.
  * @param int limit An optional item limit; default is 5; use 0 for all.
  * @return RssFedd A <code>RssFeed</code> instance.
  */
 public function getFeed($url, $category = null, $limit = 5)
 {
     $cacheKey = Toolbox::hash('feeds', $url, implode(':', $this->config));
     if (!$this->cache || false === ($rssParser = $this->cache->lookup($cacheKey))) {
         $rssParser = new RssParser($this->config);
         // todo: conditional GET
         $rssParser->parse(file_get_contents($url, $this->config['useIncludePath'], $this->getContext()));
     }
     $feed = new RssFeed();
     $feed->setChannel(new RssChannel($rssParser->getChannel()));
     $items = array();
     foreach ($rssParser->getItems() as $itemData) {
         $item = new RssItem($itemData);
         if (null == $category || in_array($category, $item->getCategories())) {
             $items[] = $item;
         }
         if (0 != $limit && $limit <= count($items)) {
             break;
         }
     }
     $feed->setItems(new \ArrayIterator($items));
     // cache if enabled
     if ($this->cache) {
         $this->cache->save($rssParser, $cacheKey);
     }
     return $feed;
 }