Added macro support to HTML class to match Form class.

This commit is contained in:
Taylor Otwell 2012-03-22 15:06:36 -05:00
parent 0455c10ae0
commit e942c2032f
1 changed files with 38 additions and 3 deletions

View File

@ -2,6 +2,25 @@
class HTML { class HTML {
/**
* The registered custom macros.
*
* @var array
*/
public static $macros = array();
/**
* Registers a custom macro.
*
* @param string $name
* @param Closure $input
* @return void
*/
public static function macro($name, $macro)
{
static::$macros[$name] = $macro;
}
/** /**
* Convert HTML characters to entities. * Convert HTML characters to entities.
* *
@ -319,7 +338,7 @@ public static function attributes($attributes)
{ {
// For numeric keys, we will assume that the key and the value are the // For numeric keys, we will assume that the key and the value are the
// same, as this will conver HTML attributes such as "required" that // same, as this will conver HTML attributes such as "required" that
// may be specified as required="required". // may be specified as required="required", etc.
if (is_numeric($key)) $key = $value; if (is_numeric($key)) $key = $value;
if ( ! is_null($value)) if ( ! is_null($value))
@ -345,8 +364,7 @@ protected static function obfuscate($value)
{ {
// To properly obfuscate the value, we will randomly convert each // To properly obfuscate the value, we will randomly convert each
// letter to its entity or hexadecimal representation, keeping a // letter to its entity or hexadecimal representation, keeping a
// bot from sniffing the randomly obfuscated letters from the // bot from sniffing the randomly obfuscated letters.
// page and guarding against e-mail harvesting.
switch (rand(1, 3)) switch (rand(1, 3))
{ {
case 1: case 1:
@ -365,4 +383,21 @@ protected static function obfuscate($value)
return $safe; return $safe;
} }
/**
* Dynamically handle calls to custom macros.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic($method, $parameters)
{
if (isset(static::$macros[$method]))
{
return call_user_func_array(static::$macros[$method], $parameters);
}
throw new \Exception("Method [$method] does not exist.");
}
} }