<?php defined('SYSPATH') or die('No direct access allowed.');
/**
 * This class extends the Kohana XML
 *
 * @package    OSB
 * @subpackage XML
 * @category   XML
 * @author     Deon George
 * @copyright  (c) 2010 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
class XML extends XML_Core {
	/**
	 * Collapse an XML object into a simple array
	 */
	public function collapse(XML $xml=NULL) {
		if (is_null($xml))
			$xml = $this;
		$result = array();

		foreach ($xml->as_array() as $j=>$k) {
			$v = $xml->$j->value();

			if (count($k) > 1) {
				foreach ($xml->get($j,1) as $k)
					if (isset($k['name'][0]))
						$result[$j][$k['name'][0]] = (isset($k['value'][0]) ? $k['value'][0] : '');
					else
						$result[$j][] = $k;

			} elseif (! is_null($v))
				$result[$j] = $v;
			else {
				$result[$j] = $this->collapse($xml->$j);
			}
		}

		if (array_key_exists('name',$result) AND array_key_exists('value',$result))
			$result = array($result['name']=>$result['value']);

		return $result;
	}
}
?>
