Passed
Push — master ( f5262c...2df108 )
by Mathieu
13:21
created

Page::addRawHead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate;
6
7
class Page
8
{
9
    protected $title;
10
    protected $encoding = 'utf-8';
11
    protected $language = 'en_US';
12
    protected $stylesheets = [];
13
    protected $metas = [];
14
    protected $scripts = [];
15
    protected $rawHead = [];
16
    protected $rss = [];
17
    protected $htmlClass = [];
18 5
19
    public function __construct()
20 5
    {
21
    }
22
23
    /**
24
     * Set language passed to html tag
25
     *
26
     * @param string $language language to set
27
     * @return Page
28 1
     */
29
    public function setLanguage(string $language): Page
30 1
    {
31
        $this->language = $language;
32 1
33
        return $this;
34
    }
35
36
    /**
37
     * Get language passed to html tag
38
     *
39
     * @return string
40 1
     */
41
    public function getLanguage(): string
42 1
    {
43
        return $this->language;
44
    }
45
46
    /**
47
     * Set encoding passed to html and rss tags
48
     *
49
     * @param string $encoding Encoding to set
50
     * @return Page
51 1
     */
52
    public function setEncoding(string $encoding): Page
53 1
    {
54
        $this->encoding = $encoding;
55 1
56
        return $this;
57
    }
58
59
    /**
60
     * Get encoding passed to html and rss tags
61
     *
62
     * @return string
63 1
     */
64
    public function getEncoding(): string
65 1
    {
66
        return $this->encoding;
67
    }
68
69
    /**
70
     * Set title of the page
71
     *
72
     * @param string $title Title of the page
73
     * @return Page
74 2
     */
75
    public function setTitle(string $title): Page
76 2
    {
77
        $this->title = $title;
78 2
79
        return $this;
80
    }
81
82
    /**
83
     * Get title of the page
84
     *
85
     * @return string
86 1
     */
87
    public function getTitle(): string
88 1
    {
89
        return $this->title;
90
    }
91
92
    /**
93
     * Add a stylesheet
94
     * @param string $identifier Unique stylesheet identifier
95
     * @param string $url        Stylesheet URL
96
     * @param string $media      Stylesheet media (default: all)
97
     * @return Page
98 2
     */
99
    public function addStylesheet(
100
        string $identifier,
101
        string $url,
102
        string $media = 'all'
103 2
    ): Page {
104 2
        $this->stylesheets[$identifier] = [
105 2
            'url' => $url,
106
            'media' => $media
107
        ];
108 2
109
        return $this;
110
    }
111
112
    /**
113
     * Render stylesheets html tags
114
     * @return string Stylesheet HTML
115 1
     */
116
    protected function renderStylesheets()
117 1
    {
118 1
        $output = '';
119 1
        foreach ($this->stylesheets as $id => $stylesheet) {
120 1
            $output .= '<link rel="stylesheet"';
121 1
            $output .= ' id="' . $id . '"';
122 1
            $output .= ' href="' . $stylesheet['url'] . '"';
123 1
            $output .= ' type="text/css"';
124 1
            $output .= ' media="' . $stylesheet['media'] . '"';
125
            $output .= '/>' . "\n";
126
        }
127 1
128
        return $output;
129
    }
130 1
131
    public function addHtmlClass($className)
132 1
    {
133
        $this->htmlClass[$className] = true;
134 1
135
        return $this;
136
    }
137
138
    /**
139
     * Add a RSS Feed
140
     * @param string $id  Unique stylesheet identifier
141
     * @param string $url Feed URL
142
     * @param string $title Title of the feed
143 1
     */
144
    public function addRss($id, $url, $title)
145 1
    {
146
        $this->rss[$id] = ['url' => $url, 'title' => $title];
147 1
148
        return $this;
149
    }
150 1
151
    protected function renderRss()
152 1
    {
153 1
        $output = '';
154 1
        foreach ($this->rss as $id => $rss) {
155 1
            $output .= '<link rel="alternate"';
156 1
            $output .= ' id="' . $id . '"';
157 1
            $output .= ' href="' . $rss['url'] . '"';
158
            $output .= ' type="application/rss+xml"';
159
            $output .=
160 1
                ' media="' .
161 1
                htmlentities($rss['title'], ENT_COMPAT, $this->encoding) .
162 1
                '"';
163
            $output .= '/>' . "\n";
164 1
        }
165
        return $output;
166
    }
167
168
    //
169
    // Scripts
170
    //
171
    /**
172
     * Add script tag in header
173
     *
174
     * @param string $id
175
     * @param string $url
176
     * @param boolean $async
177
     * @param boolean $defer
178
     * @return Page
179 1
     */
180
    public function addScript($id, $url, $async = false, $defer = false)
181 1
    {
182 1
        $this->scripts[$id] = [
183 1
            'url' => $url,
184 1
            'async' => $async,
185
            'defer' => $defer
186
        ];
187 1
188
        return $this;
189
    }
190 1
191
    protected function renderScripts()
192 1
    {
193
        $output = '';
194 1
195
        foreach ($this->scripts as $currentScript) {
196 1
            $output .=
197 1
                sprintf(
198 1
                    '<script type="text/javascript" src="%s"%s%s></script>' . "\n",
199 1
                    $currentScript['url'],
200
                    $currentScript['async'] ? ' async' : '',
201
                    $currentScript['defer'] ? ' defer' : '',
202
                );
203
        }
204 1
205
        return $output;
206
    }
207
208
    //
209
    // Metas
210 1
    //
211
    public function addMeta($name, $content)
212 1
    {
213
        $this->metas[$name] = ['content' => $content, 'type' => 'name'];
214 1
215
        return $this;
216
    }
217 1
218
    public function addMetaProperty($name, $content)
219 1
    {
220 1
        $this->metas[$name] = ['content' => $content, 'type' => 'property'];
221
    }
222 1
223
    public function addMetaLink($name, $type, $href)
224 1
    {
225 1
        $this->metas[$name] = [
226 1
            'href' => $href,
227 1
            'type' => 'rel',
228
            'relType' => $type
229 1
        ];
230
    }
231 1
232
    protected function renderMetas()
233 1
    {
234 1
        $output = '';
235 1
        foreach ($this->metas as $name => $metaData) {
236
            if ($metaData['type'] == 'name') {
237
                $output .=
238 1
                    '<meta name="' .
239 1
                    $name .
240 1
                    '" content="' .
241 1
                    $metaData['content'] .
242 1
                    '"/>' .
243 1
                    "\n";
244
            } elseif ($metaData['type'] == 'property') {
245
                $output .=
246 1
                    '<meta property="' .
247 1
                    $name .
248 1
                    '" content="' .
249 1
                    $metaData['content'] .
250 1
                    '"/>' .
251 1
                    "\n";
252
            } elseif ($metaData['type'] == 'rel') {
253
                $output .=
254 1
                    '<link rel="' .
255 1
                    $metaData['relType'] .
256 1
                    '" href="' .
257 1
                    $metaData['href'] .
258 1
                    '"/>' .
259
                    "\n";
260
            }
261
        }
262 1
263
        return $output;
264
    }
265 1
266
    /**
267 1
     * Add a raw html entry to be render in <head>
268 1
     *
269 1
     * @param string $name
270 1
     * @param string $content
271
     *
272
     * @return static
273 1
     */
274 1
    public function addRawHead(string $name, string $content): static
275 1
    {
276 1
        $this->rawHead[$name] = $content;
277 1
278 1
        return $this;
279
    }
280
281 1
    /**
282 1
     * Render raw head entries
283 1
     *
284
     * @return string
285
     */
286 1
    public function renderRawHead(): string
287 1
    {
288 1
        $output = '';
289 1
290 1
        foreach ($this->rawHead as $currentEntry) {
291 1
            $output .= $currentEntry;
292 1
        }
293 1
294 1
        return $output;
295 1
    }
296 1
297 1
    public function render($content = '')
298
    {
299 1
        $htmlClass = count($this->htmlClass)
300
            ? ' class="' . implode(' ', array_keys($this->htmlClass)) . '"'
301
            : '';
302
        $output = '<!DOCTYPE html>' . "\n";
303
        $output .=
304
            '<html lang="' .
305
            substr($this->language, 0, 2) .
306
            '"' .
307
            $htmlClass .
308
            '>' .
309
            "\n";
310
        $output .= '<head>' . "\n";
311
        $output .=
312
            '<title>' .
313
            htmlentities((string) $this->title, ENT_COMPAT, $this->encoding) .
314
            '</title>' .
315
            "\n";
316
        $output .=
317
            '<meta http-equiv="Content-Type" content="text/html; charset=' .
318
            $this->encoding .
319
            '" />' .
320
            "\n";
321
        $output .= $this->renderMetas();
322
        $output .= $this->renderStylesheets();
323
        $output .= $this->renderScripts();
324
        $output .= $this->renderRss();
325
        $output .= $this->renderRawHead();
326
        $output .= '</head>' . "\n";
327
        $output .= '<body>' . "\n";
328
        $output .= $content;
329
        $output .= '</body>' . "\n";
330
        $output .= '</html>';
331
332
        return $output;
333
    }
334
}
335