PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Html/HtmlBuilder.php

https://bitbucket.org/helfreire/tccsite
PHP | 409 lines | 159 code | 54 blank | 196 comment | 11 complexity | bad27d8c4e701472153e7d751149a0c4 MD5 | raw file
  1. <?php namespace Illuminate\Html;
  2. use Illuminate\Routing\UrlGenerator;
  3. class HtmlBuilder {
  4. /**
  5. * The URL generator instance.
  6. *
  7. * @var \Illuminate\Routing\UrlGenerator
  8. */
  9. protected $url;
  10. /**
  11. * The registered html macros.
  12. *
  13. * @var array
  14. */
  15. protected $macros;
  16. /**
  17. * Create a new HTML builder instance.
  18. *
  19. * @param \Illuminate\Routing\UrlGenerator $url
  20. * @return void
  21. */
  22. public function __construct(UrlGenerator $url = null)
  23. {
  24. $this->url = $url;
  25. }
  26. /**
  27. * Register a custom HTML macro.
  28. *
  29. * @param string $name
  30. * @param callable $macro
  31. * @return void
  32. */
  33. public function macro($name, $macro)
  34. {
  35. $this->macros[$name] = $macro;
  36. }
  37. /**
  38. * Convert an HTML string to entities.
  39. *
  40. * @param string $value
  41. * @return string
  42. */
  43. public function entities($value)
  44. {
  45. return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
  46. }
  47. /**
  48. * Convert entities to HTML characters.
  49. *
  50. * @param string $value
  51. * @return string
  52. */
  53. public function decode($value)
  54. {
  55. return html_entity_decode($value, ENT_QUOTES, 'UTF-8');
  56. }
  57. /**
  58. * Generate a link to a JavaScript file.
  59. *
  60. * @param string $url
  61. * @param array $attributes
  62. * @return string
  63. */
  64. public function script($url, $attributes = array())
  65. {
  66. $attributes['src'] = $this->url->asset($url);
  67. return '<script'.$this->attributes($attributes).'></script>'.PHP_EOL;
  68. }
  69. /**
  70. * Generate a link to a CSS file.
  71. *
  72. * @param string $url
  73. * @param array $attributes
  74. * @return string
  75. */
  76. public function style($url, $attributes = array())
  77. {
  78. $defaults = array('media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet');
  79. $attributes = $attributes + $defaults;
  80. $attributes['href'] = $this->url->asset($url);
  81. return '<link'.$this->attributes($attributes).'>'.PHP_EOL;
  82. }
  83. /**
  84. * Generate an HTML image element.
  85. *
  86. * @param string $url
  87. * @param string $alt
  88. * @param array $attributes
  89. * @return string
  90. */
  91. public function image($url, $alt = null, $attributes = array())
  92. {
  93. $attributes['alt'] = $alt;
  94. return '<img src="'.$this->url->asset($url).'"'.$this->attributes($attributes).'>';
  95. }
  96. /**
  97. * Generate a HTML link.
  98. *
  99. * @param string $url
  100. * @param string $title
  101. * @param array $attributes
  102. * @param bool $secure
  103. * @return string
  104. */
  105. public function link($url, $title = null, $attributes = array(), $secure = null)
  106. {
  107. $url = $this->url->to($url, array(), $secure);
  108. if (is_null($title) or $title === false) $title = $url;
  109. return '<a href="'.$url.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>';
  110. }
  111. /**
  112. * Generate a HTTPS HTML link.
  113. *
  114. * @param string $url
  115. * @param string $title
  116. * @param array $attributes
  117. * @return string
  118. */
  119. public function secureLink($url, $title = null, $attributes = array())
  120. {
  121. return $this->link($url, $title, $attributes, true);
  122. }
  123. /**
  124. * Generate a HTML link to an asset.
  125. *
  126. * @param string $url
  127. * @param string $title
  128. * @param array $attributes
  129. * @param bool $secure
  130. * @return string
  131. */
  132. public function linkAsset($url, $title = null, $attributes = array(), $secure = null)
  133. {
  134. $url = $this->url->asset($url, $secure);
  135. return $this->link($url, $title ?: $url, $attributes, $secure);
  136. }
  137. /**
  138. * Generate a HTTPS HTML link to an asset.
  139. *
  140. * @param string $url
  141. * @param string $title
  142. * @param array $attributes
  143. * @return string
  144. */
  145. public function linkSecureAsset($url, $title = null, $attributes = array())
  146. {
  147. return $this->linkAsset($url, $title, $attributes, true);
  148. }
  149. /**
  150. * Generate a HTML link to a named route.
  151. *
  152. * @param string $name
  153. * @param string $title
  154. * @param array $parameters
  155. * @param array $attributes
  156. * @return string
  157. */
  158. public function linkRoute($name, $title = null, $parameters = array(), $attributes = array())
  159. {
  160. return $this->link($this->url->route($name, $parameters), $title, $attributes);
  161. }
  162. /**
  163. * Generate a HTML link to a controller action.
  164. *
  165. * @param string $action
  166. * @param string $title
  167. * @param array $parameters
  168. * @param array $attributes
  169. * @return string
  170. */
  171. public function linkAction($action, $title = null, $parameters = array(), $attributes = array())
  172. {
  173. return $this->link($this->url->action($action, $parameters), $title, $attributes);
  174. }
  175. /**
  176. * Generate a HTML link to an email address.
  177. *
  178. * @param string $email
  179. * @param string $title
  180. * @param array $attributes
  181. * @return string
  182. */
  183. public function mailto($email, $title = null, $attributes = array())
  184. {
  185. $email = $this->email($email);
  186. $title = $title ?: $email;
  187. $email = $this->obfuscate('mailto:') . $email;
  188. return '<a href="'.$email.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>';
  189. }
  190. /**
  191. * Obfuscate an e-mail address to prevent spam-bots from sniffing it.
  192. *
  193. * @param string $email
  194. * @return string
  195. */
  196. public function email($email)
  197. {
  198. return str_replace('@', '&#64;', $this->obfuscate($email));
  199. }
  200. /**
  201. * Generate an ordered list of items.
  202. *
  203. * @param array $list
  204. * @param array $attributes
  205. * @return string
  206. */
  207. public function ol($list, $attributes = array())
  208. {
  209. return $this->listing('ol', $list, $attributes);
  210. }
  211. /**
  212. * Generate an un-ordered list of items.
  213. *
  214. * @param array $list
  215. * @param array $attributes
  216. * @return string
  217. */
  218. public function ul($list, $attributes = array())
  219. {
  220. return $this->listing('ul', $list, $attributes);
  221. }
  222. /**
  223. * Create a listing HTML element.
  224. *
  225. * @param string $type
  226. * @param array $list
  227. * @param array $attributes
  228. * @return string
  229. */
  230. protected function listing($type, $list, $attributes = array())
  231. {
  232. $html = '';
  233. if (count($list) == 0) return $html;
  234. // Essentially we will just spin through the list and build the list of the HTML
  235. // elements from the array. We will also handled nested lists in case that is
  236. // present in the array. Then we will build out the final listing elements.
  237. foreach ($list as $key => $value)
  238. {
  239. $html .= $this->listingElement($key, $type, $value);
  240. }
  241. $attributes = $this->attributes($attributes);
  242. return "<{$type}{$attributes}>{$html}</{$type}>";
  243. }
  244. /**
  245. * Create the HTML for a listing element.
  246. *
  247. * @param mixed $key
  248. * @param string $type
  249. * @param string $value
  250. * @return string
  251. */
  252. protected function listingElement($key, $type, $value)
  253. {
  254. if (is_array($value))
  255. {
  256. return $this->nestedListing($key, $type, $value);
  257. }
  258. else
  259. {
  260. return '<li>'.e($value).'</li>';
  261. }
  262. }
  263. /**
  264. * Create the HTML for a nested listing attribute.
  265. *
  266. * @param mixed $key
  267. * @param string $type
  268. * @param string $value
  269. * @return string
  270. */
  271. protected function nestedListing($key, $type, $value)
  272. {
  273. if (is_int($key))
  274. {
  275. return $this->listing($type, $value);
  276. }
  277. else
  278. {
  279. return '<li>'.$key.$this->listing($type, $value).'</li>';
  280. }
  281. }
  282. /**
  283. * Build an HTML attribute string from an array.
  284. *
  285. * @param array $attributes
  286. * @return string
  287. */
  288. public function attributes($attributes)
  289. {
  290. $html = array();
  291. // For numeric keys we will assume that the key and the value are the same
  292. // as this will convert HTML attributes such as "required" to a correct
  293. // form like required="required" instead of using incorrect numerics.
  294. foreach ((array) $attributes as $key => $value)
  295. {
  296. $element = $this->attributeElement($key, $value);
  297. if ( ! is_null($element)) $html[] = $element;
  298. }
  299. return count($html) > 0 ? ' '.implode(' ', $html) : '';
  300. }
  301. /**
  302. * Build a single attribute element.
  303. *
  304. * @param string $key
  305. * @param string $value
  306. * @return string
  307. */
  308. protected function attributeElement($key, $value)
  309. {
  310. if (is_numeric($key)) $key = $value;
  311. if ( ! is_null($value)) return $key.'="'.e($value).'"';
  312. }
  313. /**
  314. * Obfuscate a string to prevent spam-bots from sniffing it.
  315. *
  316. * @param string $value
  317. * @return string
  318. */
  319. public function obfuscate($value)
  320. {
  321. $safe = '';
  322. foreach (str_split($value) as $letter)
  323. {
  324. if (ord($letter) > 128) return $letter;
  325. // To properly obfuscate the value, we will randomly convert each letter to
  326. // its entity or hexadecimal representation, keeping a bot from sniffing
  327. // the randomly obfuscated letters out of the string on the responses.
  328. switch (rand(1, 3))
  329. {
  330. case 1:
  331. $safe .= '&#'.ord($letter).';'; break;
  332. case 2:
  333. $safe .= '&#x'.dechex(ord($letter)).';'; break;
  334. case 3:
  335. $safe .= $letter;
  336. }
  337. }
  338. return $safe;
  339. }
  340. /**
  341. * Dynamically handle calls to the html class.
  342. *
  343. * @param string $method
  344. * @param array $parameters
  345. * @return mixed
  346. */
  347. public function __call($method, $parameters)
  348. {
  349. if (isset($this->macros[$method]))
  350. {
  351. return call_user_func_array($this->macros[$method], $parameters);
  352. }
  353. throw new \BadMethodCallException("Method {$method} does not exist.");
  354. }
  355. }