* @license PHP License
* @package WB
* @subpackage base
*/
/**
* Insert HTML
*
* Basic interface to insert some HTML
*
* @version 0.1.1
* @package WB
* @subpackage html
*/
class WBHtml extends WBStdClass
{
const AREA_HEAD = 1;
const AREA_FOOT = 2;
const AREA_JS_ONLOAD = 4;
/**
* actual HTML
* @var array
*/
private static $html = array();
/**
* list of callbacks to finalize html
*
* @see get()
* @see setFinalizer()
* @var array
*/
private static $finalize = array();
/**
* constructor
*
* Avoid creating object
*/
private function __construct()
{
}
/**
* static constructor
*
* Setupd internal HTML buffer
*/
public static function staticConstruct()
{
self::$html[self::AREA_HEAD] = array();
self::$html[self::AREA_FOOT] = array();
self::$html[self::AREA_JS_ONLOAD] = array();
}
/**
* add HTML
*
* add string that will be included in HTML at the end
*
* @param string $html
* @param in $area
*/
public static function add($html, $area = self::AREA_HEAD)
{
self::checkArea($area);
self::$html[$area][] = $html;
}
/**
* fetch MTML
*
* Receive HTML string of specified area
*
* @param int $area
* @return string
*/
public static function get($area = self::AREA_HEAD)
{
self::checkArea($area);
foreach (self::$finalize as $callback) {
call_user_func($callback);
}
// prevent second call
self::$finalize = array();
return implode("\n", self::$html[$area]);
}
/**
* set callback for get
*
* @param $callback
*/
public static function addFinalizer($callback)
{
self::$finalize[] = $callback;
}
/**
* Check whether area parameter is valid
*
* @throws WBException_Parameter
* @param $area
*/
protected static function checkArea($area)
{
if (!isset(self::$html[$area])) {
WBClass::load('WBException_Argument');
throw new WBException_Argument('Invalid area: ' . $area, 1, __CLASS__);
}
}
}
?>