Location: PHPKode > projects > Modularized Information Environment > mie/includes/xml.inc
<?php
/**
 * @file includes/xml.inc
 * @brief XML Library.
 * @author Kenneth Smith <hide@address.com>
 *
 * Defines the XML tag class, which is necessary for creating XML document
 * elements.
 *
 * Modularized Information Environment (MIE)
 * Copyright (C) 2005-2006 by Kenneth Smith. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 */

defined('VALIE_MIE') || die(_('Direct access not allowed'));
defined('XML_INC') && exit;
define('XML_INC', true);

/**
 * @brief XML Tag Class
 *
 * Provides TAG objects used to render XML elements.
 */
class TAG {

	/** 
	 * @brief XML Data
	 * Contains XML element data.
	 * $xml['element'] = XML element name,
	 * $xml['attribute'] = Attribute array,
	 * $xml['content'] = Content array
	 */
	public $xml;

	/**
	 * @brief Object Constructor
	 * Constructs the TAG object.
	 * @param $_element
	 *   XML element name
	 * @param $_attribute
	 *   Attribute array
	 * @param $_content
	 *   Content to append
	 */
	function __construct($_element = null, $_attribute = null, $_content = null) {
		if(is_string($_element)) {
			$this->xml['element'] = $_element;
		}
		if(is_array($_attribute)) {
			$this->xml['attribute'] = $_attribute;
		}
		if(!is_null($_content)) {
			$this->append($_content);
		}
	}

	/**
	 * @brief Class's Set Interface
	 * Part of the class interface.
	 * @param $_name
	 *   The name of the value to set.
	 * @param $_value
	 *   The value to set.
	 */
	function __set($_name, $_value) {
		if(isset($this->xml['content'][$_name]) || method_exists($_value, 'render')) {
			$this->append($_value, $_name);
		}
		else {
			$this->xml['attribute'][$_name] = $_value;
		}
	}

	/**
	 * @brief Class's Get Interface
	 * Part of the class interface.
	 * @param $_name
	 *   The name of the value to get.
	 * @return
	 *   Returns value specified by $_name.
	 */
	function __get($_name) {
		if(isset($this->xml['content'][$_name])) {
			return $this->xml['content'][$_name];
		}
		else {
			return @$this->xml['attribute'][$_name];
		}
	}

	/**
	 * @brief Append Node
	 * Appends a content to this tag.
	 * @param $_content
	 *   Content node to append.
	 * @param $_index
	 *   Optional index for later reference.
	 * @return
	 *   Does not return a value
	 */
	function append($_content = null, $_index = null) {
		if(is_null($_content)) {
			return; // Nothing to append
		}
		if(is_null($_index)) {
			$this->xml['content'][] = &$_content;
		}
		else {
			if(isset($this->xml['content'][$_index])) {
				if(method_exists($this->xml['content'][$_index], 'append')) {
					$this->xml['content'][$_index]->append($_content, $_index);
				}
				elseif(is_array($this->xml['content'][$_index])) {
					$this->xml['content'][$_index][] = &$_content;
				}
				else {
					$this->xml['content'][$_index] = array($this->xml['content'][$_index]);
					$this->xml['content'][$_index][] = &$_content;
				}
			}
			else {
				$this->xml['content'][$_index] = &$_content;
			}
		}
	}

	/**
	 * @brief Draw Content
	 * Attempts to draw content ($_content) based on the content's type. If the
	 * content is an array, it is recursively (draw)n. If the content is an object
	 * with a 'render' method, that method is called. If the content is a string,
	 * the string is printed.
	 * @param $_content
	 *   Content to be drawn
	 * @return
	 *   Does not return a value
	 */
	function draw($_content){
		if(is_array($_content)) {
			foreach($_content as $c) {
				$this->draw($c);
			}
		}
		elseif(is_string($_content) || is_numeric($_content)) {
			print $_content;
		}
		elseif(method_exists($_content, 'render')) {
			$_content->render();
		}
	}

	/**
	 * @brief Render Object
	 * Causes the TAG object to render itself.
	 * @return
	 *   Does not return a value
	 */
	function render() {
		if(isset($this->xml['element']) && is_string($this->xml['element'])) {
			print "<{$this->xml['element']}";
			if(isset($this->xml['attribute'])) {
				foreach((array)$this->xml['attribute'] as $k => $v) {
					if(!is_null($v)) {
						print " $k=\"" . str_replace('"', '&quot', $v) . '"';
					}
				}
			}
			if(isset($this->xml['content'])) {
				print '>';
				$this->draw($this->xml['content']);
				print "</{$this->xml['element']}>";
			}
			else {
				print '/>';
			}
		}
		else {
			$this->draw($this->xml['content']);
		}
	}
}

?>
Return current item: Modularized Information Environment