| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- namespace BBMenus;
- class HtmlBuilder {
- /**
- * Create an HTML link.
- *
- * @param string $url
- * @param string $title
- * @param array $attributes
- * @return string
- */
- public function link($url, $title, $icon = "", $attributes = [])
- {
- return '<a href="'.$url.'"'.$this->attributes($attributes).'>'.$icon.$this->entities($title).'</a>';
- }
- /**
- * Convert an HTML string to entities.
- *
- * @param string $value
- * @return string
- */
- public function entities($value)
- {
- return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
- }
- /**
- * Build an HTML attribute string from an array.
- *
- * @param array $attributes
- * @return string
- */
- public function attributes(array $attributes)
- {
- $html = [];
- foreach ($attributes as $key => $value)
- {
- if (is_null($value)) continue;
- if (is_numeric($key)) $key = $value;
- $html[] = $key.'="'.$this->entities($value).'"';
- }
- return empty($html) ? '' : ' '.implode(' ', $html);
- }
- }
|