1
|
|
|
<?php namespace Arcanedev\SeoHelper; |
2
|
|
|
|
3
|
|
|
use Arcanedev\SeoHelper\Contracts\SeoMeta as SeoMetaContract; |
4
|
|
|
use Arcanedev\Support\Traits\Configurable; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class SeoMeta |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\SeoHelper |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class SeoMeta implements SeoMetaContract |
13
|
|
|
{ |
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
15
|
|
|
| Traits |
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
17
|
|
|
*/ |
18
|
|
|
use Configurable; |
19
|
|
|
|
20
|
|
|
/* ------------------------------------------------------------------------------------------------ |
21
|
|
|
| Properties |
22
|
|
|
| ------------------------------------------------------------------------------------------------ |
23
|
|
|
*/ |
24
|
|
|
/** |
25
|
|
|
* Current URL. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $currentUrl = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The Title instance. |
33
|
|
|
* |
34
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\Entities\TitleInterface |
35
|
|
|
*/ |
36
|
|
|
protected $title; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The Description instance. |
40
|
|
|
* |
41
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\Entities\DescriptionInterface |
42
|
|
|
*/ |
43
|
|
|
protected $description; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The Keywords instance. |
47
|
|
|
* |
48
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\Entities\KeywordsInterface |
49
|
|
|
*/ |
50
|
|
|
protected $keywords; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The MiscTags instance. |
54
|
|
|
* |
55
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\Entities\MiscTagsInterface |
56
|
|
|
*/ |
57
|
|
|
protected $misc; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The Webmasters instance. |
61
|
|
|
* |
62
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\Entities\WebmastersInterface |
63
|
|
|
*/ |
64
|
|
|
protected $webmasters; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The Analytics instance. |
68
|
|
|
* |
69
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\Entities\AnalyticsInterface |
70
|
|
|
*/ |
71
|
|
|
protected $analytics; |
72
|
|
|
|
73
|
|
|
/* ------------------------------------------------------------------------------------------------ |
74
|
|
|
| Constructor |
75
|
|
|
| ------------------------------------------------------------------------------------------------ |
76
|
|
|
*/ |
77
|
|
|
/** |
78
|
|
|
* Make SeoMeta instance. |
79
|
|
|
* |
80
|
|
|
* @param array $configs |
81
|
|
|
*/ |
82
|
324 |
|
public function __construct(array $configs) |
83
|
|
|
{ |
84
|
324 |
|
$this->setConfigs($configs); |
85
|
324 |
|
$this->init(); |
86
|
324 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Start the engine. |
90
|
|
|
*/ |
91
|
324 |
|
private function init() |
92
|
|
|
{ |
93
|
324 |
|
$this->title( |
94
|
324 |
|
new Entities\Title($this->getConfig('title', [])) |
95
|
243 |
|
); |
96
|
324 |
|
$this->description( |
97
|
324 |
|
new Entities\Description($this->getConfig('description', [])) |
98
|
243 |
|
); |
99
|
324 |
|
$this->keywords( |
100
|
324 |
|
new Entities\Keywords($this->getConfig('keywords', [])) |
101
|
243 |
|
); |
102
|
324 |
|
$this->misc( |
103
|
324 |
|
new Entities\MiscTags($this->getConfig('misc', [])) |
104
|
243 |
|
); |
105
|
324 |
|
$this->webmasters( |
106
|
324 |
|
new Entities\Webmasters($this->getConfig('webmasters', [])) |
107
|
243 |
|
); |
108
|
324 |
|
$this->analytics( |
109
|
324 |
|
new Entities\Analytics($this->getConfig('analytics', [])) |
110
|
243 |
|
); |
111
|
324 |
|
} |
112
|
|
|
|
113
|
|
|
/* ------------------------------------------------------------------------------------------------ |
114
|
|
|
| Getters & Setters |
115
|
|
|
| ------------------------------------------------------------------------------------------------ |
116
|
|
|
*/ |
117
|
|
|
/** |
118
|
|
|
* Set the Title instance. |
119
|
|
|
* |
120
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\Entities\TitleInterface $title |
121
|
|
|
* |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
324 |
|
public function title(Contracts\Entities\TitleInterface $title) |
125
|
|
|
{ |
126
|
324 |
|
$this->title = $title; |
127
|
|
|
|
128
|
324 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set the Description instance. |
133
|
|
|
* |
134
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\Entities\DescriptionInterface $description |
135
|
|
|
* |
136
|
|
|
* @return self |
137
|
|
|
*/ |
138
|
324 |
|
public function description(Contracts\Entities\DescriptionInterface $description) { |
139
|
324 |
|
$this->description = $description; |
140
|
|
|
|
141
|
324 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the Keywords instance. |
146
|
|
|
* |
147
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\Entities\KeywordsInterface $keywords |
148
|
|
|
* |
149
|
|
|
* @return self |
150
|
|
|
*/ |
151
|
324 |
|
public function keywords(Contracts\Entities\KeywordsInterface $keywords) |
152
|
|
|
{ |
153
|
324 |
|
$this->keywords = $keywords; |
154
|
|
|
|
155
|
324 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set the MiscTags instance. |
160
|
|
|
* |
161
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\Entities\MiscTagsInterface $misc |
162
|
|
|
* |
163
|
|
|
* @return self |
164
|
|
|
*/ |
165
|
324 |
|
public function misc(Contracts\Entities\MiscTagsInterface $misc) |
166
|
|
|
{ |
167
|
324 |
|
$this->misc = $misc; |
168
|
|
|
|
169
|
324 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set the Webmasters instance. |
174
|
|
|
* |
175
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\Entities\WebmastersInterface $webmasters |
176
|
|
|
* |
177
|
|
|
* @return self |
178
|
|
|
*/ |
179
|
324 |
|
public function webmasters(Contracts\Entities\WebmastersInterface $webmasters) |
180
|
|
|
{ |
181
|
324 |
|
$this->webmasters = $webmasters; |
182
|
|
|
|
183
|
324 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set the Analytics instance. |
188
|
|
|
* |
189
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\Entities\AnalyticsInterface $analytics |
190
|
|
|
* |
191
|
|
|
* @return self |
192
|
|
|
*/ |
193
|
324 |
|
private function analytics(Contracts\Entities\AnalyticsInterface $analytics) |
194
|
|
|
{ |
195
|
324 |
|
$this->analytics = $analytics; |
196
|
|
|
|
197
|
324 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Set the title. |
202
|
|
|
* |
203
|
|
|
* @param string $title |
204
|
|
|
* @param string $siteName |
205
|
|
|
* @param string $separator |
206
|
|
|
* |
207
|
|
|
* @return self |
208
|
|
|
*/ |
209
|
36 |
|
public function setTitle($title, $siteName = null, $separator = null) |
210
|
|
|
{ |
211
|
36 |
|
$this->title->set($title); |
212
|
|
|
|
213
|
36 |
|
if ( ! is_null($siteName)) { |
214
|
36 |
|
$this->title->setSiteName($siteName); |
215
|
27 |
|
} |
216
|
|
|
|
217
|
36 |
|
if ( ! is_null($separator)) { |
218
|
36 |
|
$this->title->setSeparator($separator); |
219
|
27 |
|
} |
220
|
|
|
|
221
|
36 |
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Set the description content. |
226
|
|
|
* |
227
|
|
|
* @param string $content |
228
|
|
|
* |
229
|
|
|
* @return self |
230
|
|
|
*/ |
231
|
36 |
|
public function setDescription($content) |
232
|
|
|
{ |
233
|
36 |
|
$this->description->set($content); |
234
|
|
|
|
235
|
36 |
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Set the keywords content. |
240
|
|
|
* |
241
|
|
|
* @param array|string $content |
242
|
|
|
* |
243
|
|
|
* @return self |
244
|
|
|
*/ |
245
|
60 |
|
public function setKeywords($content) |
246
|
|
|
{ |
247
|
60 |
|
$this->keywords->set($content); |
248
|
|
|
|
249
|
60 |
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Add a keyword. |
254
|
|
|
* |
255
|
|
|
* @param string $keyword |
256
|
|
|
* |
257
|
|
|
* @return self |
258
|
|
|
*/ |
259
|
12 |
|
public function addKeyword($keyword) |
260
|
|
|
{ |
261
|
12 |
|
$this->keywords->add($keyword); |
262
|
|
|
|
263
|
12 |
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Add many keywords. |
268
|
|
|
* |
269
|
|
|
* @param array $keywords |
270
|
|
|
* |
271
|
|
|
* @return self |
272
|
|
|
*/ |
273
|
12 |
|
public function addKeywords(array $keywords) |
274
|
|
|
{ |
275
|
12 |
|
$this->keywords->addMany($keywords); |
276
|
|
|
|
277
|
12 |
|
return $this; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Add a webmaster tool site verifier. |
282
|
|
|
* |
283
|
|
|
* @param string $webmaster |
284
|
|
|
* @param string $content |
285
|
|
|
* |
286
|
|
|
* @return self |
287
|
|
|
*/ |
288
|
12 |
|
public function addWebmaster($webmaster, $content) |
289
|
|
|
{ |
290
|
12 |
|
$this->webmasters->add($webmaster, $content); |
291
|
|
|
|
292
|
12 |
|
return $this; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Set the current URL. |
297
|
|
|
* |
298
|
|
|
* @param string $url |
299
|
|
|
* |
300
|
|
|
* @return self |
301
|
|
|
*/ |
302
|
120 |
|
public function setUrl($url) |
303
|
|
|
{ |
304
|
120 |
|
$this->currentUrl = $url; |
305
|
120 |
|
$this->misc->setUrl($url); |
306
|
|
|
|
307
|
120 |
|
return $this; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Set the Google Analytics code. |
312
|
|
|
* |
313
|
|
|
* @param string $code |
314
|
|
|
* |
315
|
|
|
* @return self |
316
|
|
|
*/ |
317
|
12 |
|
public function setGoogleAnalytics($code) |
318
|
|
|
{ |
319
|
12 |
|
$this->analytics->setGoogle($code); |
320
|
|
|
|
321
|
12 |
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/* ------------------------------------------------------------------------------------------------ |
325
|
|
|
| Main Functions |
326
|
|
|
| ------------------------------------------------------------------------------------------------ |
327
|
|
|
*/ |
328
|
|
|
/** |
329
|
|
|
* Add a meta tag. |
330
|
|
|
* |
331
|
|
|
* @param string $name |
332
|
|
|
* @param string $content |
333
|
|
|
* |
334
|
|
|
* @return self |
335
|
|
|
*/ |
336
|
12 |
|
public function addMeta($name, $content) |
337
|
|
|
{ |
338
|
12 |
|
$this->misc->add($name, $content); |
339
|
|
|
|
340
|
12 |
|
return $this; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Add many meta tags. |
345
|
|
|
* |
346
|
|
|
* @param array $metas |
347
|
|
|
* |
348
|
|
|
* @return self |
349
|
|
|
*/ |
350
|
12 |
|
public function addMetas(array $metas) |
351
|
|
|
{ |
352
|
12 |
|
$this->misc->addMany($metas); |
353
|
|
|
|
354
|
12 |
|
return $this; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Remove a meta from the meta collection by key. |
359
|
|
|
* |
360
|
|
|
* @param string|array $names |
361
|
|
|
* |
362
|
|
|
* @return self |
363
|
|
|
*/ |
364
|
12 |
|
public function removeMeta($names) |
365
|
|
|
{ |
366
|
12 |
|
$this->misc->remove($names); |
367
|
|
|
|
368
|
12 |
|
return $this; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Reset the meta collection except the description and keywords metas. |
373
|
|
|
* |
374
|
|
|
* @return self |
375
|
|
|
*/ |
376
|
12 |
|
public function resetMetas() |
377
|
|
|
{ |
378
|
12 |
|
$this->misc->reset(); |
379
|
|
|
|
380
|
12 |
|
return $this; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Reset all webmaster tool site verifier metas. |
385
|
|
|
* |
386
|
|
|
* @return self |
387
|
|
|
*/ |
388
|
12 |
|
public function resetWebmasters() |
389
|
|
|
{ |
390
|
12 |
|
$this->webmasters->reset(); |
391
|
|
|
|
392
|
12 |
|
return $this; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Render all seo tags. |
397
|
|
|
* |
398
|
|
|
* @return string |
399
|
|
|
*/ |
400
|
228 |
|
public function render() |
401
|
|
|
{ |
402
|
228 |
|
return implode(PHP_EOL, array_filter([ |
403
|
228 |
|
$this->title->render(), |
404
|
228 |
|
$this->description->render(), |
405
|
228 |
|
$this->keywords->render(), |
406
|
228 |
|
$this->misc->render(), |
407
|
228 |
|
$this->webmasters->render(), |
408
|
228 |
|
$this->analytics->render(), |
409
|
171 |
|
])); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Render all seo tags. |
414
|
|
|
* |
415
|
|
|
* @return string |
416
|
|
|
*/ |
417
|
96 |
|
public function __toString() |
418
|
|
|
{ |
419
|
96 |
|
return $this->render(); |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|