web/Zend/Feed/Reader/Entry/Atom.php
changeset 0 4eba9c11703f
equal deleted inserted replaced
-1:000000000000 0:4eba9c11703f
       
     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-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Atom.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Feed_Reader
       
    24  */
       
    25 require_once 'Zend/Feed/Reader.php';
       
    26 
       
    27 /**
       
    28  * @see Zend_Feed_Reader_EntryInterface
       
    29  */
       
    30 require_once 'Zend/Feed/Reader/EntryInterface.php';
       
    31 
       
    32 /**
       
    33  * @see Zend_Feed_Reader_EntryAbstract
       
    34  */
       
    35 require_once 'Zend/Feed/Reader/EntryAbstract.php';
       
    36 
       
    37 /**
       
    38  * @see Zend_Feed_Reader_Extension_Atom_Entry
       
    39  */
       
    40 require_once 'Zend/Feed/Reader/Extension/Atom/Entry.php';
       
    41 
       
    42 /**
       
    43  * @category   Zend
       
    44  * @package    Zend_Feed_Reader
       
    45  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    46  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    47  */
       
    48 class Zend_Feed_Reader_Entry_Atom extends Zend_Feed_Reader_EntryAbstract implements Zend_Feed_Reader_EntryInterface
       
    49 {
       
    50     /**
       
    51      * XPath query
       
    52      *
       
    53      * @var string
       
    54      */
       
    55     protected $_xpathQuery = '';
       
    56 
       
    57     /**
       
    58      * Constructor
       
    59      *
       
    60      * @param  DOMElement $entry
       
    61      * @param  int $entryKey
       
    62      * @param  string $type
       
    63      * @return void
       
    64      */
       
    65     public function __construct(DOMElement $entry, $entryKey, $type = null)
       
    66     {
       
    67         parent::__construct($entry, $entryKey, $type);
       
    68 
       
    69         // Everyone by now should know XPath indices start from 1 not 0
       
    70         $this->_xpathQuery = '//atom:entry[' . ($this->_entryKey + 1) . ']';
       
    71 
       
    72         $atomClass = Zend_Feed_Reader::getPluginLoader()->getClassName('Atom_Entry');
       
    73         $this->_extensions['Atom_Entry'] = new $atomClass($entry, $entryKey, $type);
       
    74 
       
    75         $threadClass = Zend_Feed_Reader::getPluginLoader()->getClassName('Thread_Entry');
       
    76         $this->_extensions['Thread_Entry'] = new $threadClass($entry, $entryKey, $type);
       
    77         
       
    78         $threadClass = Zend_Feed_Reader::getPluginLoader()->getClassName('DublinCore_Entry');
       
    79         $this->_extensions['DublinCore_Entry'] = new $threadClass($entry, $entryKey, $type);
       
    80     }
       
    81 
       
    82     /**
       
    83      * Get the specified author
       
    84      *
       
    85      * @param  int $index
       
    86      * @return string|null
       
    87      */
       
    88     public function getAuthor($index = 0)
       
    89     {
       
    90         $authors = $this->getAuthors();
       
    91 
       
    92         if (isset($authors[$index])) {
       
    93             return $authors[$index];
       
    94         }
       
    95 
       
    96         return null;
       
    97     }
       
    98 
       
    99     /**
       
   100      * Get an array with feed authors
       
   101      *
       
   102      * @return array
       
   103      */
       
   104     public function getAuthors()
       
   105     {
       
   106         if (array_key_exists('authors', $this->_data)) {
       
   107             return $this->_data['authors'];
       
   108         }
       
   109 
       
   110         $people = $this->getExtension('Atom')->getAuthors();
       
   111 
       
   112         $this->_data['authors'] = $people;
       
   113 
       
   114         return $this->_data['authors'];
       
   115     }
       
   116 
       
   117     /**
       
   118      * Get the entry content
       
   119      *
       
   120      * @return string
       
   121      */
       
   122     public function getContent()
       
   123     {
       
   124         if (array_key_exists('content', $this->_data)) {
       
   125             return $this->_data['content'];
       
   126         }
       
   127 
       
   128         $content = $this->getExtension('Atom')->getContent();
       
   129 
       
   130         $this->_data['content'] = $content;
       
   131 
       
   132         return $this->_data['content'];
       
   133     }
       
   134 
       
   135     /**
       
   136      * Get the entry creation date
       
   137      *
       
   138      * @return string
       
   139      */
       
   140     public function getDateCreated()
       
   141     {
       
   142         if (array_key_exists('datecreated', $this->_data)) {
       
   143             return $this->_data['datecreated'];
       
   144         }
       
   145 
       
   146         $dateCreated = $this->getExtension('Atom')->getDateCreated();
       
   147 
       
   148         $this->_data['datecreated'] = $dateCreated;
       
   149 
       
   150         return $this->_data['datecreated'];
       
   151     }
       
   152 
       
   153     /**
       
   154      * Get the entry modification date
       
   155      *
       
   156      * @return string
       
   157      */
       
   158     public function getDateModified()
       
   159     {
       
   160         if (array_key_exists('datemodified', $this->_data)) {
       
   161             return $this->_data['datemodified'];
       
   162         }
       
   163 
       
   164         $dateModified = $this->getExtension('Atom')->getDateModified();
       
   165 
       
   166         $this->_data['datemodified'] = $dateModified;
       
   167 
       
   168         return $this->_data['datemodified'];
       
   169     }
       
   170 
       
   171     /**
       
   172      * Get the entry description
       
   173      *
       
   174      * @return string
       
   175      */
       
   176     public function getDescription()
       
   177     {
       
   178         if (array_key_exists('description', $this->_data)) {
       
   179             return $this->_data['description'];
       
   180         }
       
   181 
       
   182         $description = $this->getExtension('Atom')->getDescription();
       
   183 
       
   184         $this->_data['description'] = $description;
       
   185 
       
   186         return $this->_data['description'];
       
   187     }
       
   188 
       
   189     /**
       
   190      * Get the entry enclosure
       
   191      *
       
   192      * @return string
       
   193      */
       
   194     public function getEnclosure()
       
   195     {
       
   196         if (array_key_exists('enclosure', $this->_data)) {
       
   197             return $this->_data['enclosure'];
       
   198         }
       
   199 
       
   200         $enclosure = $this->getExtension('Atom')->getEnclosure();
       
   201 
       
   202         $this->_data['enclosure'] = $enclosure;
       
   203 
       
   204         return $this->_data['enclosure'];
       
   205     }
       
   206 
       
   207     /**
       
   208      * Get the entry ID
       
   209      *
       
   210      * @return string
       
   211      */
       
   212     public function getId()
       
   213     {
       
   214         if (array_key_exists('id', $this->_data)) {
       
   215             return $this->_data['id'];
       
   216         }
       
   217 
       
   218         $id = $this->getExtension('Atom')->getId();
       
   219 
       
   220         $this->_data['id'] = $id;
       
   221 
       
   222         return $this->_data['id'];
       
   223     }
       
   224 
       
   225     /**
       
   226      * Get a specific link
       
   227      *
       
   228      * @param  int $index
       
   229      * @return string
       
   230      */
       
   231     public function getLink($index = 0)
       
   232     {
       
   233         if (!array_key_exists('links', $this->_data)) {
       
   234             $this->getLinks();
       
   235         }
       
   236 
       
   237         if (isset($this->_data['links'][$index])) {
       
   238             return $this->_data['links'][$index];
       
   239         }
       
   240 
       
   241         return null;
       
   242     }
       
   243 
       
   244     /**
       
   245      * Get all links
       
   246      *
       
   247      * @return array
       
   248      */
       
   249     public function getLinks()
       
   250     {
       
   251         if (array_key_exists('links', $this->_data)) {
       
   252             return $this->_data['links'];
       
   253         }
       
   254 
       
   255         $links = $this->getExtension('Atom')->getLinks();
       
   256 
       
   257         $this->_data['links'] = $links;
       
   258 
       
   259         return $this->_data['links'];
       
   260     }
       
   261 
       
   262     /**
       
   263      * Get a permalink to the entry
       
   264      *
       
   265      * @return string
       
   266      */
       
   267     public function getPermalink()
       
   268     {
       
   269         return $this->getLink(0);
       
   270     }
       
   271 
       
   272     /**
       
   273      * Get the entry title
       
   274      *
       
   275      * @return string
       
   276      */
       
   277     public function getTitle()
       
   278     {
       
   279         if (array_key_exists('title', $this->_data)) {
       
   280             return $this->_data['title'];
       
   281         }
       
   282 
       
   283         $title = $this->getExtension('Atom')->getTitle();
       
   284 
       
   285         $this->_data['title'] = $title;
       
   286 
       
   287         return $this->_data['title'];
       
   288     }
       
   289 
       
   290     /**
       
   291      * Get the number of comments/replies for current entry
       
   292      *
       
   293      * @return integer
       
   294      */
       
   295     public function getCommentCount()
       
   296     {
       
   297         if (array_key_exists('commentcount', $this->_data)) {
       
   298             return $this->_data['commentcount'];
       
   299         }
       
   300 
       
   301         $commentcount = $this->getExtension('Thread')->getCommentCount();
       
   302 
       
   303         if (!$commentcount) {
       
   304             $commentcount = $this->getExtension('Atom')->getCommentCount();
       
   305         }
       
   306 
       
   307         $this->_data['commentcount'] = $commentcount;
       
   308 
       
   309         return $this->_data['commentcount'];
       
   310     }
       
   311 
       
   312     /**
       
   313      * Returns a URI pointing to the HTML page where comments can be made on this entry
       
   314      *
       
   315      * @return string
       
   316      */
       
   317     public function getCommentLink()
       
   318     {
       
   319         if (array_key_exists('commentlink', $this->_data)) {
       
   320             return $this->_data['commentlink'];
       
   321         }
       
   322 
       
   323         $commentlink = $this->getExtension('Atom')->getCommentLink();
       
   324 
       
   325         $this->_data['commentlink'] = $commentlink;
       
   326 
       
   327         return $this->_data['commentlink'];
       
   328     }
       
   329 
       
   330     /**
       
   331      * Returns a URI pointing to a feed of all comments for this entry
       
   332      *
       
   333      * @return string
       
   334      */
       
   335     public function getCommentFeedLink()
       
   336     {
       
   337         if (array_key_exists('commentfeedlink', $this->_data)) {
       
   338             return $this->_data['commentfeedlink'];
       
   339         }
       
   340 
       
   341         $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink();
       
   342 
       
   343         $this->_data['commentfeedlink'] = $commentfeedlink;
       
   344 
       
   345         return $this->_data['commentfeedlink'];
       
   346     }
       
   347     
       
   348     /**
       
   349      * Get category data as a Zend_Feed_Reader_Collection_Category object
       
   350      *
       
   351      * @return Zend_Feed_Reader_Collection_Category
       
   352      */
       
   353     public function getCategories()
       
   354     {
       
   355         if (array_key_exists('categories', $this->_data)) {
       
   356             return $this->_data['categories'];
       
   357         }
       
   358 
       
   359         $categoryCollection = $this->getExtension('Atom')->getCategories();
       
   360         
       
   361         if (count($categoryCollection) == 0) {
       
   362             $categoryCollection = $this->getExtension('DublinCore')->getCategories();
       
   363         }
       
   364 
       
   365         $this->_data['categories'] = $categoryCollection;
       
   366 
       
   367         return $this->_data['categories'];
       
   368     }
       
   369     
       
   370     /**
       
   371      * Get source feed metadata from the entry
       
   372      *
       
   373      * @return Zend_Feed_Reader_Feed_Atom_Source|null
       
   374      */
       
   375     public function getSource()
       
   376     {
       
   377         if (array_key_exists('source', $this->_data)) {
       
   378             return $this->_data['source'];
       
   379         }
       
   380 
       
   381         $source = $this->getExtension('Atom')->getSource();
       
   382 
       
   383         $this->_data['source'] = $source;
       
   384 
       
   385         return $this->_data['source']; 
       
   386     }
       
   387 
       
   388     /**
       
   389      * Set the XPath query (incl. on all Extensions)
       
   390      *
       
   391      * @param DOMXPath $xpath
       
   392      */
       
   393     public function setXpath(DOMXPath $xpath)
       
   394     {
       
   395         parent::setXpath($xpath);
       
   396         foreach ($this->_extensions as $extension) {
       
   397             $extension->setXpath($this->_xpath);
       
   398         }
       
   399     }
       
   400 }