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 |
|
' title="' . |
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 |
|
return $this; |
223
|
|
|
} |
224
|
1 |
|
|
225
|
1 |
|
public function addMetaLink($name, $type, $href) |
226
|
1 |
|
{ |
227
|
1 |
|
$this->metas[$name] = [ |
228
|
|
|
'href' => $href, |
229
|
1 |
|
'type' => 'rel', |
230
|
|
|
'relType' => $type |
231
|
1 |
|
]; |
232
|
|
|
|
233
|
1 |
|
return $this; |
234
|
1 |
|
} |
235
|
1 |
|
|
236
|
|
|
public function addMetaCanonical(string $url) |
237
|
|
|
{ |
238
|
1 |
|
$this->addMetaLink('canonical', 'canonical', $url); |
239
|
1 |
|
|
240
|
1 |
|
return $this; |
241
|
1 |
|
} |
242
|
1 |
|
|
243
|
1 |
|
protected function renderMetas() |
244
|
|
|
{ |
245
|
|
|
$output = ''; |
246
|
1 |
|
foreach ($this->metas as $name => $metaData) { |
247
|
1 |
|
if ($metaData['type'] == 'name') { |
248
|
1 |
|
$output .= '<meta name="' . $name . '" content="' . $metaData['content'] . '">'; |
249
|
1 |
|
} elseif ($metaData['type'] == 'property') { |
250
|
1 |
|
$output .= '<meta property="' . $name . '" content="' . $metaData['content'] . '">'; |
251
|
1 |
|
} elseif ($metaData['type'] == 'rel') { |
252
|
|
|
$output .= '<link rel="' . $metaData['relType'] . '" href="' . $metaData['href'] . '">'; |
253
|
|
|
} |
254
|
1 |
|
} |
255
|
1 |
|
|
256
|
1 |
|
return $output; |
257
|
1 |
|
} |
258
|
1 |
|
|
259
|
|
|
/** |
260
|
|
|
* Add a raw html entry to be render in <head> |
261
|
|
|
* |
262
|
1 |
|
* @param string $name |
263
|
|
|
* @param string $content |
264
|
|
|
* |
265
|
1 |
|
* @return static |
266
|
|
|
*/ |
267
|
1 |
|
public function addRawHead(string $name, string $content): static |
268
|
1 |
|
{ |
269
|
1 |
|
$this->rawHead[$name] = $content; |
270
|
1 |
|
|
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
1 |
|
|
274
|
1 |
|
/** |
275
|
1 |
|
* Render raw head entries |
276
|
1 |
|
* |
277
|
1 |
|
* @return string |
278
|
1 |
|
*/ |
279
|
|
|
public function renderRawHead(): string |
280
|
|
|
{ |
281
|
1 |
|
$output = ''; |
282
|
1 |
|
|
283
|
1 |
|
foreach ($this->rawHead as $currentEntry) { |
284
|
|
|
$output .= $currentEntry; |
285
|
|
|
} |
286
|
1 |
|
|
287
|
1 |
|
return $output; |
288
|
1 |
|
} |
289
|
1 |
|
|
290
|
1 |
|
public function render($content = '') |
291
|
1 |
|
{ |
292
|
1 |
|
$htmlClass = count($this->htmlClass) |
293
|
1 |
|
? ' class="' . implode(' ', array_keys($this->htmlClass)) . '"' |
294
|
1 |
|
: ''; |
295
|
1 |
|
$output = '<!DOCTYPE html>'; |
296
|
1 |
|
$output .= '<html lang="' . substr($this->language, 0, 2) . '"' . $htmlClass . '>'; |
297
|
1 |
|
$output .= '<head>'; |
298
|
|
|
$output .= '<title>' . htmlentities((string) $this->title, ENT_COMPAT, $this->encoding) . '</title>'; |
299
|
1 |
|
$output .= '<meta http-equiv="Content-Type" content="text/html; charset=' . $this->encoding . '">'; |
300
|
|
|
$output .= $this->renderMetas(); |
301
|
|
|
$output .= $this->renderStylesheets(); |
302
|
|
|
$output .= $this->renderScripts(); |
303
|
|
|
$output .= $this->renderRss(); |
304
|
|
|
$output .= $this->renderRawHead(); |
305
|
|
|
$output .= '</head>'; |
306
|
|
|
$output .= '<body>'; |
307
|
|
|
$output .= $content; |
308
|
|
|
$output .= '</body>'; |
309
|
|
|
$output .= '</html>'; |
310
|
|
|
|
311
|
|
|
return $output; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|