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