From e70dccafd7db405de95b66cf0a8a94a4b5363ddc Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Wed, 30 Jan 2013 22:16:55 -0500 Subject: [PATCH] Added HTML class --- classes/Display/PHP.php | 8 ++- classes/Form.php | 2 + classes/HTML.php | 144 ++++++++++++++++++++++++++++++++++++++ jar.php | 151 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 299 insertions(+), 6 deletions(-) create mode 100644 classes/HTML.php diff --git a/classes/Display/PHP.php b/classes/Display/PHP.php index c113a10..8592313 100644 --- a/classes/Display/PHP.php +++ b/classes/Display/PHP.php @@ -9,7 +9,7 @@ * Redistribution of these files must retain the above copyright notice. * * @author Josh Sherman - * @copyright Copyright 2007-2012, Josh Sherman + * @copyright Copyright 2007-2013, Josh Sherman * @license http://www.opensource.org/licenses/mit-license.html * @package PICKLES * @link https://github.com/joshtronic/pickles @@ -71,11 +71,13 @@ class Display_PHP extends Display_Common $__fluid = $this->fluid; // Creates (possibly overwritten) objects - $form_class = (class_exists('CustomForm') ? 'CustomForm' : 'Form'); $dynamic_class = (class_exists('CustomDynamic') ? 'CustomDynamic' : 'Dynamic'); + $form_class = (class_exists('CustomForm') ? 'CustomForm' : 'Form'); + $html_class = (class_exists('CustomHTML') ? 'CustomHTML' : 'HTML'); - $__form = new $form_class(); $__dynamic = new $dynamic_class(); + $__form = new $form_class(); + $__html = new $html_class(); // Loads the template if ($this->parent_template != null) diff --git a/classes/Form.php b/classes/Form.php index 83aff6f..adfe7b1 100644 --- a/classes/Form.php +++ b/classes/Form.php @@ -20,6 +20,8 @@ * * This class contains methods for easily generating form elements. There is a * heavy focus on select boxes as they have the most overhead for a developer. + * + * @deprecated */ class Form extends Object { diff --git a/classes/HTML.php b/classes/HTML.php new file mode 100644 index 0000000..9366dcd --- /dev/null +++ b/classes/HTML.php @@ -0,0 +1,144 @@ + + * @copyright Copyright 2007-2013, Josh Sherman + * @license http://www.opensource.org/licenses/mit-license.html + * @package PICKLES + * @link https://github.com/joshtronic/pickles + */ + +/** + * HTML Class + * + * This class contains methods for easily generating HTML elements. + */ +class HTML extends Object +{ + private $self_closing = array('br', 'hr', 'img', 'input', 'link', 'meta'); + + public function __call($method, $arguments) + { + + $attributes = null; + $contents = null; + + if (isset($arguments[0])) + { + $attributes = $arguments[0]; + } + + if (isset($arguments[1])) + { + $contents = $arguments[1]; + } + + // ->inputType('name', $attributes); + if (preg_match('/^input/', $method)) + { + $type = strtolower(str_replace('input', '', $method)); + + switch ($type) + { + case 'datetimelocal': $type = 'datetime-local'; break; + case '': $type = 'text'; break; + } + + if (is_array($attributes)) + { + $attributes['type'] = $type; + } + else + { + $attributes = array('type' => $type); + } + + if (isset($attributes['label'], $attributes['name'])) + { + $label = $this->label(array('for' => $attributes['name']), $attributes['label']); + unset($attributes['label']); + + return $label . $this->div($this->input($attributes)); + } + } + + return $this->element($method, $attributes, $contents); + } + + // {{{ Get Instance + + /** + * Get Instance + * + * Gets an instance of the Form class + * + * @static + * @param string $class name of the class to get an instance of + * @return object instance of the class + */ + public static function getInstance($class = 'HTML') + { + return parent::getInstance($class); + } + + // }}} + + public function element($element) + { + $attributes = null; + $contents = null; + + foreach (func_get_args() as $key => $value) + { + if ($key && $key < 3) + { + if (is_array($value)) + { + $attributes = $value; + } + elseif ($value) + { + $contents = $value; + } + } + } + + $element = strtolower($element); + $html = '<' . $element; + + if ($attributes) + { + if (is_array($attributes)) + { + foreach ($attributes as $attribute => $value) + { + $html .= ' ' . $attribute . '="' . str_replace('"', '\"', $value) . '"'; + } + } + else + { + throw new Exception('Attributes must be an array.'); + } + } + + if ($contents || !in_array($element, $this->self_closing)) + { + $html .= '>' . $contents . ''; + } + else + { + $html .= ' />'; + } + + return $html; + } +} + +?> diff --git a/jar.php b/jar.php index f1afc4d..1d25fdd 100755 --- a/jar.php +++ b/jar.php @@ -2588,7 +2588,7 @@ class Display_JSON extends Display_Common * Redistribution of these files must retain the above copyright notice. * * @author Josh Sherman - * @copyright Copyright 2007-2012, Josh Sherman + * @copyright Copyright 2007-2013, Josh Sherman * @license http://www.opensource.org/licenses/mit-license.html * @package PICKLES * @link https://github.com/joshtronic/pickles @@ -2650,11 +2650,13 @@ class Display_PHP extends Display_Common $__fluid = $this->fluid; // Creates (possibly overwritten) objects - $form_class = (class_exists('CustomForm') ? 'CustomForm' : 'Form'); $dynamic_class = (class_exists('CustomDynamic') ? 'CustomDynamic' : 'Dynamic'); + $form_class = (class_exists('CustomForm') ? 'CustomForm' : 'Form'); + $html_class = (class_exists('CustomHTML') ? 'CustomHTML' : 'HTML'); - $__form = new $form_class(); $__dynamic = new $dynamic_class(); + $__form = new $form_class(); + $__html = new $html_class(); // Loads the template if ($this->parent_template != null) @@ -3489,6 +3491,8 @@ class File * * This class contains methods for easily generating form elements. There is a * heavy focus on select boxes as they have the most overhead for a developer. + * + * @deprecated */ class Form extends Object { @@ -4146,6 +4150,147 @@ class Form extends Object // }}} } +/** + * HTML Class File for PICKLES + * + * PHP version 5 + * + * Licensed under The MIT License + * Redistribution of these files must retain the above copyright notice. + * + * @author Josh Sherman + * @copyright Copyright 2007-2013, Josh Sherman + * @license http://www.opensource.org/licenses/mit-license.html + * @package PICKLES + * @link https://github.com/joshtronic/pickles + */ + +/** + * HTML Class + * + * This class contains methods for easily generating HTML elements. + */ +class HTML extends Object +{ + private $self_closing = array('br', 'hr', 'img', 'input', 'link', 'meta'); + + public function __call($method, $arguments) + { + + $attributes = null; + $contents = null; + + if (isset($arguments[0])) + { + $attributes = $arguments[0]; + } + + if (isset($arguments[1])) + { + $contents = $arguments[1]; + } + + // ->inputType('name', $attributes); + if (preg_match('/^input/', $method)) + { + $type = strtolower(str_replace('input', '', $method)); + + switch ($type) + { + case 'datetimelocal': $type = 'datetime-local'; break; + case '': $type = 'text'; break; + } + + if (is_array($attributes)) + { + $attributes['type'] = $type; + } + else + { + $attributes = array('type' => $type); + } + + if (isset($attributes['label'], $attributes['name'])) + { + $label = $this->label(array('for' => $attributes['name']), $attributes['label']); + unset($attributes['label']); + + return $label . $this->div($this->input($attributes)); + } + } + + return $this->element($method, $attributes, $contents); + } + + // {{{ Get Instance + + /** + * Get Instance + * + * Gets an instance of the Form class + * + * @static + * @param string $class name of the class to get an instance of + * @return object instance of the class + */ + public static function getInstance($class = 'HTML') + { + return parent::getInstance($class); + } + + // }}} + + public function element($element) + { + $attributes = null; + $contents = null; + + foreach (func_get_args() as $key => $value) + { + if ($key && $key < 3) + { + if (is_array($value)) + { + $attributes = $value; + } + elseif ($value) + { + $contents = $value; + } + } + } + + $element = strtolower($element); + $html = '<' . $element; + + if ($attributes) + { + if (is_array($attributes)) + { + foreach ($attributes as $attribute => $value) + { + $html .= ' ' . $attribute . '="' . str_replace('"', '\"', $value) . '"'; + } + } + else + { + throw new Exception('Attributes must be an array.'); + } + } + + if ($contents || !in_array($element, $this->self_closing)) + { + $html .= '>' . $contents . ''; + } + else + { + $html .= ' />'; + } + + return $html; + } +} + /** * Logging System for PICKLES *