|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace UIAwesome\Html\Helper\Base; |
| 6 | + |
| 7 | +use UIAwesome\Html\Helper\Attributes; |
| 8 | + |
| 9 | +use function in_array; |
| 10 | +use function strtolower; |
| 11 | + |
| 12 | +/** |
| 13 | + * Provides common functionality for generate HTML code fragments programmatically. |
| 14 | + * |
| 15 | + * Concrete classes should extend this class to implement specific HTML elements and their generation logic. |
| 16 | + */ |
| 17 | +abstract class AbstractHTMLBuilder |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @psalm-var string[] |
| 21 | + */ |
| 22 | + private const INLINE_ELEMENTS = [ |
| 23 | + 'a', |
| 24 | + 'abbr', |
| 25 | + 'acronym', |
| 26 | + 'audio', |
| 27 | + 'b', |
| 28 | + 'bdi', |
| 29 | + 'bdo', |
| 30 | + 'big', |
| 31 | + 'br', |
| 32 | + 'button', |
| 33 | + 'canvas', |
| 34 | + 'cite', |
| 35 | + 'code', |
| 36 | + 'data', |
| 37 | + 'datalist', |
| 38 | + 'del', |
| 39 | + 'dfn', |
| 40 | + 'em', |
| 41 | + 'embed', |
| 42 | + 'i', |
| 43 | + 'iframe', |
| 44 | + 'img', |
| 45 | + 'input', |
| 46 | + 'ins', |
| 47 | + 'kbd', |
| 48 | + 'label', |
| 49 | + 'map', |
| 50 | + 'mark', |
| 51 | + 'meter', |
| 52 | + 'noscript', |
| 53 | + 'object', |
| 54 | + 'option', |
| 55 | + 'output', |
| 56 | + 'picture', |
| 57 | + 'progress', |
| 58 | + 'q', |
| 59 | + 'ruby', |
| 60 | + 's', |
| 61 | + 'samp', |
| 62 | + 'script', |
| 63 | + 'select', |
| 64 | + 'slot', |
| 65 | + 'small', |
| 66 | + 'span', |
| 67 | + 'strong', |
| 68 | + 'sub', |
| 69 | + 'sup', |
| 70 | + 'svg', |
| 71 | + 'template', |
| 72 | + 'textarea', |
| 73 | + 'time', |
| 74 | + 'u', |
| 75 | + 'td', |
| 76 | + 'th', |
| 77 | + 'tt', |
| 78 | + 'var', |
| 79 | + 'video', |
| 80 | + 'wbr', |
| 81 | + ]; |
| 82 | + |
| 83 | + /** |
| 84 | + * @psalm-var string[] |
| 85 | + */ |
| 86 | + private const VOID_ELEMENT = [ |
| 87 | + 'area', |
| 88 | + 'base', |
| 89 | + 'br', |
| 90 | + 'col', |
| 91 | + 'command', |
| 92 | + 'embed', |
| 93 | + 'hr', |
| 94 | + 'img', |
| 95 | + 'input', |
| 96 | + 'keygen', |
| 97 | + 'link', |
| 98 | + 'meta', |
| 99 | + 'param', |
| 100 | + 'source', |
| 101 | + 'track', |
| 102 | + 'wbr', |
| 103 | + ]; |
| 104 | + |
| 105 | + /** |
| 106 | + * This method creates a new HTML begin tag with the specified tag name and attributes. |
| 107 | + * |
| 108 | + * @param string $tag The tag name. |
| 109 | + * @param array $attributes The tag attributes. |
| 110 | + * |
| 111 | + * @return string The begin tag. |
| 112 | + */ |
| 113 | + public static function beginTag(string $tag, array $attributes = []): string |
| 114 | + { |
| 115 | + $helperAttributes = new Attributes(); |
| 116 | + $tag = self::validateTag($tag); |
| 117 | + |
| 118 | + if (self::inlinedElements($tag)) { |
| 119 | + throw new \InvalidArgumentException('Inline elements cannot be used with begin/end syntax.'); |
| 120 | + } |
| 121 | + |
| 122 | + return '<' . $tag . $helperAttributes->render($attributes) . '>'; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * This method creates a new HTML tag with the specified tag name, content, and attributes. |
| 127 | + * |
| 128 | + * @param string $tag The tag name. |
| 129 | + * @param string $content The content of the tag. |
| 130 | + * @param array $attributes The attributes of the tag. |
| 131 | + * |
| 132 | + * @return string The tag. |
| 133 | + */ |
| 134 | + public static function createTag(string $tag, string $content = '', array $attributes = []): string |
| 135 | + { |
| 136 | + $tag = self::validateTag($tag); |
| 137 | + $voidElement = "<$tag" . Attributes::render($attributes) . '>'; |
| 138 | + |
| 139 | + if (self::voidElements($tag)) { |
| 140 | + return $voidElement; |
| 141 | + } |
| 142 | + |
| 143 | + if (self::inlinedElements($tag)) { |
| 144 | + return "$voidElement$content</$tag>"; |
| 145 | + } |
| 146 | + |
| 147 | + $content = $content === '' ? '' : $content . PHP_EOL; |
| 148 | + |
| 149 | + return "$voidElement\n$content</$tag>"; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * This method creates a new HTML end tag with the specified tag name. |
| 154 | + * |
| 155 | + * @param string $tag The tag name. |
| 156 | + * |
| 157 | + * @return string The closing tag. |
| 158 | + */ |
| 159 | + public static function endTag(string $tag): string |
| 160 | + { |
| 161 | + if (self::inlinedElements($tag)) { |
| 162 | + throw new \InvalidArgumentException('Inline elements cannot be used with begin/end syntax.'); |
| 163 | + } |
| 164 | + |
| 165 | + $tag = self::validateTag($tag); |
| 166 | + |
| 167 | + return "</$tag>"; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @return bool True if tag is inlined element. |
| 172 | + * |
| 173 | + * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements |
| 174 | + */ |
| 175 | + private static function inlinedElements(string $tag): bool |
| 176 | + { |
| 177 | + return in_array($tag, self::INLINE_ELEMENTS, true); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * @return bool True if tag is void element. |
| 182 | + * |
| 183 | + * @link http://www.w3.org/TR/html-markup/syntax.html#void-element |
| 184 | + */ |
| 185 | + private static function voidElements(string $tag): bool |
| 186 | + { |
| 187 | + return in_array($tag, self::VOID_ELEMENT, true); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * @throws \InvalidArgumentException |
| 192 | + */ |
| 193 | + private static function validateTag(string $tag): string |
| 194 | + { |
| 195 | + $tag = strtolower($tag); |
| 196 | + |
| 197 | + if ($tag === '') { |
| 198 | + throw new \InvalidArgumentException('Tag name cannot be empty.'); |
| 199 | + } |
| 200 | + |
| 201 | + return $tag; |
| 202 | + } |
| 203 | +} |
0 commit comments