PageRenderTime 31ms CodeModel.GetById 23ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Feed/Reader/FeedSet.php

https://bitbucket.org/hamidrezas/melobit
PHP | 148 lines | 74 code | 13 blank | 61 comment | 23 complexity | 655d939ad24a6d311a958280d075fb47 MD5 | raw file
Possible License(s): AGPL-1.0
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Feed_Reader
 17 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 19 * @version    $Id: FeedSet.php 24594 2012-01-05 21:27:01Z matthew $
 20 */
 21
 22/**
 23 * @see Zend_Feed_Reader
 24 */
 25require_once 'Zend/Feed/Reader.php';
 26
 27/**
 28 * @see Zend_Uri
 29 */
 30require_once 'Zend/Uri.php';
 31
 32/**
 33 * @category   Zend
 34 * @package    Zend_Feed_Reader
 35 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 36 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 37 */
 38class Zend_Feed_Reader_FeedSet extends ArrayObject
 39{
 40
 41    public $rss = null;
 42
 43    public $rdf = null;
 44
 45    public $atom = null;
 46
 47    /**
 48     * Import a DOMNodeList from any document containing a set of links
 49     * for alternate versions of a document, which will normally refer to
 50     * RSS/RDF/Atom feeds for the current document.
 51     *
 52     * All such links are stored internally, however the first instance of
 53     * each RSS, RDF or Atom type has its URI stored as a public property
 54     * as a shortcut where the use case is simply to get a quick feed ref.
 55     *
 56     * Note that feeds are not loaded at this point, but will be lazy
 57     * loaded automatically when each links 'feed' array key is accessed.
 58     *
 59     * @param DOMNodeList $links
 60     * @param string $uri
 61     * @return void
 62     */
 63    public function addLinks(DOMNodeList $links, $uri)
 64    {
 65        foreach ($links as $link) {
 66            if (strtolower($link->getAttribute('rel')) !== 'alternate'
 67                || !$link->getAttribute('type') || !$link->getAttribute('href')) {
 68                continue;
 69            }
 70            if (!isset($this->rss) && $link->getAttribute('type') == 'application/rss+xml') {
 71                $this->rss = $this->_absolutiseUri(trim($link->getAttribute('href')), $uri);
 72            } elseif(!isset($this->atom) && $link->getAttribute('type') == 'application/atom+xml') {
 73                $this->atom = $this->_absolutiseUri(trim($link->getAttribute('href')), $uri);
 74            } elseif(!isset($this->rdf) && $link->getAttribute('type') == 'application/rdf+xml') {
 75                $this->rdf = $this->_absolutiseUri(trim($link->getAttribute('href')), $uri);
 76            }
 77            $this[] = new self(array(
 78                'rel' => 'alternate',
 79                'type' => $link->getAttribute('type'),
 80                'href' => $this->_absolutiseUri(trim($link->getAttribute('href')), $uri),
 81            ));
 82        }
 83    }
 84
 85    /**
 86     *  Attempt to turn a relative URI into an absolute URI
 87     */
 88    protected function _absolutiseUri($link, $uri = null)
 89    {
 90        if (!Zend_Uri::check($link)) {
 91            if ($uri !== null) {
 92                $uri = Zend_Uri::factory($uri);
 93
 94                if ($link[0] !== '/') {
 95                    $link = $uri->getPath() . '/' . $link;
 96                }
 97
 98                $link = $uri->getScheme() . '://' . $uri->getHost() . '/' . $this->_canonicalizePath($link);
 99                if (!Zend_Uri::check($link)) {
100                    $link = null;
101                }
102            }
103        }
104        return $link;
105    }
106
107    /**
108     *  Canonicalize relative path
109     */
110    protected function _canonicalizePath($path)
111    {
112        $parts = array_filter(explode('/', $path));
113        $absolutes = array();
114        foreach ($parts as $part) {
115            if ('.' == $part) {
116                continue;
117            }
118            if ('..' == $part) {
119                array_pop($absolutes);
120            } else {
121                $absolutes[] = $part;
122            }
123        }
124        return implode('/', $absolutes);
125    }
126
127    /**
128     * Supports lazy loading of feeds using Zend_Feed_Reader::import() but
129     * delegates any other operations to the parent class.
130     *
131     * @param string $offset
132     * @return mixed
133     * @uses Zend_Feed_Reader
134     */
135    public function offsetGet($offset)
136    {
137        if ($offset == 'feed' && !$this->offsetExists('feed')) {
138            if (!$this->offsetExists('href')) {
139                return null;
140            }
141            $feed = Zend_Feed_Reader::import($this->offsetGet('href'));
142            $this->offsetSet('feed', $feed);
143            return $feed;
144        }
145        return parent::offsetGet($offset);
146    }
147
148}