PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/alamgircsejnu/AMS-Project-Laravel
PHP | 568 lines | 224 code | 74 blank | 270 comment | 16 complexity | 88e894d8a23fac0ec5fe6b4c623a344a 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. * @param bool $escape
  129. *
  130. * @return \Illuminate\Support\HtmlString
  131. */
  132. public function link($url, $title = null, $attributes = [], $secure = null, $escape = true)
  133. {
  134. $url = $this->url->to($url, [], $secure);
  135. if (is_null($title) || $title === false) {
  136. $title = $url;
  137. }
  138. if ($escape) {
  139. $title = $this->entities($title);
  140. }
  141. return $this->toHtmlString('<a href="' . $url . '"' . $this->attributes($attributes) . '>' . $title . '</a>');
  142. }
  143. /**
  144. * Generate a HTTPS HTML link.
  145. *
  146. * @param string $url
  147. * @param string $title
  148. * @param array $attributes
  149. *
  150. * @return \Illuminate\Support\HtmlString
  151. */
  152. public function secureLink($url, $title = null, $attributes = [])
  153. {
  154. return $this->link($url, $title, $attributes, true);
  155. }
  156. /**
  157. * Generate a HTML link to an asset.
  158. *
  159. * @param string $url
  160. * @param string $title
  161. * @param array $attributes
  162. * @param bool $secure
  163. *
  164. * @return \Illuminate\Support\HtmlString
  165. */
  166. public function linkAsset($url, $title = null, $attributes = [], $secure = null)
  167. {
  168. $url = $this->url->asset($url, $secure);
  169. return $this->link($url, $title ?: $url, $attributes, $secure);
  170. }
  171. /**
  172. * Generate a HTTPS HTML link to an asset.
  173. *
  174. * @param string $url
  175. * @param string $title
  176. * @param array $attributes
  177. *
  178. * @return \Illuminate\Support\HtmlString
  179. */
  180. public function linkSecureAsset($url, $title = null, $attributes = [])
  181. {
  182. return $this->linkAsset($url, $title, $attributes, true);
  183. }
  184. /**
  185. * Generate a HTML link to a named route.
  186. *
  187. * @param string $name
  188. * @param string $title
  189. * @param array $parameters
  190. * @param array $attributes
  191. *
  192. * @return \Illuminate\Support\HtmlString
  193. */
  194. public function linkRoute($name, $title = null, $parameters = [], $attributes = [])
  195. {
  196. return $this->link($this->url->route($name, $parameters), $title, $attributes);
  197. }
  198. /**
  199. * Generate a HTML link to a controller action.
  200. *
  201. * @param string $action
  202. * @param string $title
  203. * @param array $parameters
  204. * @param array $attributes
  205. *
  206. * @return \Illuminate\Support\HtmlString
  207. */
  208. public function linkAction($action, $title = null, $parameters = [], $attributes = [])
  209. {
  210. return $this->link($this->url->action($action, $parameters), $title, $attributes);
  211. }
  212. /**
  213. * Generate a HTML link to an email address.
  214. *
  215. * @param string $email
  216. * @param string $title
  217. * @param array $attributes
  218. * @param bool $escape
  219. *
  220. * @return \Illuminate\Support\HtmlString
  221. */
  222. public function mailto($email, $title = null, $attributes = [], $escape = true)
  223. {
  224. $email = $this->email($email);
  225. $title = $title ?: $email;
  226. if ($escape) {
  227. $title = $this->entities($title);
  228. }
  229. $email = $this->obfuscate('mailto:') . $email;
  230. return $this->toHtmlString('<a href="' . $email . '"' . $this->attributes($attributes) . '>' . $title . '</a>');
  231. }
  232. /**
  233. * Obfuscate an e-mail address to prevent spam-bots from sniffing it.
  234. *
  235. * @param string $email
  236. *
  237. * @return string
  238. */
  239. public function email($email)
  240. {
  241. return str_replace('@', '&#64;', $this->obfuscate($email));
  242. }
  243. /**
  244. * Generates non-breaking space entities based on number supplied.
  245. *
  246. * @param int $num
  247. *
  248. * @return string
  249. */
  250. public function nbsp($num = 1)
  251. {
  252. return str_repeat('&nbsp;', $num);
  253. }
  254. /**
  255. * Generate an ordered list of items.
  256. *
  257. * @param array $list
  258. * @param array $attributes
  259. *
  260. * @return \Illuminate\Support\HtmlString|string
  261. */
  262. public function ol($list, $attributes = [])
  263. {
  264. return $this->listing('ol', $list, $attributes);
  265. }
  266. /**
  267. * Generate an un-ordered list of items.
  268. *
  269. * @param array $list
  270. * @param array $attributes
  271. *
  272. * @return \Illuminate\Support\HtmlString|string
  273. */
  274. public function ul($list, $attributes = [])
  275. {
  276. return $this->listing('ul', $list, $attributes);
  277. }
  278. /**
  279. * Generate a description list of items.
  280. *
  281. * @param array $list
  282. * @param array $attributes
  283. *
  284. * @return \Illuminate\Support\HtmlString
  285. */
  286. public function dl(array $list, array $attributes = [])
  287. {
  288. $attributes = $this->attributes($attributes);
  289. $html = "<dl{$attributes}>";
  290. foreach ($list as $key => $value) {
  291. $value = (array) $value;
  292. $html .= "<dt>$key</dt>";
  293. foreach ($value as $v_key => $v_value) {
  294. $html .= "<dd>$v_value</dd>";
  295. }
  296. }
  297. $html .= '</dl>';
  298. return $this->toHtmlString($html);
  299. }
  300. /**
  301. * Create a listing HTML element.
  302. *
  303. * @param string $type
  304. * @param array $list
  305. * @param array $attributes
  306. *
  307. * @return \Illuminate\Support\HtmlString|string
  308. */
  309. protected function listing($type, $list, $attributes = [])
  310. {
  311. $html = '';
  312. if (count($list) == 0) {
  313. return $html;
  314. }
  315. // Essentially we will just spin through the list and build the list of the HTML
  316. // elements from the array. We will also handled nested lists in case that is
  317. // present in the array. Then we will build out the final listing elements.
  318. foreach ($list as $key => $value) {
  319. $html .= $this->listingElement($key, $type, $value);
  320. }
  321. $attributes = $this->attributes($attributes);
  322. return $this->toHtmlString("<{$type}{$attributes}>{$html}</{$type}>");
  323. }
  324. /**
  325. * Create the HTML for a listing element.
  326. *
  327. * @param mixed $key
  328. * @param string $type
  329. * @param mixed $value
  330. *
  331. * @return string
  332. */
  333. protected function listingElement($key, $type, $value)
  334. {
  335. if (is_array($value)) {
  336. return $this->nestedListing($key, $type, $value);
  337. } else {
  338. return '<li>' . e($value) . '</li>';
  339. }
  340. }
  341. /**
  342. * Create the HTML for a nested listing attribute.
  343. *
  344. * @param mixed $key
  345. * @param string $type
  346. * @param mixed $value
  347. *
  348. * @return string
  349. */
  350. protected function nestedListing($key, $type, $value)
  351. {
  352. if (is_int($key)) {
  353. return $this->listing($type, $value);
  354. } else {
  355. return '<li>' . $key . $this->listing($type, $value) . '</li>';
  356. }
  357. }
  358. /**
  359. * Build an HTML attribute string from an array.
  360. *
  361. * @param array $attributes
  362. *
  363. * @return string
  364. */
  365. public function attributes($attributes)
  366. {
  367. $html = [];
  368. foreach ((array) $attributes as $key => $value) {
  369. $element = $this->attributeElement($key, $value);
  370. if (! is_null($element)) {
  371. $html[] = $element;
  372. }
  373. }
  374. return count($html) > 0 ? ' ' . implode(' ', $html) : '';
  375. }
  376. /**
  377. * Build a single attribute element.
  378. *
  379. * @param string $key
  380. * @param string $value
  381. *
  382. * @return string
  383. */
  384. protected function attributeElement($key, $value)
  385. {
  386. // For numeric keys we will assume that the key and the value are the same
  387. // as this will convert HTML attributes such as "required" to a correct
  388. // form like required="required" instead of using incorrect numerics.
  389. if (is_numeric($key)) {
  390. $key = $value;
  391. }
  392. // Treat boolean attributes as HTML properties
  393. if (is_bool($value)) {
  394. return $value ? $key : '';
  395. }
  396. if (! is_null($value)) {
  397. return $key . '="' . e($value) . '"';
  398. }
  399. }
  400. /**
  401. * Obfuscate a string to prevent spam-bots from sniffing it.
  402. *
  403. * @param string $value
  404. *
  405. * @return string
  406. */
  407. public function obfuscate($value)
  408. {
  409. $safe = '';
  410. foreach (str_split($value) as $letter) {
  411. if (ord($letter) > 128) {
  412. return $letter;
  413. }
  414. // To properly obfuscate the value, we will randomly convert each letter to
  415. // its entity or hexadecimal representation, keeping a bot from sniffing
  416. // the randomly obfuscated letters out of the string on the responses.
  417. switch (rand(1, 3)) {
  418. case 1:
  419. $safe .= '&#' . ord($letter) . ';';
  420. break;
  421. case 2:
  422. $safe .= '&#x' . dechex(ord($letter)) . ';';
  423. break;
  424. case 3:
  425. $safe .= $letter;
  426. }
  427. }
  428. return $safe;
  429. }
  430. /**
  431. * Generate a meta tag.
  432. *
  433. * @param string $name
  434. * @param string $content
  435. * @param array $attributes
  436. *
  437. * @return \Illuminate\Support\HtmlString
  438. */
  439. public function meta($name, $content, array $attributes = [])
  440. {
  441. $defaults = compact('name', 'content');
  442. $attributes = array_merge($defaults, $attributes);
  443. return $this->toHtmlString('<meta' . $this->attributes($attributes) . '>' . PHP_EOL);
  444. }
  445. /**
  446. * Generate an html tag.
  447. *
  448. * @param string $tag
  449. * @param mixed $content
  450. * @param array $attributes
  451. *
  452. * @return \Illuminate\Support\HtmlString
  453. */
  454. public function tag($tag, $content, array $attributes = [])
  455. {
  456. $content = is_array($content) ? implode(PHP_EOL, $content) : $content;
  457. return $this->toHtmlString('<' . $tag . $this->attributes($attributes) . '>' . PHP_EOL . $this->toHtmlString($content) . PHP_EOL . '</' . $tag . '>' . PHP_EOL);
  458. }
  459. /**
  460. * Transform the string to an Html serializable object
  461. *
  462. * @param $html
  463. *
  464. * @return \Illuminate\Support\HtmlString
  465. */
  466. protected function toHtmlString($html)
  467. {
  468. return new HtmlString($html);
  469. }
  470. /**
  471. * Dynamically handle calls to the class.
  472. *
  473. * @param string $method
  474. * @param array $parameters
  475. *
  476. * @return \Illuminate\Contracts\View\View|mixed
  477. *
  478. * @throws \BadMethodCallException
  479. */
  480. public function __call($method, $parameters)
  481. {
  482. try {
  483. return $this->componentCall($method, $parameters);
  484. } catch (BadMethodCallException $e) {
  485. //
  486. }
  487. try {
  488. return $this->macroCall($method, $parameters);
  489. } catch (BadMethodCallException $e) {
  490. //
  491. }
  492. throw new BadMethodCallException("Method {$method} does not exist.");
  493. }
  494. }