web/lib/Zend/Feed/Reader/EntryAbstract.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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: EntryAbstract.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @category   Zend
       
    24  * @package    Zend_Feed_Reader
       
    25  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    26  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    27  */
       
    28 abstract class Zend_Feed_Reader_EntryAbstract
       
    29 {
       
    30     /**
       
    31      * Feed entry data
       
    32      *
       
    33      * @var array
       
    34      */
       
    35     protected $_data = array();
       
    36 
       
    37     /**
       
    38      * DOM document object
       
    39      *
       
    40      * @var DOMDocument
       
    41      */
       
    42     protected $_domDocument = null;
       
    43 
       
    44     /**
       
    45      * Entry instance
       
    46      *
       
    47      * @var Zend_Feed_Entry_Interface
       
    48      */
       
    49     protected $_entry = null;
       
    50 
       
    51     /**
       
    52      * Pointer to the current entry
       
    53      *
       
    54      * @var int
       
    55      */
       
    56     protected $_entryKey = 0;
       
    57 
       
    58     /**
       
    59      * XPath object
       
    60      *
       
    61      * @var DOMXPath
       
    62      */
       
    63     protected $_xpath = null;
       
    64 
       
    65     /**
       
    66      * Registered extensions
       
    67      *
       
    68      * @var array
       
    69      */
       
    70     protected $_extensions = array();
       
    71 
       
    72     /**
       
    73      * Constructor
       
    74      *
       
    75      * @param  DOMElement $entry
       
    76      * @param  int $entryKey
       
    77      * @param  string $type
       
    78      * @return void
       
    79      */
       
    80     public function __construct(DOMElement $entry, $entryKey, $type = null)
       
    81     {
       
    82         $this->_entry       = $entry;
       
    83         $this->_entryKey    = $entryKey;
       
    84         $this->_domDocument = $entry->ownerDocument;
       
    85         if ($type !== null) {
       
    86             $this->_data['type'] = $type;
       
    87         } else {
       
    88             $this->_data['type'] = Zend_Feed_Reader::detectType($feed);
       
    89         }
       
    90         $this->_loadExtensions();
       
    91     }
       
    92 
       
    93     /**
       
    94      * Get the DOM
       
    95      *
       
    96      * @return DOMDocument
       
    97      */
       
    98     public function getDomDocument()
       
    99     {
       
   100         return $this->_domDocument;
       
   101     }
       
   102 
       
   103     /**
       
   104      * Get the entry element
       
   105      *
       
   106      * @return DOMElement
       
   107      */
       
   108     public function getElement()
       
   109     {
       
   110         return $this->_entry;
       
   111     }
       
   112 
       
   113     /**
       
   114      * Get the Entry's encoding
       
   115      *
       
   116      * @return string
       
   117      */
       
   118     public function getEncoding()
       
   119     {
       
   120         $assumed = $this->getDomDocument()->encoding;
       
   121         if (empty($assumed)) {
       
   122             $assumed = 'UTF-8';
       
   123         }
       
   124         return $assumed;
       
   125     }
       
   126 
       
   127     /**
       
   128      * Get entry as xml
       
   129      *
       
   130      * @return string
       
   131      */
       
   132     public function saveXml()
       
   133     {
       
   134         $dom = new DOMDocument('1.0', $this->getEncoding());
       
   135         $entry = $dom->importNode($this->getElement(), true);
       
   136         $dom->appendChild($entry);
       
   137         return $dom->saveXml();
       
   138     }
       
   139 
       
   140     /**
       
   141      * Get the entry type
       
   142      *
       
   143      * @return string
       
   144      */
       
   145     public function getType()
       
   146     {
       
   147         return $this->_data['type'];
       
   148     }
       
   149 
       
   150     /**
       
   151      * Get the XPath query object
       
   152      *
       
   153      * @return DOMXPath
       
   154      */
       
   155     public function getXpath()
       
   156     {
       
   157         if (!$this->_xpath) {
       
   158             $this->setXpath(new DOMXPath($this->getDomDocument()));
       
   159         }
       
   160         return $this->_xpath;
       
   161     }
       
   162 
       
   163     /**
       
   164      * Set the XPath query
       
   165      *
       
   166      * @param  DOMXPath $xpath
       
   167      * @return Zend_Feed_Reader_Entry_EntryAbstract
       
   168      */
       
   169     public function setXpath(DOMXPath $xpath)
       
   170     {
       
   171         $this->_xpath = $xpath;
       
   172         return $this;
       
   173     }
       
   174 
       
   175     /**
       
   176      * Get registered extensions
       
   177      *
       
   178      * @return array
       
   179      */
       
   180     public function getExtensions()
       
   181     {
       
   182         return $this->_extensions;
       
   183     }
       
   184 
       
   185     /**
       
   186      * Return an Extension object with the matching name (postfixed with _Entry)
       
   187      *
       
   188      * @param string $name
       
   189      * @return Zend_Feed_Reader_Extension_EntryAbstract
       
   190      */
       
   191     public function getExtension($name)
       
   192     {
       
   193         if (array_key_exists($name . '_Entry', $this->_extensions)) {
       
   194             return $this->_extensions[$name . '_Entry'];
       
   195         }
       
   196         return null;
       
   197     }
       
   198 
       
   199     /**
       
   200      * Method overloading: call given method on first extension implementing it
       
   201      *
       
   202      * @param  string $method
       
   203      * @param  array $args
       
   204      * @return mixed
       
   205      * @throws Zend_Feed_Exception if no extensions implements the method
       
   206      */
       
   207     public function __call($method, $args)
       
   208     {
       
   209         foreach ($this->_extensions as $extension) {
       
   210             if (method_exists($extension, $method)) {
       
   211                 return call_user_func_array(array($extension, $method), $args);
       
   212             }
       
   213         }
       
   214         require_once 'Zend/Feed/Exception.php';
       
   215         throw new Zend_Feed_Exception('Method: ' . $method
       
   216             . 'does not exist and could not be located on a registered Extension');
       
   217     }
       
   218 
       
   219     /**
       
   220      * Load extensions from Zend_Feed_Reader
       
   221      *
       
   222      * @return void
       
   223      */
       
   224     protected function _loadExtensions()
       
   225     {
       
   226         $all = Zend_Feed_Reader::getExtensions();
       
   227         $feed = $all['entry'];
       
   228         foreach ($feed as $extension) {
       
   229             if (in_array($extension, $all['core'])) {
       
   230                 continue;
       
   231             }
       
   232             $className = Zend_Feed_Reader::getPluginLoader()->getClassName($extension);
       
   233             $this->_extensions[$extension] = new $className(
       
   234                 $this->getElement(), $this->_entryKey, $this->_data['type']
       
   235             );
       
   236         }
       
   237     }
       
   238 }