HtmlBuilder.php 976 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace BBMenus;
  3. class HtmlBuilder {
  4. /**
  5. * Create an HTML link.
  6. *
  7. * @param string $url
  8. * @param string $title
  9. * @param array $attributes
  10. * @return string
  11. */
  12. public function link($url, $title, $icon = "", $attributes = [])
  13. {
  14. return '<a href="'.$url.'"'.$this->attributes($attributes).'>'.$icon.$this->entities($title).'</a>';
  15. }
  16. /**
  17. * Convert an HTML string to entities.
  18. *
  19. * @param string $value
  20. * @return string
  21. */
  22. public function entities($value)
  23. {
  24. return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
  25. }
  26. /**
  27. * Build an HTML attribute string from an array.
  28. *
  29. * @param array $attributes
  30. * @return string
  31. */
  32. public function attributes(array $attributes)
  33. {
  34. $html = [];
  35. foreach ($attributes as $key => $value)
  36. {
  37. if (is_null($value)) continue;
  38. if (is_numeric($key)) $key = $value;
  39. $html[] = $key.'="'.$this->entities($value).'"';
  40. }
  41. return empty($html) ? '' : ' '.implode(' ', $html);
  42. }
  43. }