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

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

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