Created
November 15, 2011 02:06
-
-
Save LouisLandry/1365908 to your computer and use it in GitHub Desktop.
JFeedReader concept.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package Joomla.Platform | |
* @subpackage Feed | |
* | |
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE | |
*/ | |
defined('JPATH_PLATFORM') or die(); | |
jimport('joomla.utilities.xmlelement'); | |
/** | |
* Feed Reader class. | |
* | |
* @package Joomla.Platform | |
* @subpackage Feed | |
* @since 11.4 | |
*/ | |
class JFeedReader | |
{ | |
/** | |
* @var integer | |
* @since 11.4 | |
*/ | |
const RSS = 1; | |
/** | |
* @var integer | |
* @since 11.4 | |
*/ | |
const ATOM = 2; | |
/** | |
* @var JHttp The HTTP client object for requesting feeds. | |
* @since 11.4 | |
*/ | |
protected $http; | |
/** | |
* @var DOMDocument | |
* @since 11.4 | |
*/ | |
protected $node; | |
/** | |
* @var XMLReader | |
* @since 11.4 | |
*/ | |
protected $reader; | |
/** | |
* @var integer The feed format. | |
* @since 11.4 | |
*/ | |
protected $format; | |
/** | |
* @var string The feed format version. | |
* @since 11.4 | |
*/ | |
protected $version; | |
/** | |
* Constructor. | |
* | |
* @param JHttp $http The HTTP client object. | |
* | |
* @since 11.4 | |
*/ | |
public function __construct(JHttp $http = null) | |
{ | |
$this->http = isset($http) ? $http : new JHttp; | |
$this->node = new DOMDocument; | |
$this->reader = new XMLReader; | |
} | |
/** | |
* Method to open a file as a feed. | |
* | |
* @param string $uri The URI of the feed to open. | |
* | |
* @return JFeed | |
* | |
* @since 11.4 | |
*/ | |
public function loadFile($uri) | |
{ | |
// Make sure the file exists. | |
if (!file_exists($uri)) | |
{ | |
throw new InvalidArgumentException('The file ' . $uri . ' does not exist.'); | |
} | |
// Open the URI within the stream reader. | |
if (!$this->reader->open($uri)) | |
{ | |
throw new RuntimeException('Unable to open the feed.'); | |
} | |
// Read the root node from the feed. | |
if (!$this->reader->read()) | |
{ | |
throw new RuntimeException('Unable to read the root node of the feed.'); | |
} | |
return $this; | |
} | |
/** | |
* Method to detect the feed type and version. | |
* | |
* @return void | |
* | |
* @since 11.4 | |
*/ | |
protected function detectFormat() | |
{ | |
if (strtolower($this->reader->name) == 'rss') | |
{ | |
$this->format = self::RSS; | |
$this->version = $this->reader->getAttribute('version'); | |
} | |
elseif (strtolower($this->reader->name) == 'feed') | |
{ | |
$this->format = self::ATOM; | |
$this->version = ($this->reader->getAttribute('version') == '0.3') ? '0.3' : '1.0'; | |
} | |
} | |
/** | |
* Method to expand the current reader node into a SimpleXML node for more detailed reading | |
* and manipulation. | |
* | |
* @return SimpleXMLElement | |
* | |
* @since 11.4 | |
*/ | |
protected function expandToSimpleXml() | |
{ | |
$el = simplexml_import_dom($this->node->importNode($this->reader->expand(), true), 'JXMLElement'); | |
if (!($el instanceof JXMLElement)) | |
{ | |
throw new RuntimeException('Unable to expand node to SimpleXML element.'); | |
} | |
return $el; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment