Passed
Push — master ( 89a21c...d22187 )
by Mathieu
12:43
created

Page::addStylesheet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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