PageRenderTime 26ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

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

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