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

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

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