Source for file factory.php

Documentation is available at factory.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Feed
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Feed factory class.
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Feed
  17.  * @since       12.3
  18.  */
  19. {
  20.     /**
  21.      * @var    array  The list of registered parser classes for feeds.
  22.      * @since  12.3
  23.      */
  24.     protected $parsers = array('rss' => 'JFeedParserRss''feed' => 'JFeedParserAtom');
  25.  
  26.     /**
  27.      * Method to load a URI into the feed reader for parsing.
  28.      *
  29.      * @param   string  $uri  The URI of the feed to load. Idn uris must be passed already converted to punycode.
  30.      *
  31.      * @return  JFeedReader 
  32.      *
  33.      * @since   12.3
  34.      * @throws  InvalidArgumentException
  35.      * @throws  RuntimeException
  36.      */
  37.     public function getFeed($uri)
  38.     {
  39.         // Create the XMLReader object.
  40.         $reader new XMLReader;
  41.  
  42.         // Open the URI within the stream reader.
  43.         if (!$reader->open($urinullLIBXML_NOERROR LIBXML_ERR_NONE LIBXML_NOWARNING))
  44.         {
  45.             throw new RuntimeException('Unable to open the feed.');
  46.         }
  47.  
  48.         try
  49.         {
  50.             // Skip ahead to the root node.
  51.             do
  52.             {
  53.                 $reader->read();
  54.             }
  55.             while ($reader->nodeType !== XMLReader::ELEMENT);
  56.         }
  57.         catch (Exception $e)
  58.         {
  59.             throw new RuntimeException('Error reading feed.');
  60.         }
  61.  
  62.         // Setup the appopriate feed parser for the feed.
  63.         $parser $this->_fetchFeedParser($reader->name$reader);
  64.  
  65.         return $parser->parse();
  66.     }
  67.  
  68.     /**
  69.      * Method to register a JFeedParser class for a given root tag name.
  70.      *
  71.      * @param   string   $tagName    The root tag name for which to register the parser class.
  72.      * @param   string   $className  The JFeedParser class name to register for a root tag name.
  73.      * @param   boolean  $overwrite  True to overwrite the parser class if one is already registered.
  74.      *
  75.      * @return  JFeedFactory 
  76.      *
  77.      * @since   12.3
  78.      * @throws  InvalidArgumentException
  79.      */
  80.     public function registerParser($tagName$className$overwrite false)
  81.     {
  82.         // Verify that the class exists.
  83.         if (!class_exists($className))
  84.         {
  85.             throw new InvalidArgumentException('The feed parser class ' $className ' does not exist.');
  86.         }
  87.  
  88.         // Validate that the tag name is valid.
  89.         if (!preg_match('/\A(?!XML)[a-z][\w0-9-]*/i'$tagName))
  90.         {
  91.             throw new InvalidArgumentException('The tag name ' $tagName ' is not valid.');
  92.         }
  93.  
  94.         // Register the given parser class for the tag name if nothing registered or the overwrite flag set.
  95.         if (empty($this->parsers[$tagName]|| (bool) $overwrite)
  96.         {
  97.             $this->parsers[(string) $tagName= (string) $className;
  98.         }
  99.  
  100.         return $this;
  101.     }
  102.  
  103.     /**
  104.      * Method to return a new JFeedParser object based on the registered parsers and a given type.
  105.      *
  106.      * @param   string     $type    The name of parser to return.
  107.      * @param   XMLReader  $reader  The XMLReader instance for the feed.
  108.      *
  109.      * @return  JFeedParser 
  110.      *
  111.      * @since   12.3
  112.      * @throws  LogicException
  113.      */
  114.     private function _fetchFeedParser($typeXMLReader $reader)
  115.     {
  116.         // Look for a registered parser for the feed type.
  117.         if (empty($this->parsers[$type]))
  118.         {
  119.             throw new LogicException('No registered feed parser for type ' $type '.');
  120.         }
  121.  
  122.         return new $this->parsers[$type]($reader);
  123.     }
  124. }

Documentation generated on Tue, 19 Nov 2013 15:02:47 +0100 by phpDocumentor 1.4.3