Skip to content

Commit

Permalink
Form Helper: Custom Macros - added
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Oct 3, 2012
1 parent 7df3ad9 commit 727207f
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions monstra/helpers/form.php
Expand Up @@ -20,6 +20,14 @@


class Form {


/**
* The registered custom macros.
*
* @var array
*/
public static $macros = array();


/**
Expand All @@ -30,6 +38,38 @@ class Form {
protected function __construct() {
// Nothing here
}


/**
* Registers a custom macro.
*
* <code>
*
* // Registering a Form macro
* Form::macro('my_field', function() {
* return '<input type="text" name="my_field">';
* });
*
* // Calling a custom Form macro
* echo Form::my_field();
*
*
* // Registering a Form macro with parameters
* Form::macro('my_field', function($value = '') {
* return '<input type="text" name="my_field" value="'.$value.'">';
* });
*
* // Calling a custom Form macro with parameters
* echo Form::my_field('Monstra');
*
* </code>
*
* @param string $name Name
* @param Closure $macro Macro
*/
public static function macro($name, $macro) {
Form::$macros[$name] = $macro;
}


/**
Expand Down Expand Up @@ -360,4 +400,21 @@ public static function close() {
return '</form>';
}


/**
* Dynamically handle calls to custom macros.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic($method, $parameters) {

if (isset(Form::$macros[$method])) {
return call_user_func_array(Form::$macros[$method], $parameters);
}

throw new RuntimeException("Method [$method] does not exist.");
}

}

0 comments on commit 727207f

Please sign in to comment.