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
|
224 |
|
public function __construct(array $configs) |
89
|
|
|
{ |
90
|
224 |
|
$this->setConfigs($configs); |
91
|
224 |
|
$this->init(); |
92
|
224 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Start the engine. |
96
|
|
|
*/ |
97
|
224 |
|
private function init() |
98
|
|
|
{ |
99
|
224 |
|
$this->title( |
100
|
224 |
|
new Entities\Title($this->getConfig('title', [])) |
101
|
112 |
|
); |
102
|
224 |
|
$this->description( |
103
|
224 |
|
new Entities\Description($this->getConfig('description', [])) |
104
|
112 |
|
); |
105
|
224 |
|
$this->keywords( |
106
|
224 |
|
new Entities\Keywords($this->getConfig('keywords', [])) |
107
|
112 |
|
); |
108
|
224 |
|
$this->misc( |
109
|
224 |
|
new Entities\MiscTags($this->getConfig('misc', [])) |
110
|
112 |
|
); |
111
|
224 |
|
$this->webmasters( |
112
|
224 |
|
new Entities\Webmasters($this->getConfig('webmasters', [])) |
113
|
112 |
|
); |
114
|
224 |
|
$this->analytics( |
115
|
224 |
|
new Entities\Analytics($this->getConfig('analytics', [])) |
116
|
112 |
|
); |
117
|
224 |
|
} |
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
|
224 |
|
public function title(TitleContract $title) |
131
|
|
|
{ |
132
|
224 |
|
$this->title = $title; |
133
|
|
|
|
134
|
224 |
|
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
|
224 |
|
public function description(DescriptionContract $description) { |
145
|
224 |
|
$this->description = $description; |
146
|
|
|
|
147
|
224 |
|
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
|
224 |
|
public function keywords(KeywordsContract $keywords) |
158
|
|
|
{ |
159
|
224 |
|
$this->keywords = $keywords; |
160
|
|
|
|
161
|
224 |
|
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
|
224 |
|
public function misc(MiscTagsContract $misc) |
172
|
|
|
{ |
173
|
224 |
|
$this->misc = $misc; |
174
|
|
|
|
175
|
224 |
|
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
|
224 |
|
public function webmasters(WebmastersContract $webmasters) |
186
|
|
|
{ |
187
|
224 |
|
$this->webmasters = $webmasters; |
188
|
|
|
|
189
|
224 |
|
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
|
224 |
|
private function analytics(AnalyticsContract $analytics) |
200
|
|
|
{ |
201
|
224 |
|
$this->analytics = $analytics; |
202
|
|
|
|
203
|
224 |
|
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
|
32 |
|
public function setTitle($title, $siteName = null, $separator = null) |
216
|
|
|
{ |
217
|
32 |
|
$this->title->set($title) |
218
|
32 |
|
->setSeparator($separator); |
219
|
|
|
|
220
|
32 |
|
return $this->setSiteName($siteName); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Set the site name. |
225
|
|
|
* |
226
|
|
|
* @param string $siteName |
227
|
|
|
* |
228
|
|
|
* @return self |
229
|
|
|
*/ |
230
|
32 |
|
public function setSiteName($siteName) |
231
|
|
|
{ |
232
|
32 |
|
$this->title->setSiteName($siteName); |
233
|
|
|
|
234
|
32 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Set the description content. |
239
|
|
|
* |
240
|
|
|
* @param string $content |
241
|
|
|
* |
242
|
|
|
* @return \Arcanedev\SeoHelper\SeoMeta |
243
|
|
|
*/ |
244
|
24 |
|
public function setDescription($content) |
245
|
|
|
{ |
246
|
24 |
|
$this->description->set($content); |
247
|
|
|
|
248
|
24 |
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Set the keywords content. |
253
|
|
|
* |
254
|
|
|
* @param array|string $content |
255
|
|
|
* |
256
|
|
|
* @return \Arcanedev\SeoHelper\SeoMeta |
257
|
|
|
*/ |
258
|
40 |
|
public function setKeywords($content) |
259
|
|
|
{ |
260
|
40 |
|
$this->keywords->set($content); |
261
|
|
|
|
262
|
40 |
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Add a keyword. |
267
|
|
|
* |
268
|
|
|
* @param string $keyword |
269
|
|
|
* |
270
|
|
|
* @return \Arcanedev\SeoHelper\SeoMeta |
271
|
|
|
*/ |
272
|
8 |
|
public function addKeyword($keyword) |
273
|
|
|
{ |
274
|
8 |
|
$this->keywords->add($keyword); |
275
|
|
|
|
276
|
8 |
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Add many keywords. |
281
|
|
|
* |
282
|
|
|
* @param array $keywords |
283
|
|
|
* |
284
|
|
|
* @return \Arcanedev\SeoHelper\SeoMeta |
285
|
|
|
*/ |
286
|
8 |
|
public function addKeywords(array $keywords) |
287
|
|
|
{ |
288
|
8 |
|
$this->keywords->addMany($keywords); |
289
|
|
|
|
290
|
8 |
|
return $this; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Add a webmaster tool site verifier. |
295
|
|
|
* |
296
|
|
|
* @param string $webmaster |
297
|
|
|
* @param string $content |
298
|
|
|
* |
299
|
|
|
* @return \Arcanedev\SeoHelper\SeoMeta |
300
|
|
|
*/ |
301
|
8 |
|
public function addWebmaster($webmaster, $content) |
302
|
|
|
{ |
303
|
8 |
|
$this->webmasters->add($webmaster, $content); |
304
|
|
|
|
305
|
8 |
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Set the current URL. |
310
|
|
|
* |
311
|
|
|
* @param string $url |
312
|
|
|
* |
313
|
|
|
* @return \Arcanedev\SeoHelper\SeoMeta |
314
|
|
|
*/ |
315
|
80 |
|
public function setUrl($url) |
316
|
|
|
{ |
317
|
80 |
|
$this->currentUrl = $url; |
318
|
80 |
|
$this->misc->setUrl($url); |
319
|
|
|
|
320
|
80 |
|
return $this; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Set the Google Analytics code. |
325
|
|
|
* |
326
|
|
|
* @param string $code |
327
|
|
|
* |
328
|
|
|
* @return \Arcanedev\SeoHelper\SeoMeta |
329
|
|
|
*/ |
330
|
8 |
|
public function setGoogleAnalytics($code) |
331
|
|
|
{ |
332
|
8 |
|
$this->analytics->setGoogle($code); |
333
|
|
|
|
334
|
8 |
|
return $this; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/* ------------------------------------------------------------------------------------------------ |
338
|
|
|
| Main Functions |
339
|
|
|
| ------------------------------------------------------------------------------------------------ |
340
|
|
|
*/ |
341
|
|
|
/** |
342
|
|
|
* Add a meta tag. |
343
|
|
|
* |
344
|
|
|
* @param string $name |
345
|
|
|
* @param string $content |
346
|
|
|
* |
347
|
|
|
* @return \Arcanedev\SeoHelper\SeoMeta |
348
|
|
|
*/ |
349
|
8 |
|
public function addMeta($name, $content) |
350
|
|
|
{ |
351
|
8 |
|
$this->misc->add($name, $content); |
352
|
|
|
|
353
|
8 |
|
return $this; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Add many meta tags. |
358
|
|
|
* |
359
|
|
|
* @param array $metas |
360
|
|
|
* |
361
|
|
|
* @return \Arcanedev\SeoHelper\SeoMeta |
362
|
|
|
*/ |
363
|
8 |
|
public function addMetas(array $metas) |
364
|
|
|
{ |
365
|
8 |
|
$this->misc->addMany($metas); |
366
|
|
|
|
367
|
8 |
|
return $this; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Remove a meta from the meta collection by key. |
372
|
|
|
* |
373
|
|
|
* @param string|array $names |
374
|
|
|
* |
375
|
|
|
* @return \Arcanedev\SeoHelper\SeoMeta |
376
|
|
|
*/ |
377
|
8 |
|
public function removeMeta($names) |
378
|
|
|
{ |
379
|
8 |
|
$this->misc->remove($names); |
380
|
|
|
|
381
|
8 |
|
return $this; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Reset the meta collection except the description and keywords metas. |
386
|
|
|
* |
387
|
|
|
* @return \Arcanedev\SeoHelper\SeoMeta |
388
|
|
|
*/ |
389
|
8 |
|
public function resetMetas() |
390
|
|
|
{ |
391
|
8 |
|
$this->misc->reset(); |
392
|
|
|
|
393
|
8 |
|
return $this; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Reset all webmaster tool site verifier metas. |
398
|
|
|
* |
399
|
|
|
* @return \Arcanedev\SeoHelper\SeoMeta |
400
|
|
|
*/ |
401
|
8 |
|
public function resetWebmasters() |
402
|
|
|
{ |
403
|
8 |
|
$this->webmasters->reset(); |
404
|
|
|
|
405
|
8 |
|
return $this; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Render all seo tags. |
410
|
|
|
* |
411
|
|
|
* @return string |
412
|
|
|
*/ |
413
|
160 |
|
public function render() |
414
|
|
|
{ |
415
|
160 |
|
return implode(PHP_EOL, array_filter([ |
416
|
160 |
|
$this->title->render(), |
417
|
160 |
|
$this->description->render(), |
418
|
160 |
|
$this->keywords->render(), |
419
|
160 |
|
$this->misc->render(), |
420
|
160 |
|
$this->webmasters->render(), |
421
|
160 |
|
$this->analytics->render(), |
422
|
80 |
|
])); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Render all seo tags. |
427
|
|
|
* |
428
|
|
|
* @return string |
429
|
|
|
*/ |
430
|
64 |
|
public function __toString() |
431
|
|
|
{ |
432
|
64 |
|
return $this->render(); |
433
|
|
|
} |
434
|
|
|
} |
435
|
|
|
|