1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
namespace Suricate; |
3
|
|
|
|
4
|
|
|
class Page |
5
|
|
|
{ |
6
|
|
|
protected $title; |
7
|
|
|
protected $encoding = 'utf-8'; |
8
|
|
|
protected $language = 'en_US'; |
9
|
|
|
protected $stylesheets = []; |
10
|
|
|
protected $metas = []; |
11
|
|
|
protected $scripts = []; |
12
|
|
|
protected $rss = []; |
13
|
|
|
protected $htmlClass = []; |
14
|
|
|
|
15
|
5 |
|
public function __construct() |
16
|
|
|
{ |
17
|
5 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Set language passed to html tag |
21
|
|
|
* |
22
|
|
|
* @param string $language language to set |
23
|
|
|
* @return Page |
24
|
|
|
*/ |
25
|
1 |
|
public function setLanguage(string $language): Page |
26
|
|
|
{ |
27
|
1 |
|
$this->language = $language; |
28
|
|
|
|
29
|
1 |
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Set encoding passed to html and rss tags |
34
|
|
|
* |
35
|
|
|
* @param string $encoding Encoding to set |
36
|
|
|
* @return Page |
37
|
|
|
*/ |
38
|
1 |
|
public function setEncoding(string $encoding): Page |
39
|
|
|
{ |
40
|
1 |
|
$this->encoding = $encoding; |
41
|
|
|
|
42
|
1 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set title of the page |
47
|
|
|
* |
48
|
|
|
* @param string $title Title of the page |
49
|
|
|
* @return Page |
50
|
|
|
*/ |
51
|
2 |
|
public function setTitle(string $title): Page |
52
|
|
|
{ |
53
|
2 |
|
$this->title = $title; |
54
|
|
|
|
55
|
2 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// |
59
|
|
|
// Stylesheets |
60
|
|
|
// |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add a stylesheet |
64
|
|
|
* @param string $identifier Unique stylesheet identifier |
65
|
|
|
* @param string $url Stylesheet URL |
66
|
|
|
* @param string $media Stylesheet media (default: all) |
67
|
|
|
* @return Page |
68
|
|
|
*/ |
69
|
2 |
|
public function addStylesheet(string $identifier, string $url, string $media = 'all'): Page |
70
|
|
|
{ |
71
|
2 |
|
$this->stylesheets[$identifier] = [ |
72
|
2 |
|
'url' => $url, |
73
|
2 |
|
'media' => $media |
74
|
|
|
]; |
75
|
|
|
|
76
|
2 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Render stylesheets html tags |
81
|
|
|
* @return string Stylesheet HTML |
82
|
|
|
*/ |
83
|
1 |
|
protected function renderStylesheets() |
84
|
|
|
{ |
85
|
1 |
|
$output = ''; |
86
|
1 |
|
foreach ($this->stylesheets as $id => $stylesheet) { |
87
|
1 |
|
$output .= '<link rel="stylesheet"'; |
88
|
1 |
|
$output .= ' id="' . $id . '"'; |
89
|
1 |
|
$output .= ' href="' . $stylesheet['url'] . '"'; |
90
|
1 |
|
$output .= ' type="text/css"'; |
91
|
1 |
|
$output .= ' media="' . $stylesheet['media'] . '"'; |
92
|
1 |
|
$output .= '/>' . "\n"; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return $output; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
public function addHtmlClass($className) |
99
|
|
|
{ |
100
|
1 |
|
$this->htmlClass[$className] = true; |
101
|
|
|
|
102
|
1 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Add a RSS Feed |
107
|
|
|
* @param string $id Unique stylesheet identifier |
108
|
|
|
* @param string $url Feed URL |
109
|
|
|
* @param string $title Title of the feed |
110
|
|
|
*/ |
111
|
1 |
|
public function addRss($id, $url, $title) |
112
|
|
|
{ |
113
|
1 |
|
$this->rss[$id] = ['url' => $url, 'title' => $title]; |
114
|
|
|
|
115
|
1 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
protected function renderRss() |
119
|
|
|
{ |
120
|
1 |
|
$output = ''; |
121
|
1 |
|
foreach ($this->rss as $id => $rss) { |
122
|
1 |
|
$output .= '<link rel="alternate"'; |
123
|
1 |
|
$output .= ' id="' . $id . '"'; |
124
|
1 |
|
$output .= ' href="' . $rss['url'] . '"'; |
125
|
1 |
|
$output .= ' type="application/rss+xml"'; |
126
|
1 |
|
$output .= ' media="' . htmlentities($rss['title'], ENT_COMPAT, $this->encoding) . '"'; |
127
|
1 |
|
$output .= '/>' . "\n"; |
128
|
|
|
} |
129
|
1 |
|
return $output; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// |
133
|
|
|
// Scripts |
134
|
|
|
// |
135
|
1 |
|
public function addScript($id, $url) |
136
|
|
|
{ |
137
|
1 |
|
$this->scripts[$id] = $url; |
138
|
|
|
|
139
|
1 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
protected function renderScripts() |
143
|
|
|
{ |
144
|
1 |
|
$output = ''; |
145
|
|
|
|
146
|
1 |
|
foreach ($this->scripts as $currentScriptUrl) { |
147
|
1 |
|
$output .= '<script type="text/javascript" src="' . $currentScriptUrl . '"></script>' . "\n"; |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
return $output; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// |
154
|
|
|
// Metas |
155
|
|
|
// |
156
|
1 |
|
public function addMeta($name, $content) |
157
|
|
|
{ |
158
|
1 |
|
$this->metas[$name] = ['content' => $content, 'type' => 'name']; |
159
|
|
|
|
160
|
1 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
public function addMetaProperty($name, $content) |
164
|
|
|
{ |
165
|
1 |
|
$this->metas[$name] = ['content' => $content, 'type' => 'property']; |
166
|
1 |
|
} |
167
|
|
|
|
168
|
1 |
|
public function addMetaLink($name, $type, $href) |
169
|
|
|
{ |
170
|
1 |
|
$this->metas[$name] = ['href' => $href, 'type' => 'rel', 'relType' => $type]; |
171
|
1 |
|
} |
172
|
|
|
|
173
|
1 |
|
protected function renderMetas() |
174
|
|
|
{ |
175
|
1 |
|
$output = ''; |
176
|
1 |
|
foreach ($this->metas as $name => $metaData) { |
177
|
1 |
|
if ($metaData['type'] == 'name') { |
178
|
1 |
|
$output .= '<meta name="' . $name . '" content="' . $metaData['content'] . '"/>' . "\n"; |
179
|
1 |
|
} elseif ($metaData['type'] == 'property') { |
180
|
1 |
|
$output .= '<meta property="' . $name . '" content="' . $metaData['content'] . '"/>' . "\n"; |
181
|
1 |
|
} elseif ($metaData['type'] == 'rel') { |
182
|
1 |
|
$output .= '<link rel="' . $metaData['relType'] . '" href="' . $metaData['href'] . '"/>'."\n"; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
return $output; |
187
|
|
|
} |
188
|
|
|
|
189
|
1 |
|
public function render($content = '') |
190
|
|
|
{ |
191
|
1 |
|
$htmlClass = count($this->htmlClass) ? ' class="' . implode(' ', array_keys($this->htmlClass)) .'"' : ''; |
192
|
1 |
|
$output = '<!DOCTYPE html>' . "\n"; |
193
|
1 |
|
$output .= '<html lang="' . substr($this->language, 0, 2) . '"' . $htmlClass . '>' . "\n"; |
194
|
1 |
|
$output .= '<head>' . "\n"; |
195
|
1 |
|
$output .= '<title>' . htmlentities((string) $this->title, ENT_COMPAT, $this->encoding) . '</title>' . "\n"; |
196
|
1 |
|
$output .= '<meta http-equiv="Content-Type" content="text/html; charset=' . $this->encoding . '" />'."\n"; |
197
|
1 |
|
$output .= $this->renderMetas(); |
198
|
1 |
|
$output .= $this->renderStylesheets(); |
199
|
1 |
|
$output .= $this->renderScripts(); |
200
|
1 |
|
$output .= $this->renderRss(); |
201
|
1 |
|
$output .= '</head>' . "\n"; |
202
|
1 |
|
$output .= '<body>' . "\n"; |
203
|
1 |
|
$output .= $content; |
204
|
1 |
|
$output .= '</body>'."\n"; |
205
|
1 |
|
$output .= '</html>'; |
206
|
|
|
|
207
|
1 |
|
return $output; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|