PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/aurora/html.php

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