PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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