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