PageRenderTime 113ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel/html.php

https://gitlab.com/waltspence/gtd-pad
PHP | 405 lines | 139 code | 45 blank | 221 comment | 8 complexity | e9380ebe805a1eb10894c015c8e79985 MD5 | raw file
  1. <?php namespace Laravel;
  2. class HTML {
  3. /**
  4. * The registered custom macros.
  5. *
  6. * @var array
  7. */
  8. public static $macros = array();
  9. /**
  10. * Registers a custom macro.
  11. *
  12. * @param string $name
  13. * @param Closure $input
  14. * @return void
  15. */
  16. public static function macro($name, $macro)
  17. {
  18. static::$macros[$name] = $macro;
  19. }
  20. /**
  21. * Convert HTML characters to entities.
  22. *
  23. * The encoding specified in the application configuration file will be used.
  24. *
  25. * @param string $value
  26. * @return string
  27. */
  28. public static function entities($value)
  29. {
  30. return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false);
  31. }
  32. /**
  33. * Convert entities to HTML characters.
  34. *
  35. * @param string $value
  36. * @return string
  37. */
  38. public static function decode($value)
  39. {
  40. return html_entity_decode($value, ENT_QUOTES, Config::get('application.encoding'));
  41. }
  42. /**
  43. * Generate a link to a JavaScript file.
  44. *
  45. * <code>
  46. * // Generate a link to a JavaScript file
  47. * echo HTML::script('js/jquery.js');
  48. *
  49. * // Generate a link to a JavaScript file and add some attributes
  50. * echo HTML::script('js/jquery.js', array('defer'));
  51. * </code>
  52. *
  53. * @param string $url
  54. * @param array $attributes
  55. * @return string
  56. */
  57. public static function script($url, $attributes = array())
  58. {
  59. $url = URL::to_asset($url);
  60. return '<script src="'.$url.'"'.static::attributes($attributes).'></script>'.PHP_EOL;
  61. }
  62. /**
  63. * Generate a link to a CSS file.
  64. *
  65. * If no media type is selected, "all" will be used.
  66. *
  67. * <code>
  68. * // Generate a link to a CSS file
  69. * echo HTML::style('css/common.css');
  70. *
  71. * // Generate a link to a CSS file and add some attributes
  72. * echo HTML::style('css/common.css', array('media' => 'print'));
  73. * </code>
  74. *
  75. * @param string $url
  76. * @param array $attributes
  77. * @return string
  78. */
  79. public static function style($url, $attributes = array())
  80. {
  81. $defaults = array('media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet');
  82. $attributes = $attributes + $defaults;
  83. $url = URL::to_asset($url);
  84. return '<link href="'.$url.'"'.static::attributes($attributes).'>'.PHP_EOL;
  85. }
  86. /**
  87. * Generate a HTML span.
  88. *
  89. * @param string $value
  90. * @param array $attributes
  91. * @return string
  92. */
  93. public static function span($value, $attributes = array())
  94. {
  95. return '<span'.static::attributes($attributes).'>'.static::entities($value).'</span>';
  96. }
  97. /**
  98. * Generate a HTML link.
  99. *
  100. * <code>
  101. * // Generate a link to a location within the application
  102. * echo HTML::link('user/profile', 'User Profile');
  103. *
  104. * // Generate a link to a location outside of the application
  105. * echo HTML::link('http://google.com', 'Google');
  106. * </code>
  107. *
  108. * @param string $url
  109. * @param string $title
  110. * @param array $attributes
  111. * @param bool $https
  112. * @return string
  113. */
  114. public static function link($url, $title, $attributes = array(), $https = false)
  115. {
  116. $url = URL::to($url, $https);
  117. return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  118. }
  119. /**
  120. * Generate a HTTPS HTML link.
  121. *
  122. * @param string $url
  123. * @param string $title
  124. * @param array $attributes
  125. * @return string
  126. */
  127. public static function link_to_secure($url, $title, $attributes = array())
  128. {
  129. return static::link($url, $title, $attributes, true);
  130. }
  131. /**
  132. * Generate an HTML link to an asset.
  133. *
  134. * The application index page will not be added to asset links.
  135. *
  136. * @param string $url
  137. * @param string $title
  138. * @param array $attributes
  139. * @param bool $https
  140. * @return string
  141. */
  142. public static function link_to_asset($url, $title, $attributes = array(), $https = null)
  143. {
  144. $url = URL::to_asset($url, $https);
  145. return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  146. }
  147. /**
  148. * Generate an HTTPS HTML link to an asset.
  149. *
  150. * @param string $url
  151. * @param string $title
  152. * @param array $attributes
  153. * @return string
  154. */
  155. public static function link_to_secure_asset($url, $title, $attributes = array())
  156. {
  157. return static::link_to_asset($url, $title, $attributes, true);
  158. }
  159. /**
  160. * Generate an HTML link to a route.
  161. *
  162. * An array of parameters may be specified to fill in URI segment wildcards.
  163. *
  164. * <code>
  165. * // Generate a link to the "profile" named route
  166. * echo HTML::link_to_route('profile', 'Profile');
  167. *
  168. * // Generate a link to the "profile" route and add some parameters
  169. * echo HTML::link_to_route('profile', 'Profile', array('taylor'));
  170. * </code>
  171. *
  172. * @param string $name
  173. * @param string $title
  174. * @param array $parameters
  175. * @param array $attributes
  176. * @return string
  177. */
  178. public static function link_to_route($name, $title, $parameters = array(), $attributes = array())
  179. {
  180. return static::link(URL::to_route($name, $parameters), $title, $attributes);
  181. }
  182. /**
  183. * Generate an HTML link to a controller action.
  184. *
  185. * An array of parameters may be specified to fill in URI segment wildcards.
  186. *
  187. * <code>
  188. * // Generate a link to the "home@index" action
  189. * echo HTML::link_to_action('home@index', 'Home');
  190. *
  191. * // Generate a link to the "user@profile" route and add some parameters
  192. * echo HTML::link_to_action('user@profile', 'Profile', array('taylor'));
  193. * </code>
  194. *
  195. * @param string $action
  196. * @param string $title
  197. * @param array $parameters
  198. * @param array $attributes
  199. * @return string
  200. */
  201. public static function link_to_action($action, $title, $parameters = array(), $attributes = array())
  202. {
  203. return static::link(URL::to_action($action, $parameters), $title, $attributes);
  204. }
  205. /**
  206. * Generate an HTML mailto link.
  207. *
  208. * The E-Mail address will be obfuscated to protect it from spam bots.
  209. *
  210. * @param string $email
  211. * @param string $title
  212. * @param array $attributes
  213. * @return string
  214. */
  215. public static function mailto($email, $title = null, $attributes = array())
  216. {
  217. $email = static::email($email);
  218. if (is_null($title)) $title = $email;
  219. $email = '&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email;
  220. return '<a href="'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
  221. }
  222. /**
  223. * Obfuscate an e-mail address to prevent spam-bots from sniffing it.
  224. *
  225. * @param string $email
  226. * @return string
  227. */
  228. public static function email($email)
  229. {
  230. return str_replace('@', '&#64;', static::obfuscate($email));
  231. }
  232. /**
  233. * Generate an HTML image element.
  234. *
  235. * @param string $url
  236. * @param string $alt
  237. * @param array $attributes
  238. * @return string
  239. */
  240. public static function image($url, $alt = '', $attributes = array())
  241. {
  242. $attributes['alt'] = $alt;
  243. return '<img src="'.URL::to_asset($url).'"'.static::attributes($attributes).'>';
  244. }
  245. /**
  246. * Generate an ordered list of items.
  247. *
  248. * @param array $list
  249. * @param array $attributes
  250. * @return string
  251. */
  252. public static function ol($list, $attributes = array())
  253. {
  254. return static::listing('ol', $list, $attributes);
  255. }
  256. /**
  257. * Generate an un-ordered list of items.
  258. *
  259. * @param array $list
  260. * @param array $attributes
  261. * @return string
  262. */
  263. public static function ul($list, $attributes = array())
  264. {
  265. return static::listing('ul', $list, $attributes);
  266. }
  267. /**
  268. * Generate an ordered or un-ordered list.
  269. *
  270. * @param string $type
  271. * @param array $list
  272. * @param array $attributes
  273. * @return string
  274. */
  275. private static function listing($type, $list, $attributes = array())
  276. {
  277. $html = '';
  278. if (count($list) == 0) return $html;
  279. foreach ($list as $key => $value)
  280. {
  281. // If the value is an array, we will recurse the function so that we can
  282. // produce a nested list within the list being built. Of course, nested
  283. // lists may exist within nested lists, etc.
  284. if (is_array($value))
  285. {
  286. $html .= static::listing($type, $value);
  287. }
  288. else
  289. {
  290. $html .= '<li>'.static::entities($value).'</li>';
  291. }
  292. }
  293. return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
  294. }
  295. /**
  296. * Build a list of HTML attributes from an array.
  297. *
  298. * @param array $attributes
  299. * @return string
  300. */
  301. public static function attributes($attributes)
  302. {
  303. $html = array();
  304. foreach ((array) $attributes as $key => $value)
  305. {
  306. // For numeric keys, we will assume that the key and the value are the
  307. // same, as this will conver HTML attributes such as "required" that
  308. // may be specified as required="required", etc.
  309. if (is_numeric($key)) $key = $value;
  310. if ( ! is_null($value))
  311. {
  312. $html[] = $key.'="'.static::entities($value).'"';
  313. }
  314. }
  315. return (count($html) > 0) ? ' '.implode(' ', $html) : '';
  316. }
  317. /**
  318. * Obfuscate a string to prevent spam-bots from sniffing it.
  319. *
  320. * @param string $value
  321. * @return string
  322. */
  323. protected static function obfuscate($value)
  324. {
  325. $safe = '';
  326. foreach (str_split($value) as $letter)
  327. {
  328. // To properly obfuscate the value, we will randomly convert each
  329. // letter to its entity or hexadecimal representation, keeping a
  330. // bot from sniffing the randomly obfuscated letters.
  331. switch (rand(1, 3))
  332. {
  333. case 1:
  334. $safe .= '&#'.ord($letter).';';
  335. break;
  336. case 2:
  337. $safe .= '&#x'.dechex(ord($letter)).';';
  338. break;
  339. case 3:
  340. $safe .= $letter;
  341. }
  342. }
  343. return $safe;
  344. }
  345. /**
  346. * Dynamically handle calls to custom macros.
  347. *
  348. * @param string $method
  349. * @param array $parameters
  350. * @return mixed
  351. */
  352. public static function __callStatic($method, $parameters)
  353. {
  354. if (isset(static::$macros[$method]))
  355. {
  356. return call_user_func_array(static::$macros[$method], $parameters);
  357. }
  358. throw new \Exception("Method [$method] does not exist.");
  359. }
  360. }