1
|
|
|
<?php namespace Arcanedev\SeoHelper; |
2
|
|
|
|
3
|
|
|
use Arcanedev\SeoHelper\Contracts\SeoHelper as SeoHelperContract; |
4
|
|
|
use Arcanedev\SeoHelper\Contracts\SeoMeta as SeoMetaContract; |
5
|
|
|
use Arcanedev\SeoHelper\Contracts\SeoOpenGraph as SeoOpenGraphContract; |
6
|
|
|
use Arcanedev\SeoHelper\Contracts\SeoTwitter as SeoTwitterContract; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SeoHelper |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\SeoHelper |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class SeoHelper implements SeoHelperContract |
15
|
|
|
{ |
16
|
|
|
/* ----------------------------------------------------------------- |
17
|
|
|
| Properties |
18
|
|
|
| ----------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
/** |
21
|
|
|
* The SeoMeta instance. |
22
|
|
|
* |
23
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\SeoMeta |
24
|
|
|
*/ |
25
|
|
|
private $seoMeta; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The SeoOpenGraph instance. |
29
|
|
|
* |
30
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\SeoOpenGraph |
31
|
|
|
*/ |
32
|
|
|
private $seoOpenGraph; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The SeoTwitter instance. |
36
|
|
|
* |
37
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\SeoTwitter |
38
|
|
|
*/ |
39
|
|
|
private $seoTwitter; |
40
|
|
|
|
41
|
|
|
/* ----------------------------------------------------------------- |
42
|
|
|
| Constructor |
43
|
|
|
| ----------------------------------------------------------------- |
44
|
|
|
*/ |
45
|
|
|
/** |
46
|
|
|
* Make SeoHelper instance. |
47
|
|
|
* |
48
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\SeoMeta $seoMeta |
49
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\SeoOpenGraph $seoOpenGraph |
50
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\SeoTwitter $seoTwitter |
51
|
|
|
*/ |
52
|
63 |
|
public function __construct( |
53
|
|
|
SeoMetaContract $seoMeta, |
54
|
|
|
SeoOpenGraphContract $seoOpenGraph, |
55
|
|
|
SeoTwitterContract $seoTwitter |
56
|
|
|
) { |
57
|
63 |
|
$this->setSeoMeta($seoMeta); |
58
|
63 |
|
$this->setSeoOpenGraph($seoOpenGraph); |
59
|
63 |
|
$this->setSeoTwitter($seoTwitter); |
60
|
63 |
|
} |
61
|
|
|
|
62
|
|
|
/* ----------------------------------------------------------------- |
63
|
|
|
| Getters & Setters |
64
|
|
|
| ----------------------------------------------------------------- |
65
|
|
|
*/ |
66
|
|
|
/** |
67
|
|
|
* Get SeoMeta instance. |
68
|
|
|
* |
69
|
|
|
* @return \Arcanedev\SeoHelper\Contracts\SeoMeta |
70
|
|
|
*/ |
71
|
45 |
|
public function meta() |
72
|
|
|
{ |
73
|
45 |
|
return $this->seoMeta; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set SeoMeta instance. |
78
|
|
|
* |
79
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\SeoMeta $seoMeta |
80
|
|
|
* |
81
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
82
|
|
|
*/ |
83
|
63 |
|
public function setSeoMeta(SeoMetaContract $seoMeta) |
84
|
|
|
{ |
85
|
63 |
|
$this->seoMeta = $seoMeta; |
86
|
|
|
|
87
|
63 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get SeoOpenGraph instance. |
92
|
|
|
* |
93
|
|
|
* @return \Arcanedev\SeoHelper\Contracts\SeoOpenGraph |
94
|
|
|
*/ |
95
|
45 |
|
public function openGraph() |
96
|
|
|
{ |
97
|
45 |
|
return $this->seoOpenGraph; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get SeoOpenGraph instance (alias). |
102
|
|
|
* |
103
|
|
|
* @see openGraph() |
104
|
|
|
* |
105
|
|
|
* @return \Arcanedev\SeoHelper\Contracts\SeoOpenGraph |
106
|
|
|
*/ |
107
|
3 |
|
public function og() |
108
|
|
|
{ |
109
|
3 |
|
return $this->openGraph(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get SeoOpenGraph instance. |
114
|
|
|
* |
115
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\SeoOpenGraph $seoOpenGraph |
116
|
|
|
* |
117
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
118
|
|
|
*/ |
119
|
63 |
|
public function setSeoOpenGraph(SeoOpenGraphContract $seoOpenGraph) |
120
|
|
|
{ |
121
|
63 |
|
$this->seoOpenGraph = $seoOpenGraph; |
122
|
|
|
|
123
|
63 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get SeoTwitter instance. |
128
|
|
|
* |
129
|
|
|
* @return \Arcanedev\SeoHelper\Contracts\SeoTwitter |
130
|
|
|
*/ |
131
|
42 |
|
public function twitter() |
132
|
|
|
{ |
133
|
42 |
|
return $this->seoTwitter; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set SeoTwitter instance. |
138
|
|
|
* |
139
|
|
|
* @param \Arcanedev\SeoHelper\Contracts\SeoTwitter $seoTwitter |
140
|
|
|
* |
141
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
142
|
|
|
*/ |
143
|
63 |
|
public function setSeoTwitter(SeoTwitterContract $seoTwitter) |
144
|
|
|
{ |
145
|
63 |
|
$this->seoTwitter = $seoTwitter; |
146
|
|
|
|
147
|
63 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Set title. |
152
|
|
|
* |
153
|
|
|
* @param string $title |
154
|
|
|
* @param string|null $siteName |
155
|
|
|
* @param string|null $separator |
156
|
|
|
* |
157
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
158
|
|
|
*/ |
159
|
12 |
|
public function setTitle($title, $siteName = null, $separator = null) |
160
|
|
|
{ |
161
|
12 |
|
$this->meta()->setTitle($title, null, $separator); |
162
|
12 |
|
$this->openGraph()->setTitle($title); |
163
|
12 |
|
$this->twitter()->setTitle($title); |
164
|
|
|
|
165
|
12 |
|
return $this->setSiteName($siteName); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set the site name. |
170
|
|
|
* |
171
|
|
|
* @param string $siteName |
172
|
|
|
* |
173
|
|
|
* @return self |
174
|
|
|
*/ |
175
|
12 |
|
public function setSiteName($siteName) |
176
|
|
|
{ |
177
|
12 |
|
$this->meta()->setSiteName($siteName); |
178
|
12 |
|
$this->openGraph()->setSiteName($siteName); |
179
|
|
|
|
180
|
12 |
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Hide the site name. |
185
|
|
|
* |
186
|
|
|
* @return self |
187
|
|
|
*/ |
188
|
3 |
|
public function hideSiteName() |
189
|
|
|
{ |
190
|
3 |
|
$this->meta()->hideSiteName(); |
191
|
|
|
|
192
|
3 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Show the site name. |
197
|
|
|
* |
198
|
|
|
* @return self |
199
|
|
|
*/ |
200
|
3 |
|
public function showSiteName() |
201
|
|
|
{ |
202
|
3 |
|
$this->meta()->showSiteName(); |
203
|
|
|
|
204
|
3 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Set description. |
209
|
|
|
* |
210
|
|
|
* @param string $description |
211
|
|
|
* |
212
|
|
|
* @return \Arcanedev\SeoHelper\Contracts\SeoHelper |
213
|
|
|
*/ |
214
|
6 |
|
public function setDescription($description) |
215
|
|
|
{ |
216
|
6 |
|
$this->meta()->setDescription($description); |
217
|
6 |
|
$this->openGraph()->setDescription($description); |
218
|
6 |
|
$this->twitter()->setDescription($description); |
219
|
|
|
|
220
|
6 |
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Set keywords. |
225
|
|
|
* |
226
|
|
|
* @param array|string $keywords |
227
|
|
|
* |
228
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
229
|
|
|
*/ |
230
|
6 |
|
public function setKeywords($keywords) |
231
|
|
|
{ |
232
|
6 |
|
$this->meta()->setKeywords($keywords); |
233
|
|
|
|
234
|
6 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Set Image. |
239
|
|
|
* |
240
|
|
|
* @param string $imageUrl |
241
|
|
|
* |
242
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
243
|
|
|
*/ |
244
|
3 |
|
public function setImage($imageUrl) |
245
|
|
|
{ |
246
|
3 |
|
$this->openGraph()->setImage($imageUrl); |
247
|
3 |
|
$this->twitter()->addImage($imageUrl); |
248
|
|
|
|
249
|
3 |
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/* ----------------------------------------------------------------- |
253
|
|
|
| Main Methods |
254
|
|
|
| ----------------------------------------------------------------- |
255
|
|
|
*/ |
256
|
|
|
/** |
257
|
|
|
* Render all seo tags. |
258
|
|
|
* |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
39 |
|
public function render() |
262
|
|
|
{ |
263
|
39 |
|
return implode(PHP_EOL, array_filter([ |
264
|
39 |
|
$this->meta()->render(), |
265
|
39 |
|
$this->openGraph()->render(), |
266
|
39 |
|
$this->twitter()->render(), |
267
|
13 |
|
])); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Render all seo tags with HtmlString object. |
272
|
|
|
* |
273
|
|
|
* @return \Illuminate\Support\HtmlString |
274
|
|
|
*/ |
275
|
3 |
|
public function renderHtml() |
276
|
|
|
{ |
277
|
3 |
|
return new \Illuminate\Support\HtmlString( |
278
|
3 |
|
$this->render() |
279
|
1 |
|
); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Render the tag. |
284
|
|
|
* |
285
|
|
|
* @return string |
286
|
|
|
*/ |
287
|
21 |
|
public function __toString() |
288
|
|
|
{ |
289
|
21 |
|
return $this->render(); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Enable the OpenGraph. |
294
|
|
|
* |
295
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
296
|
|
|
*/ |
297
|
3 |
|
public function enableOpenGraph() |
298
|
|
|
{ |
299
|
3 |
|
$this->openGraph()->enable(); |
300
|
|
|
|
301
|
3 |
|
return $this; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Disable the OpenGraph. |
306
|
|
|
* |
307
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
308
|
|
|
*/ |
309
|
3 |
|
public function disableOpenGraph() |
310
|
|
|
{ |
311
|
3 |
|
$this->openGraph()->disable(); |
312
|
|
|
|
313
|
3 |
|
return $this; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Enable the Twitter Card. |
318
|
|
|
* |
319
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
320
|
|
|
*/ |
321
|
3 |
|
public function enableTwitter() |
322
|
|
|
{ |
323
|
3 |
|
$this->twitter()->enable(); |
324
|
|
|
|
325
|
3 |
|
return $this; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Disable the Twitter Card. |
330
|
|
|
* |
331
|
|
|
* @return \Arcanedev\SeoHelper\SeoHelper |
332
|
|
|
*/ |
333
|
3 |
|
public function disableTwitter() |
334
|
|
|
{ |
335
|
3 |
|
$this->twitter()->disable(); |
336
|
|
|
|
337
|
3 |
|
return $this; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|