From 617756d71593d06e059d52af5b476d3d193b152f Mon Sep 17 00:00:00 2001 From: Anahkiasen Date: Tue, 2 Apr 2013 14:28:04 +0200 Subject: [PATCH] Add custom HTML macros to HtmlBuilder --- HtmlBuilder.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/HtmlBuilder.php b/HtmlBuilder.php index 5929c97..d98f3c9 100644 --- a/HtmlBuilder.php +++ b/HtmlBuilder.php @@ -11,6 +11,13 @@ class HtmlBuilder { */ protected $url; + /** + * The registered html macros. + * + * @var array + */ + protected $macros; + /** * Create a new HTML builder instance. * @@ -22,6 +29,18 @@ class HtmlBuilder { $this->url = $url; } + /** + * Register a custom HTML macro. + * + * @param string $name + * @param callable $macro + * @return void + */ + public function macro($name, $macro) + { + $this->macros[$name] = $macro; + } + /** * Convert an HTML string to entities. * @@ -275,4 +294,21 @@ class HtmlBuilder { if ( ! is_null($value)) return $key.'="'.e($value).'"'; } + /** + * Dynamically handle calls to the html class. + * + * @param string $method + * @param array $parameters + * @return mixed + */ + public function __call($method, $parameters) + { + if (isset($this->macros[$method])) + { + return call_user_func_array($this->macros[$method], $parameters); + } + + throw new \BadMethodCallException("Method {$method} does not exist."); + } + }