Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
40.91% |
9 / 22 |
CRAP | |
45.83% |
22 / 48 |
| Theme | |
0.00% |
0 / 1 |
|
40.91% |
9 / 22 |
142.86 | |
45.83% |
22 / 48 |
| getLang | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setLang | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| getMetas | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setMetas | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| addMeta | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| renderMetas | |
100.00% |
1 / 1 |
3 | |
100.00% |
7 / 7 |
|||
| getTitle | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setTitle | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| renderTitle | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getBody | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| setBody | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| getStyles | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| setStyles | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| appendStyles | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| prependStyles | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| renderStyles | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 4 |
|||
| getScripts | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| setScripts | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| appendScripts | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| prependScripts | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| renderScripts | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 4 |
|||
| escape | |
0.00% |
0 / 1 |
2.06 | |
75.00% |
3 / 4 |
|||
| 1 | <?php namespace Framework\Theme; |
| 2 | |
| 3 | class Theme |
| 4 | { |
| 5 | protected string $lang = 'en'; |
| 6 | /** |
| 7 | * @var array|array[] |
| 8 | */ |
| 9 | protected array $metas = []; |
| 10 | protected string $title = ''; |
| 11 | protected string $body = ''; |
| 12 | /** |
| 13 | * @var array|string[] |
| 14 | */ |
| 15 | protected array $styles = []; |
| 16 | /** |
| 17 | * @var array|string[] |
| 18 | */ |
| 19 | protected array $scripts = []; |
| 20 | |
| 21 | public function getLang() : string |
| 22 | { |
| 23 | return $this->lang; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @param string $lang |
| 28 | * |
| 29 | * @return $this |
| 30 | */ |
| 31 | public function setLang(string $lang) |
| 32 | { |
| 33 | $this->lang = $lang; |
| 34 | return $this; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @return array|array[] |
| 39 | */ |
| 40 | public function getMetas() : array |
| 41 | { |
| 42 | return $this->metas; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param array|array[] $metas |
| 47 | * |
| 48 | * @return $this |
| 49 | */ |
| 50 | public function setMetas(array $metas) |
| 51 | { |
| 52 | $this->metas = $metas; |
| 53 | return $this; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param array|string[] $meta |
| 58 | * |
| 59 | * @return $this |
| 60 | */ |
| 61 | public function addMeta(array $meta) |
| 62 | { |
| 63 | $this->metas[] = $meta; |
| 64 | return $this; |
| 65 | } |
| 66 | |
| 67 | public function renderMetas() : string |
| 68 | { |
| 69 | $content = ''; |
| 70 | foreach ($this->getMetas() as $meta) { |
| 71 | $part = ''; |
| 72 | foreach ($meta as $key => $value) { |
| 73 | $part .= $this->escape($key) . '="' . $this->escape($value) . '" '; |
| 74 | } |
| 75 | $content .= '<meta ' . \rtrim($part) . '>' . \PHP_EOL; |
| 76 | } |
| 77 | return $content; |
| 78 | } |
| 79 | |
| 80 | public function getTitle() : string |
| 81 | { |
| 82 | return $this->title; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @param string $title |
| 87 | * |
| 88 | * @return $this |
| 89 | */ |
| 90 | public function setTitle(string $title) |
| 91 | { |
| 92 | $this->title = $title; |
| 93 | return $this; |
| 94 | } |
| 95 | |
| 96 | public function renderTitle() : string |
| 97 | { |
| 98 | return '<title>' . $this->escape($this->getTitle()) . '</title>' . \PHP_EOL; |
| 99 | } |
| 100 | |
| 101 | public function getBody() : string |
| 102 | { |
| 103 | return $this->body; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @param string $body |
| 108 | * |
| 109 | * @return $this |
| 110 | */ |
| 111 | public function setBody(string $body) |
| 112 | { |
| 113 | $this->body = $body; |
| 114 | return $this; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @return array|string[] |
| 119 | */ |
| 120 | public function getStyles() : array |
| 121 | { |
| 122 | return $this->styles; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @param array|string[] $styles |
| 127 | * |
| 128 | * @return $this |
| 129 | */ |
| 130 | public function setStyles(array $styles) |
| 131 | { |
| 132 | $this->styles = $styles; |
| 133 | return $this; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param array|string[] $styles |
| 138 | * |
| 139 | * @return $this |
| 140 | */ |
| 141 | public function appendStyles(array $styles) |
| 142 | { |
| 143 | \array_push($this->styles, ...$styles); |
| 144 | return $this; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @param array|string[] $styles |
| 149 | * |
| 150 | * @return $this |
| 151 | */ |
| 152 | public function prependStyles(array $styles) |
| 153 | { |
| 154 | \array_unshift($this->styles, ...$styles); |
| 155 | return $this; |
| 156 | } |
| 157 | |
| 158 | public function renderStyles() : string |
| 159 | { |
| 160 | $content = ''; |
| 161 | foreach ($this->getStyles() as $style) { |
| 162 | $content .= '<link rel="stylesheet" type="text/css" href="' . $this->escape($style) . '">' . \PHP_EOL; |
| 163 | } |
| 164 | return $content; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @return array|string[] |
| 169 | */ |
| 170 | public function getScripts() : array |
| 171 | { |
| 172 | return $this->scripts; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @param array|string[] $scripts |
| 177 | * |
| 178 | * @return $this |
| 179 | */ |
| 180 | public function setScripts(array $scripts) |
| 181 | { |
| 182 | $this->scripts = $scripts; |
| 183 | return $this; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @param array|string[] $scripts |
| 188 | * |
| 189 | * @return $this |
| 190 | */ |
| 191 | public function appendScripts(array $scripts) |
| 192 | { |
| 193 | \array_push($this->scripts, ...$scripts); |
| 194 | return $this; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @param array|string[] $scripts |
| 199 | * |
| 200 | * @return $this |
| 201 | */ |
| 202 | public function prependScripts(array $scripts) |
| 203 | { |
| 204 | \array_unshift($this->scripts, ...$scripts); |
| 205 | return $this; |
| 206 | } |
| 207 | |
| 208 | public function renderScripts() : string |
| 209 | { |
| 210 | $content = ''; |
| 211 | foreach ($this->getScripts() as $script) { |
| 212 | $content .= '<script src="' . $this->escape($script) . '"></script>' . \PHP_EOL; |
| 213 | } |
| 214 | return $content; |
| 215 | } |
| 216 | |
| 217 | public function escape(?string $text, string $encoding = 'UTF-8') : string |
| 218 | { |
| 219 | $text = (string) $text; |
| 220 | return empty($text) |
| 221 | ? $text |
| 222 | : \htmlspecialchars($text, \ENT_QUOTES | \ENT_HTML5, $encoding); |
| 223 | } |
| 224 | } |