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

/**
 * This is class renders standard lists and their values
 *
 * @package    OSB
 * @subpackage Utilities
 * @category   Helpers
 * @author     Deon George
 * @copyright  (c) 2010 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
abstract class StaticList {
	// This is our list of items that will be rendered
	protected $list = array();

	/**
	 * Each static list type must provide the table function that contains
	 * the table of list and values.
	 */
	abstract protected function table();

	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__));
	}

	/**
	 * Display a static name for a value
	 *
	 * @param key $id value to render
	 * @see _display()
	 */
	public static function display($value) {
		return static::_display($value);
	}

	// Due to static scope, sometimes we need to call this function from the child class.
	protected static function _display($id) {
		$table = static::factory()->table();

		if (! $table)
			return 'No Table';
		elseif (empty($table[$id]))
			return sprintf('No Value (%s)',$id);
		else
			return $table[$id];
	}

	/**
	 * Lists our available keys
	 */
	public static function keys() {
		return array_keys(static::factory()->table());
	}

	/**
	 * Renders form input
	 *
	 * @param string Form name to render
	 * @param string Default value to populate in the Form input.
	 */
	public static function form($name,$default='',$addblank=FALSE) {
		$table = static::factory()->table();

		if ($addblank)
			$table = array_merge(array(''=>'&nbsp;'),$table);

		return Form::Select($name,$table,$default);
	}
}
?>