<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class is the base used for common static methods that are used
 * for rendering.
 *
 * @package    lnApp
 * @category   lnApp/Helpers
 * @author     Deon George
 * @copyright  (c) 2009-2013 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
abstract class lnApp_HTMLRender {
	protected static $_media_path = 'default/media';
	protected static $_required_keys = array();
	protected static $_unique_vals = array();

	public function __construct() {
		if (! isset(static::$_data))
			throw new Kohana_Exception(':class is missing important static variables',array(':class'=>get_called_class()));
	}

	/**
	 * Add an item to be rendered
	 *
	 * @param array Item to be added
	 */
	public static function add($item,$prepend=FALSE) {
		foreach (static::$_required_keys as $key)
			if (! isset($item[$key]))
				throw new Kohana_Exception('Missing key :key for image',array(':key'=>$key));

		// Check for unique keys
		if (static::$_unique_vals)
			foreach (static::$_unique_vals as $v=>$u)
				foreach (static::$_data as $d)
					if (isset($d[$u]) && $d['data'] == $item['data'])
						return;

		if ($prepend)
			array_unshift(static::$_data,$item);
		else
			array_push(static::$_data,$item);
	}

	/**
	 * Set the space used between rendering output
	 */
	public static function setSpacer($spacer) {
		static::$_spacer = $spacer;
	}

	/**
	 * Set the Kohana Media Path, used to determine where to find additional
	 * HTML content required for rendering.
	 */
	public static function setMediaPath($path) {
		static::$_media_path = $path;
	}

	/**
	 * Factory instance method must be declared by the child class
	 */
	public static function factory() {
		throw new Kohana_Exception(':class is calling :method, when it should have its own method',
			array(':class'=>get_called_class(),':method'=>__METHOD__));
	}

	/**
	 * Return the HTML to render the header images
	 */
	public function __toString() {
		try {
			return static::render();
		}

		// Display the exception message
		catch (Exception $e) {
			Kohana_Exception::handler($e);
		}
	}

	/**
	 * Rendering must be declared by the child class
	 */
	protected function render() {
		throw new Kohana_Exception(':class is calling :method, when it should have its own method',
			array(':class'=>get_called_class(),':method'=>__METHOD__));
	}
}
?>