123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- <?php namespace Laravel;
- class HTML {
-
- public static $macros = array();
-
- public static function macro($name, $macro)
- {
- static::$macros[$name] = $macro;
- }
-
- public static function entities($value)
- {
- return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false);
- }
-
- public static function decode($value)
- {
- return html_entity_decode($value, ENT_QUOTES, Config::get('application.encoding'));
- }
-
- public static function script($url, $attributes = array())
- {
- $url = URL::to_asset($url);
- return '<script src="'.$url.'"'.static::attributes($attributes).'></script>'.PHP_EOL;
- }
-
- public static function style($url, $attributes = array())
- {
- $defaults = array('media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet');
- $attributes = $attributes + $defaults;
- $url = URL::to_asset($url);
- return '<link href="'.$url.'"'.static::attributes($attributes).'>'.PHP_EOL;
- }
-
- public static function span($value, $attributes = array())
- {
- return '<span'.static::attributes($attributes).'>'.static::entities($value).'</span>';
- }
-
- public static function link($url, $title, $attributes = array(), $https = false)
- {
- $url = URL::to($url, $https);
- return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
- }
-
- public static function link_to_secure($url, $title, $attributes = array())
- {
- return static::link($url, $title, $attributes, true);
- }
-
- public static function link_to_asset($url, $title, $attributes = array(), $https = null)
- {
- $url = URL::to_asset($url, $https);
- return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
- }
-
- public static function link_to_secure_asset($url, $title, $attributes = array())
- {
- return static::link_to_asset($url, $title, $attributes, true);
- }
-
- public static function link_to_route($name, $title, $parameters = array(), $attributes = array())
- {
- return static::link(URL::to_route($name, $parameters), $title, $attributes);
- }
-
- public static function link_to_action($action, $title, $parameters = array(), $attributes = array())
- {
- return static::link(URL::to_action($action, $parameters), $title, $attributes);
- }
-
- public static function mailto($email, $title = null, $attributes = array())
- {
- $email = static::email($email);
- if (is_null($title)) $title = $email;
- $email = 'mailto:'.$email;
- return '<a href="'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
- }
-
- public static function email($email)
- {
- return str_replace('@', '@', static::obfuscate($email));
- }
-
- public static function image($url, $alt = '', $attributes = array())
- {
- $attributes['alt'] = $alt;
- return '<img src="'.URL::to_asset($url).'"'.static::attributes($attributes).'>';
- }
-
- public static function ol($list, $attributes = array())
- {
- return static::listing('ol', $list, $attributes);
- }
-
- public static function ul($list, $attributes = array())
- {
- return static::listing('ul', $list, $attributes);
- }
-
- private static function listing($type, $list, $attributes = array())
- {
- $html = '';
- if (count($list) == 0) return $html;
- foreach ($list as $key => $value)
- {
-
-
-
- if (is_array($value))
- {
- $html .= static::listing($type, $value);
- }
- else
- {
- $html .= '<li>'.static::entities($value).'</li>';
- }
- }
- return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
- }
-
- public static function attributes($attributes)
- {
- $html = array();
- foreach ((array) $attributes as $key => $value)
- {
-
-
-
- if (is_numeric($key)) $key = $value;
- if ( ! is_null($value))
- {
- $html[] = $key.'="'.static::entities($value).'"';
- }
- }
- return (count($html) > 0) ? ' '.implode(' ', $html) : '';
- }
-
- protected static function obfuscate($value)
- {
- $safe = '';
- foreach (str_split($value) as $letter)
- {
-
-
-
- switch (rand(1, 3))
- {
- case 1:
- $safe .= '&#'.ord($letter).';';
- break;
- case 2:
- $safe .= '&#x'.dechex(ord($letter)).';';
- break;
- case 3:
- $safe .= $letter;
- }
- }
- return $safe;
- }
-
- public static function __callStatic($method, $parameters)
- {
- if (isset(static::$macros[$method]))
- {
- return call_user_func_array(static::$macros[$method], $parameters);
- }
-
- throw new \Exception("Method [$method] does not exist.");
- }
- }
|