PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravelcollective/html/src/HtmlBuilder.php

https://gitlab.com/judielsm/Handora
PHP | 438 lines | 172 code | 63 blank | 203 comment | 11 complexity | 8323ff35dbb79fdaf1f63b446fe34727 MD5 | raw file
  1. <?php namespace Collective\Html;
  2. use Illuminate\Routing\UrlGenerator;
  3. use Illuminate\Support\Traits\Macroable;
  4. class HtmlBuilder {
  5. use Macroable;
  6. /**
  7. * The URL generator instance.
  8. *
  9. * @var \Illuminate\Routing\UrlGenerator
  10. */
  11. protected $url;
  12. /**
  13. * Create a new HTML builder instance.
  14. *
  15. * @param \Illuminate\Routing\UrlGenerator $url
  16. * @return void
  17. */
  18. public function __construct(UrlGenerator $url = null)
  19. {
  20. $this->url = $url;
  21. }
  22. /**
  23. * Convert an HTML string to entities.
  24. *
  25. * @param string $value
  26. * @return string
  27. */
  28. public function entities($value)
  29. {
  30. return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
  31. }
  32. /**
  33. * Convert entities to HTML characters.
  34. *
  35. * @param string $value
  36. * @return string
  37. */
  38. public function decode($value)
  39. {
  40. return html_entity_decode($value, ENT_QUOTES, 'UTF-8');
  41. }
  42. /**
  43. * Generate a link to a JavaScript file.
  44. *
  45. * @param string $url
  46. * @param array $attributes
  47. * @param bool $secure
  48. * @return string
  49. */
  50. public function script($url, $attributes = array(), $secure = null)
  51. {
  52. $attributes['src'] = $this->url->asset($url, $secure);
  53. return '<script'.$this->attributes($attributes).'></script>'.PHP_EOL;
  54. }
  55. /**
  56. * Generate a link to a CSS file.
  57. *
  58. * @param string $url
  59. * @param array $attributes
  60. * @param bool $secure
  61. * @return string
  62. */
  63. public function style($url, $attributes = array(), $secure = null)
  64. {
  65. $defaults = array('media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet');
  66. $attributes = $attributes + $defaults;
  67. $attributes['href'] = $this->url->asset($url, $secure);
  68. return '<link'.$this->attributes($attributes).'>'.PHP_EOL;
  69. }
  70. /**
  71. * Generate an HTML image element.
  72. *
  73. * @param string $url
  74. * @param string $alt
  75. * @param array $attributes
  76. * @param bool $secure
  77. * @return string
  78. */
  79. public function image($url, $alt = null, $attributes = array(), $secure = null)
  80. {
  81. $attributes['alt'] = $alt;
  82. return '<img src="'.$this->url->asset($url, $secure).'"'.$this->attributes($attributes).'>';
  83. }
  84. /**
  85. * Generate a link to a Favicon file.
  86. *
  87. * @param string $url
  88. * @param array $attributes
  89. * @param bool $secure
  90. * @return string
  91. */
  92. public function favicon($url, $attributes = array(), $secure = null)
  93. {
  94. $defaults = array('rel' => 'shortcut icon', 'type' => 'image/x-icon');
  95. $attributes = $attributes + $defaults;
  96. $attributes['href'] = $this->url->asset($url, $secure);
  97. return '<link'.$this->attributes($attributes).'>'.PHP_EOL;
  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. * Generate a description list of items.
  227. *
  228. * @param array $list
  229. * @param array $attributes
  230. * @return string
  231. */
  232. public function dl(array $list, array $attributes = [])
  233. {
  234. $attributes = $this->attributes($attributes);
  235. $html = "<dl{$attributes}>";
  236. foreach ($list as $key => $value)
  237. {
  238. $html .= "<dt>$key</dt><dd>$value</dd>";
  239. }
  240. $html .= '</dl>';
  241. return $html;
  242. }
  243. /**
  244. * Create a listing HTML element.
  245. *
  246. * @param string $type
  247. * @param array $list
  248. * @param array $attributes
  249. * @return string
  250. */
  251. protected function listing($type, $list, $attributes = array())
  252. {
  253. $html = '';
  254. if (count($list) == 0) return $html;
  255. // Essentially we will just spin through the list and build the list of the HTML
  256. // elements from the array. We will also handled nested lists in case that is
  257. // present in the array. Then we will build out the final listing elements.
  258. foreach ($list as $key => $value)
  259. {
  260. $html .= $this->listingElement($key, $type, $value);
  261. }
  262. $attributes = $this->attributes($attributes);
  263. return "<{$type}{$attributes}>{$html}</{$type}>";
  264. }
  265. /**
  266. * Create the HTML for a listing element.
  267. *
  268. * @param mixed $key
  269. * @param string $type
  270. * @param string $value
  271. * @return string
  272. */
  273. protected function listingElement($key, $type, $value)
  274. {
  275. if (is_array($value))
  276. {
  277. return $this->nestedListing($key, $type, $value);
  278. }
  279. else
  280. {
  281. return '<li>'.e($value).'</li>';
  282. }
  283. }
  284. /**
  285. * Create the HTML for a nested listing attribute.
  286. *
  287. * @param mixed $key
  288. * @param string $type
  289. * @param string $value
  290. * @return string
  291. */
  292. protected function nestedListing($key, $type, $value)
  293. {
  294. if (is_int($key))
  295. {
  296. return $this->listing($type, $value);
  297. }
  298. else
  299. {
  300. return '<li>'.$key.$this->listing($type, $value).'</li>';
  301. }
  302. }
  303. /**
  304. * Build an HTML attribute string from an array.
  305. *
  306. * @param array $attributes
  307. * @return string
  308. */
  309. public function attributes($attributes)
  310. {
  311. $html = array();
  312. foreach ((array) $attributes as $key => $value)
  313. {
  314. $element = $this->attributeElement($key, $value);
  315. if ( ! is_null($element)) $html[] = $element;
  316. }
  317. return count($html) > 0 ? ' '.implode(' ', $html) : '';
  318. }
  319. /**
  320. * Build a single attribute element.
  321. *
  322. * @param string $key
  323. * @param string $value
  324. * @return string
  325. */
  326. protected function attributeElement($key, $value)
  327. {
  328. // For numeric keys we will assume that the key and the value are the same
  329. // as this will convert HTML attributes such as "required" to a correct
  330. // form like required="required" instead of using incorrect numerics.
  331. if (is_numeric($key)) $key = $value;
  332. if ( ! is_null($value)) return $key.'="'.e($value).'"';
  333. }
  334. /**
  335. * Obfuscate a string to prevent spam-bots from sniffing it.
  336. *
  337. * @param string $value
  338. * @return string
  339. */
  340. public function obfuscate($value)
  341. {
  342. $safe = '';
  343. foreach (str_split($value) as $letter)
  344. {
  345. if (ord($letter) > 128) return $letter;
  346. // To properly obfuscate the value, we will randomly convert each letter to
  347. // its entity or hexadecimal representation, keeping a bot from sniffing
  348. // the randomly obfuscated letters out of the string on the responses.
  349. switch (rand(1, 3))
  350. {
  351. case 1:
  352. $safe .= '&#'.ord($letter).';'; break;
  353. case 2:
  354. $safe .= '&#x'.dechex(ord($letter)).';'; break;
  355. case 3:
  356. $safe .= $letter;
  357. }
  358. }
  359. return $safe;
  360. }
  361. /**
  362. * Generate a meta tag.
  363. *
  364. * @param string $name
  365. * @param string $content
  366. * @param array $attributes
  367. * @return string
  368. */
  369. public function meta($name, $content, array $attributes = [])
  370. {
  371. $defaults = compact('name', 'content');
  372. $attributes = array_merge($defaults, $attributes);
  373. return '<meta'.$this->attributes($attributes).'>'.PHP_EOL;
  374. }
  375. }