Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
31 / 31 |
|
100.00% |
14 / 14 |
CRAP | |
100.00% |
1 / 1 |
| Meta | |
100.00% |
31 / 31 |
|
100.00% |
14 / 14 |
22 | |
100.00% |
1 / 1 |
| addKeyword | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getKeywords | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCharset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setCharset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setProperty | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getProperty | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setItemprop | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getItemprop | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| render | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| renderProperty | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| renderItemprop | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| renderName | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Model\Html |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Model\Html; |
| 16 | |
| 17 | use phpOMS\Contract\RenderableInterface; |
| 18 | use phpOMS\Views\ViewAbstract; |
| 19 | |
| 20 | /** |
| 21 | * Meta class. |
| 22 | * |
| 23 | * @package phpOMS\Model\Html |
| 24 | * @license OMS License 2.0 |
| 25 | * @link https://jingga.app |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | final class Meta implements RenderableInterface |
| 29 | { |
| 30 | /** |
| 31 | * Keywords. |
| 32 | * |
| 33 | * @var string[] |
| 34 | * @since 1.0.0 |
| 35 | */ |
| 36 | private array $keywords = []; |
| 37 | |
| 38 | /** |
| 39 | * Author. |
| 40 | * |
| 41 | * @var string |
| 42 | * @since 1.0.0 |
| 43 | */ |
| 44 | public string $author = ''; |
| 45 | |
| 46 | /** |
| 47 | * Charset. |
| 48 | * |
| 49 | * @var string |
| 50 | * @since 1.0.0 |
| 51 | */ |
| 52 | private string $charset = ''; |
| 53 | |
| 54 | /** |
| 55 | * Description. |
| 56 | * |
| 57 | * @var string |
| 58 | * @since 1.0.0 |
| 59 | */ |
| 60 | public string $description = ''; |
| 61 | |
| 62 | /** |
| 63 | * Itemprop. |
| 64 | * |
| 65 | * @var array<string, string> |
| 66 | * @since 1.0.0 |
| 67 | */ |
| 68 | private array $itemprops = []; |
| 69 | |
| 70 | /** |
| 71 | * Property. |
| 72 | * |
| 73 | * @var array<string, string> |
| 74 | * @since 1.0.0 |
| 75 | */ |
| 76 | private array $properties = []; |
| 77 | |
| 78 | /** |
| 79 | * Name. |
| 80 | * |
| 81 | * @var array<string, string> |
| 82 | * @since 1.0.0 |
| 83 | */ |
| 84 | private array $names = []; |
| 85 | |
| 86 | /** |
| 87 | * Add keyword. |
| 88 | * |
| 89 | * @param string $keyword Keyword |
| 90 | * |
| 91 | * @return void |
| 92 | * |
| 93 | * @since 1.0.0 |
| 94 | */ |
| 95 | public function addKeyword(string $keyword) : void |
| 96 | { |
| 97 | if (!\in_array($keyword, $this->keywords)) { |
| 98 | $this->keywords[] = $keyword; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get keywords. |
| 104 | * |
| 105 | * @return string[] Keywords |
| 106 | * |
| 107 | * @since 1.0.0 |
| 108 | */ |
| 109 | public function getKeywords() : array |
| 110 | { |
| 111 | return $this->keywords; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get charset. |
| 116 | * |
| 117 | * @return string Charset |
| 118 | * |
| 119 | * @since 1.0.0 |
| 120 | */ |
| 121 | public function getCharset() : string |
| 122 | { |
| 123 | return $this->charset; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Set charset. |
| 128 | * |
| 129 | * @param string $charset Charset |
| 130 | * |
| 131 | * @return void |
| 132 | * |
| 133 | * @since 1.0.0 |
| 134 | */ |
| 135 | public function setCharset(string $charset) : void |
| 136 | { |
| 137 | $this->charset = $charset; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Set property. |
| 142 | * |
| 143 | * @param string $property Property |
| 144 | * @param string $content Content |
| 145 | * |
| 146 | * @return void |
| 147 | * |
| 148 | * @since 1.0.0 |
| 149 | */ |
| 150 | public function setProperty(string $property, string $content) : void |
| 151 | { |
| 152 | $this->properties[$property] = $content; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get property. |
| 157 | * |
| 158 | * @param string $property Property |
| 159 | * |
| 160 | * @return string |
| 161 | * |
| 162 | * @since 1.0.0 |
| 163 | */ |
| 164 | public function getProperty(string $property) : string |
| 165 | { |
| 166 | return $this->properties[$property] ?? ''; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Set itemprop. |
| 171 | * |
| 172 | * @param string $itemprop Property |
| 173 | * @param string $content Content |
| 174 | * |
| 175 | * @return void |
| 176 | * |
| 177 | * @since 1.0.0 |
| 178 | */ |
| 179 | public function setItemprop(string $itemprop, string $content) : void |
| 180 | { |
| 181 | $this->itemprops[$itemprop] = $content; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Get itemprop. |
| 186 | * |
| 187 | * @param string $itemprop Itemprop |
| 188 | * |
| 189 | * @return string |
| 190 | * |
| 191 | * @since 1.0.0 |
| 192 | */ |
| 193 | public function getItemprop(string $itemprop) : string |
| 194 | { |
| 195 | return $this->itemprops[$itemprop] ?? ''; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Set name. |
| 200 | * |
| 201 | * @param string $name Name |
| 202 | * @param string $content Content |
| 203 | * |
| 204 | * @return void |
| 205 | * |
| 206 | * @since 1.0.0 |
| 207 | */ |
| 208 | public function setName(string $name, string $content) : void |
| 209 | { |
| 210 | $this->names[$name] = $content; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Get name. |
| 215 | * |
| 216 | * @param string $name Name |
| 217 | * |
| 218 | * @return string |
| 219 | * |
| 220 | * @since 1.0.0 |
| 221 | */ |
| 222 | public function getName(string $name) : string |
| 223 | { |
| 224 | return $this->names[$name] ?? ''; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * {@inheritdoc} |
| 229 | */ |
| 230 | public function render(mixed ...$data) : string |
| 231 | { |
| 232 | return (empty($this->keywords) ? '' : '<meta name="keywords" content="' . ViewAbstract::html(\implode(',', $this->keywords)) . '">') |
| 233 | . (empty($this->author) ? '' : '<meta name="author" content="' . ViewAbstract::html($this->author) . '">') |
| 234 | . (empty($this->description) ? '' : '<meta name="description" content="' . ViewAbstract::html($this->description) . '">') |
| 235 | . (empty($this->charset) ? '' : '<meta charset="' . ViewAbstract::html($this->charset) . '">') |
| 236 | . '<meta name="generator" content="Karaka">' |
| 237 | . $this->renderProperty() |
| 238 | . $this->renderItemprop() |
| 239 | . $this->renderName(); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Render property meta tags. |
| 244 | * |
| 245 | * @return string |
| 246 | * |
| 247 | * @since 1.0.0 |
| 248 | */ |
| 249 | private function renderProperty() : string |
| 250 | { |
| 251 | $properties = ''; |
| 252 | foreach ($this->properties as $key => $content) { |
| 253 | $properties .= '<meta property="' . $key . '" content="' . $content . '">'; |
| 254 | } |
| 255 | |
| 256 | return $properties; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Render itemprop meta tags. |
| 261 | * |
| 262 | * @return string |
| 263 | * |
| 264 | * @since 1.0.0 |
| 265 | */ |
| 266 | private function renderItemprop() : string |
| 267 | { |
| 268 | $itemprops = ''; |
| 269 | foreach ($this->itemprops as $key => $content) { |
| 270 | $itemprops .= '<meta itemprop="' . $key . '" content="' . $content . '">'; |
| 271 | } |
| 272 | |
| 273 | return $itemprops; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Render name meta tags. |
| 278 | * |
| 279 | * @return string |
| 280 | * |
| 281 | * @since 1.0.0 |
| 282 | */ |
| 283 | private function renderName() : string |
| 284 | { |
| 285 | $names = ''; |
| 286 | foreach ($this->names as $key => $content) { |
| 287 | $names .= '<meta name="' . $key . '" content="' . $content . '">'; |
| 288 | } |
| 289 | |
| 290 | return $names; |
| 291 | } |
| 292 | } |