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