Example #1
0
 /**
  * Parses a remote feed into an array.
  *
  * @param   string   remote feed URL
  * @param   integer  item limit to fetch
  * @return  array
  */
 public static function parse($feed, $limit = 0)
 {
     // Check if SimpleXML is installed
     if (!function_exists('simplexml_load_file')) {
         throw new Kohana_Exception('SimpleXML must be installed!');
     }
     // Make limit an integer
     $limit = (int) $limit;
     // Disable error reporting while opening the feed
     $ER = error_reporting(0);
     // Allow loading by filename or raw XML string
     $load = (is_file($feed) or validate::url($feed)) ? 'simplexml_load_file' : 'simplexml_load_string';
     // Load the feed
     $feed = $load($feed, 'SimpleXMLElement', LIBXML_NOCDATA);
     // Restore error reporting
     error_reporting($ER);
     // Feed could not be loaded
     if ($feed === FALSE) {
         return array();
     }
     // Detect the feed type. RSS 1.0/2.0 and Atom 1.0 are supported.
     $feed = isset($feed->channel) ? $feed->xpath('//item') : $feed->entry;
     $i = 0;
     $items = array();
     foreach ($feed as $item) {
         if ($limit > 0 and $i++ === $limit) {
             break;
         }
         $items[] = (array) $item;
     }
     return $items;
 }
Example #2
0
File: core.php Project: refo/kohana
	/**
	 * Class constructor. You should use the factory instead.
	 * @param string $element [optional] What to construct from. Could be some xml string, a file name, or a DOMNode
	 * @param string $root_node [optional] The root node name. To be specified if no driver are used.
	 * @return XML XML object instance
	 */
	public function __construct($element = NULL, $root_node = NULL)
	{
		// Create the initial DOMDocument
		$this->dom_doc = new DOMDocument(XML::$xml_version, Kohana::$charset);

		if ($root_node)
		{
			// If a root node is specified, overwrite the current_one
			$this->root_node = $root_node;
		}

		// Initialize the document with the given element
		if (is_string($element))
		{
			if (is_file($element) OR validate::url($element))
			{
				// Generate XML from a file
				$this->dom_doc->load($element);
			}
			else
			{
				// Generate XML from a string
				$this->dom_doc->loadXML($element);
			}
			// Node is the root node of the document, containing the whole tree
			$this->dom_node = $this->dom_doc->documentElement;
		}
		elseif ($element instanceof DOMNode)
		{
			// This is called from another XML instance ( through add_node, or else...)
			// Let's add that node to the new object node 
			$this->dom_node = $element;

			// And overwrite the document with that node's owner document
			$this->dom_doc = $this->dom_node->ownerDocument;
		}
		elseif ( ! is_null($this->root_node))
		{
			// Create the Root Element from the driver attributes
			if ($this->meta()->ns($this->root_node))
			{
				list($ns, $prefix) = $this->meta()->ns($this->root_node);
				
				$root_node_name = $prefix ? "$prefix:$this->root_node" : $this->root_node;
				
				// Create the root node in its prefixed namespace
				$root_node = $this->dom_doc->createElementNS($ns, $root_node_name);
			}
			else
			{
				// Create the root node
				$root_node = $this->dom_doc->createElement($this->root_node);
			}
			
			// Append the root node to the object DOMDocument, and set the resulting DOMNode as it's node
			$this->dom_node = $this->dom_doc->appendChild($root_node);
			
			// Add other attributes
			$this->add_attributes($this->dom_node);
		}
		else
		{
			throw new Kohana_Exception("You have to specify a root_node, either in your driver or in the constructor if you're not using any.");
		}
	}
Example #3
0
 /**
  * Accessor method for setting the default image if the supplied email address or rating return an empty result
  *
  * @param   string       url  the url of the image to use instead of the Gravatar
  * @return  self
  */
 public function default_image($url = NULL)
 {
     if ($url === NULL) {
         return $this->_config['default'];
     } else {
         if (validate::url($url)) {
             $this->_config['default'] = $url;
         } else {
             throw new Gravatar('The url : :url is improperly formatted', array(':url' => $url));
         }
     }
     return $this;
 }