1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Helpers\View; |
4
|
|
|
|
5
|
|
|
use Nip\Config\Config; |
6
|
|
|
use stdClass; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Nip Framework |
10
|
|
|
* |
11
|
|
|
* @category Nip |
12
|
|
|
* @copyright 2009 Nip Framework |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
14
|
|
|
* @version SVN: $Id: Meta.php 60 2009-04-28 14:50:04Z victor.stanciu $ |
15
|
|
|
*/ |
16
|
|
|
class Meta extends AbstractHelper |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
public $charset = 'utf-8'; |
20
|
|
|
public $language = 'en'; |
21
|
|
|
|
22
|
|
|
public $authors = []; |
23
|
|
|
public $publisher; |
24
|
|
|
public $copyright; |
25
|
|
|
|
26
|
|
|
public $robots = 'index,follow'; |
27
|
|
|
|
28
|
|
|
public $title = false; |
29
|
|
|
public $titleComponents = [ |
30
|
|
|
'base' => false, |
31
|
|
|
'elements' => [], |
32
|
|
|
'separator' => ' - ', |
33
|
|
|
|
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
public $keywords = []; |
37
|
|
|
public $description = false; |
38
|
|
|
public $descriptionComponents = []; |
39
|
|
|
public $verify_v1 = false; |
40
|
|
|
public $feeds = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Config $config |
44
|
|
|
*/ |
45
|
|
|
public function populateFromConfig($config) |
46
|
|
|
{ |
47
|
|
|
$this->setTitleBase($config->get('title')); |
48
|
|
|
$this->authors = explode(",", $config->get('authors')); |
49
|
|
|
$this->description = $config->get('description'); |
50
|
|
|
$this->addKeywords(explode(",", $config->get('keywords'))); |
51
|
|
|
$this->copyright = $config->get('copyright'); |
52
|
|
|
$this->robots = $config->get('robots'); |
53
|
|
|
$this->verify_v1 = $config->get('verify_v1'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $base |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setTitleBase($base) |
61
|
|
|
{ |
62
|
|
|
$this->titleComponents['base'] = $base; |
63
|
|
|
$this->generateTitle(); |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function generateTitle() |
69
|
|
|
{ |
70
|
|
|
$components = $this->titleComponents['elements']; |
71
|
|
|
if ($this->titleComponents['base']) { |
72
|
|
|
$components[] = $this->titleComponents['base']; |
73
|
|
|
} |
74
|
|
|
$this->title = implode($this->titleComponents['separator'], $components); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $keywords |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function addKeywords($keywords) |
82
|
|
|
{ |
83
|
|
|
if (!is_array($keywords)) { |
84
|
|
|
$keywords = [$keywords]; |
85
|
|
|
} |
86
|
|
|
foreach ($keywords as $keyword) { |
87
|
|
|
if ($keyword) { |
88
|
|
|
array_unshift($this->keywords, $keyword); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $title |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function appendTitle($title) |
100
|
|
|
{ |
101
|
|
|
$this->titleComponents['elements'][] = $title; |
102
|
|
|
$this->generateTitle(); |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $title |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function prependTitle($title) |
112
|
|
|
{ |
113
|
|
|
array_unshift($this->titleComponents['elements'], $title); |
114
|
|
|
$this->generateTitle(); |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
public function getFirstTitle() |
123
|
|
|
{ |
124
|
|
|
$components = $this->titleComponents['elements']; |
125
|
|
|
|
126
|
|
|
return end($components); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $description |
131
|
|
|
*/ |
132
|
|
|
public function addDescription($description) |
133
|
|
|
{ |
134
|
|
|
array_unshift($this->descriptionComponents, $description); |
135
|
|
|
$this->generateDescription(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function generateDescription() |
139
|
|
|
{ |
140
|
|
|
$this->description = implode('. ', $this->descriptionComponents); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param $description |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function setDescription($description) |
148
|
|
|
{ |
149
|
|
|
if ($description) { |
150
|
|
|
$this->description = $description; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param array $feeds |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
|
public function addFeeds(array $feeds) |
161
|
|
|
{ |
162
|
|
|
foreach ($feeds as $feed) { |
163
|
|
|
$this->addFeed($feed); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $url |
171
|
|
|
* @param string $title |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function addFeed($url, $title = 'Rss') |
175
|
|
|
{ |
176
|
|
|
if (is_object($url)) { |
177
|
|
|
$feed = $url; |
178
|
|
|
} else { |
179
|
|
|
$feed = new stdClass(); |
180
|
|
|
$feed->title = $title; |
181
|
|
|
$feed->url = $url; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$this->feeds[$feed->url] = $feed; |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function __toString() |
193
|
|
|
{ |
194
|
|
|
if ($this->title) { |
195
|
|
|
$return[] = '<title>'.$this->title.'</title>'; |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$return[] = '<meta http-equiv="Content-Type" content="text/html;" />'; |
|
|
|
|
199
|
|
|
$return[] = '<meta charset="'.$this->charset.'">'; |
200
|
|
|
$return[] = '<meta http-equiv="content-language" content="'.$this->language.'" />'; |
201
|
|
|
|
202
|
|
|
$return[] = '<meta name="viewport" |
203
|
|
|
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>'; |
204
|
|
|
|
205
|
|
|
if ($this->authors) { |
|
|
|
|
206
|
|
|
$return[] = '<meta name="author" content="'.implode(", ", $this->authors).'" />'; |
207
|
|
|
} |
208
|
|
|
if ($this->publisher) { |
209
|
|
|
$return[] = '<meta name="publisher" content="'.$this->publisher.'" />'; |
210
|
|
|
} |
211
|
|
|
if ($this->copyright) { |
212
|
|
|
$return[] = '<meta name="copyright" content="'.$this->copyright.'" />'; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$return[] = '<meta name="robots" content="'.$this->robots.'" />'; |
216
|
|
|
|
217
|
|
|
if ($this->keywords) { |
|
|
|
|
218
|
|
|
$return[] = '<meta name="keywords" content="'.implode(",", $this->keywords).'" />'; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if (!empty($this->description)) { |
222
|
|
|
$return[] = '<meta name="description" content="'.$this->description.'" />'; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
if (!empty($this->verify_v1)) { |
226
|
|
|
$return[] = '<meta name="verify-v1" content="'.$this->verify_v1.'" />'; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
foreach ($this->feeds as $feed) { |
230
|
|
|
$return[] = '<link rel="alternate" |
231
|
|
|
type="application/rss+xml" title="'.$feed->title.'" href="'.$feed->url.'" />'; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// $return[] = '<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />'; |
|
|
|
|
235
|
|
|
|
236
|
|
|
return implode("\n", $return); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.