* @copyright 2012-2013 Romanenko Sergey / Awilum * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ class Html { /** * Preferred order of attributes * * @var array */ public static $attribute_order = array ( 'action', 'method', 'type', 'id', 'name', 'value', 'href', 'src', 'width', 'height', 'cols', 'rows', 'size', 'maxlength', 'rel', 'media', 'accept-charset', 'accept', 'tabindex', 'accesskey', 'alt', 'title', 'class', 'style', 'selected', 'checked', 'readonly', 'disabled', ); /** * Protected constructor since this is a static class. * * @access protected */ protected function __construct() { // Nothing here } /** * Registers a custom macro. * * * * // Registering a Htmlk macro * Html::macro('my_element', function() { * return ''; * }); * * // Calling a custom Html macro * echo Html::my_element(); * * * // Registering a Html macro with parameters * Html::macro('my_element', function(id = '') { * return ''; * }); * * // Calling a custom Html macro with parameters * echo Html::my_element('monstra'); * * * * @param string $name Name * @param Closure $macro Macro */ public static function macro($name, $macro) { Html::$macros[$name] = $macro; } /** * Convert special characters to HTML entities. All untrusted content * should be passed through this method to prevent XSS injections. * * * echo Html::chars($username); * * * @param string $value String to convert * @param boolean $double_encode Encode existing entities * @return string */ public static function chars($value, $double_encode = true) { return htmlspecialchars((string) $value, ENT_QUOTES, 'utf-8', $double_encode); } /** * Compiles an array of HTML attributes into an attribute string. * Attributes will be sorted using Html::$attribute_order for consistency. * * * echo ''.$content.''; * * * @param array $attributes Attribute list * @return string */ public static function attributes(array $attributes = null) { if (empty($attributes)) return ''; // Init var $sorted = array(); foreach (Html::$attribute_order as $key) { if (isset($attributes[$key])) { // Add the attribute to the sorted list $sorted[$key] = $attributes[$key]; } } // Combine the sorted attributes $attributes = $sorted + $attributes; $compiled = ''; foreach ($attributes as $key => $value) { if ($value === NULL) { // Skip attributes that have NULL values continue; } if (is_int($key)) { // Assume non-associative keys are mirrored attributes $key = $value; } // Add the attribute value $compiled .= ' '.$key.'="'.Html::chars($value).'"'; } return $compiled; } /** * Create br tags * * * echo Html::br(2); * * * @param integer $num Count of line break tag * @return string */ public static function br($num = 1) { return str_repeat("
",(int) $num); } /** * Create   * * * echo Html::nbsp(2); * * * @param integer $num Count of   * @return string */ public static function nbsp($num = 1) { return str_repeat(" ", (int) $num); } /** * Create an arrow * * * echo Html::arrow('right'); * * * @param string $direction Arrow direction [up,down,left,right] * @param boolean $render If this option is true then render html object else return it * @return string */ public static function arrow($direction) { switch ($direction) { case "up": $output = ''; break; case "down": $output = ''; break; case "left": $output = ''; break; case "right": $output = ''; break; } return $output; } /** * Create HTML link anchor. * * * echo Html::anchor('About', 'http://sitename.com/about'); * * * @param string $title Anchor title * @param string $url Anchor url * @param array $attributes Anchor attributes * @uses Html::attributes * @return string */ public static function anchor($title, $url = null, array $attributes = null) { // Add link if ($url !== null) $attributes['href'] = $url; return ''.$title.''; } /** * Create HTML tag * * * echo Html::heading('Title', 1); * * * @param string $title Text * @param integer $h Number [1-6] * @param array $attributes Heading attributes * @uses Html::attributes * @return string */ public static function heading($title, $h = 1, array $attributes = null) { $output = ''.$title.''; return $output; } /** * Generate document type declarations * * * echo Html::doctype('html5'); * * * @param string $type Doctype to generated * @return mixed */ public static function doctype($type = 'html5') { $doctypes = array('xhtml11' => '', 'xhtml1-strict' => '', 'xhtml1-trans' => '', 'xhtml1-frame' => '', 'html5' => '', 'html4-strict' => '', 'html4-trans' => '', 'html4-frame' => ''); if (isset($doctypes[$type])) return $doctypes[$type]; else return false; } /** * Create image * * * echo Html::image('data/files/pic1.jpg'); * * * @param array $attributes Image attributes * @param string $file File * @uses Url::base * @return string */ public static function image($file, array $attributes = null) { if (strpos($file, '://') === FALSE) { $file = Url::base().'/'.$file; } // Add the image link $attributes['src'] = $file; $attributes['alt'] = (isset($attributes['alt'])) ? $attributes['alt'] : pathinfo($file, PATHINFO_FILENAME); return ''; } /** * Convert html to plain text * * * echo Html::toText('test'); * * * @param string $str String * @return string */ public static function toText($str) { return htmlspecialchars($str, ENT_QUOTES, 'utf-8'); } /** * Dynamically handle calls to custom macros. * * @param string $method * @param array $parameters * @return mixed */ public static function __callStatic($method, $parameters) { if (isset(Html::$macros[$method])) { return call_user_func_array(Html::$macros[$method], $parameters); } throw new RuntimeException("Method [$method] does not exist."); } }