diff --git a/monstra/helpers/form.php b/monstra/helpers/form.php index 1f473a5..9192343 100644 --- a/monstra/helpers/form.php +++ b/monstra/helpers/form.php @@ -20,6 +20,14 @@ class Form { + + + /** + * The registered custom macros. + * + * @var array + */ + public static $macros = array(); /** @@ -30,6 +38,38 @@ protected function __construct() { // Nothing here } + + + /** + * Registers a custom macro. + * + * + * + * // Registering a Form macro + * Form::macro('my_field', function() { + * return ''; + * }); + * + * // Calling a custom Form macro + * echo Form::my_field(); + * + * + * // Registering a Form macro with parameters + * Form::macro('my_field', function($value = '') { + * return ''; + * }); + * + * // Calling a custom Form macro with parameters + * echo Form::my_field('Monstra'); + * + * + * + * @param string $name Name + * @param Closure $macro Macro + */ + public static function macro($name, $macro) { + Form::$macros[$name] = $macro; + } /** @@ -360,4 +400,21 @@ return ''; } + + /** + * 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."); + } + } \ No newline at end of file