1
|
|
|
<?php namespace Arcanedev\SeoHelper\Entities\Twitter; |
2
|
|
|
|
3
|
|
|
use Arcanedev\SeoHelper\Contracts\Entities\TwitterCard as CardContract; |
4
|
|
|
use Arcanedev\SeoHelper\Exceptions\InvalidTwitterCardException; |
5
|
|
|
use Arcanedev\Support\Traits\Configurable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Card |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\SeoHelper\Entities\Twitter |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Card implements CardContract |
14
|
|
|
{ |
15
|
|
|
/* ------------------------------------------------------------------------------------------------ |
16
|
|
|
| Traits |
17
|
|
|
| ------------------------------------------------------------------------------------------------ |
18
|
|
|
*/ |
19
|
|
|
use Configurable; |
20
|
|
|
|
21
|
|
|
/* ------------------------------------------------------------------------------------------------ |
22
|
|
|
| Properties |
23
|
|
|
| ------------------------------------------------------------------------------------------------ |
24
|
|
|
*/ |
25
|
|
|
/** |
26
|
|
|
* Card type. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $type; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Card meta collection. |
34
|
|
|
* |
35
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\Entities\MetaCollection |
36
|
|
|
*/ |
37
|
|
|
protected $metas; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Card images. |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $images = []; |
45
|
|
|
|
46
|
|
|
/* ------------------------------------------------------------------------------------------------ |
47
|
|
|
| Constructor |
48
|
|
|
| ------------------------------------------------------------------------------------------------ |
49
|
|
|
*/ |
50
|
|
|
/** |
51
|
|
|
* Make the twitter card instance. |
52
|
|
|
* |
53
|
|
|
* @param array $configs |
54
|
|
|
*/ |
55
|
328 |
|
public function __construct(array $configs = []) |
56
|
|
|
{ |
57
|
328 |
|
$this->setConfigs($configs); |
58
|
328 |
|
$this->metas = new MetaCollection; |
59
|
|
|
|
60
|
328 |
|
$this->init(); |
61
|
328 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Start the engine. |
65
|
|
|
* |
66
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Twitter\Card |
67
|
|
|
*/ |
68
|
328 |
|
private function init() |
69
|
|
|
{ |
70
|
328 |
|
$this->setPrefix($this->getConfig('prefix', 'twitter:')); |
71
|
328 |
|
$this->setType($this->getConfig('card', static::TYPE_SUMMARY)); |
72
|
328 |
|
$this->setSite($this->getConfig('site', '')); |
73
|
328 |
|
$this->setTitle($this->getConfig('title', '')); |
74
|
328 |
|
$this->addMetas($this->getConfig('metas', [])); |
75
|
|
|
|
76
|
328 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/* ------------------------------------------------------------------------------------------------ |
80
|
|
|
| Getters & Setters |
81
|
|
|
| ------------------------------------------------------------------------------------------------ |
82
|
|
|
*/ |
83
|
|
|
/** |
84
|
|
|
* Set meta prefix name. |
85
|
|
|
* |
86
|
|
|
* @param string $prefix |
87
|
|
|
* |
88
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Twitter\Card |
89
|
|
|
*/ |
90
|
328 |
|
private function setPrefix($prefix) |
91
|
|
|
{ |
92
|
328 |
|
$this->metas->setPrefix($prefix); |
93
|
|
|
|
94
|
328 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set the card type. |
99
|
|
|
* |
100
|
|
|
* @param string $type |
101
|
|
|
* |
102
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Twitter\Card |
103
|
|
|
*/ |
104
|
328 |
|
public function setType($type) |
105
|
|
|
{ |
106
|
328 |
|
if (empty($type)) return $this; |
107
|
|
|
|
108
|
328 |
|
$this->checkType($type); |
109
|
328 |
|
$this->type = $type; |
110
|
|
|
|
111
|
328 |
|
return $this->addMeta('card', $type); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set card site. |
116
|
|
|
* |
117
|
|
|
* @param string $site |
118
|
|
|
* |
119
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Twitter\Card |
120
|
|
|
*/ |
121
|
328 |
|
public function setSite($site) |
122
|
|
|
{ |
123
|
328 |
|
if (empty($site)) return $this; |
124
|
|
|
|
125
|
328 |
|
$this->checkSite($site); |
126
|
|
|
|
127
|
328 |
|
return $this->addMeta('site', $site); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set card title. |
132
|
|
|
* |
133
|
|
|
* @param string $title |
134
|
|
|
* |
135
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Twitter\Card |
136
|
|
|
*/ |
137
|
328 |
|
public function setTitle($title) |
138
|
|
|
{ |
139
|
328 |
|
return $this->addMeta('title', $title); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set card description. |
144
|
|
|
* |
145
|
|
|
* @param string $description |
146
|
|
|
* |
147
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Twitter\Card |
148
|
|
|
*/ |
149
|
40 |
|
public function setDescription($description) |
150
|
|
|
{ |
151
|
40 |
|
return $this->addMeta('description', $description); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Add image to the card. |
156
|
|
|
* |
157
|
|
|
* @param string $url |
158
|
|
|
* |
159
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Twitter\Card |
160
|
|
|
*/ |
161
|
40 |
|
public function addImage($url) |
162
|
|
|
{ |
163
|
40 |
|
if (count($this->images) < 4) { |
164
|
40 |
|
$this->images[] = $url; |
165
|
20 |
|
} |
166
|
|
|
|
167
|
40 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Add many metas to the card. |
172
|
|
|
* |
173
|
|
|
* @param array $metas |
174
|
|
|
* |
175
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Twitter\Card |
176
|
|
|
*/ |
177
|
328 |
|
public function addMetas(array $metas) |
178
|
|
|
{ |
179
|
328 |
|
$this->metas->addMany($metas); |
180
|
|
|
|
181
|
328 |
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Add a meta to the card. |
186
|
|
|
* |
187
|
|
|
* @param string $name |
188
|
|
|
* @param string $content |
189
|
|
|
* |
190
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Twitter\Card |
191
|
|
|
*/ |
192
|
328 |
|
public function addMeta($name, $content) |
193
|
|
|
{ |
194
|
328 |
|
$this->metas->add($name, $content); |
195
|
|
|
|
196
|
328 |
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get all supported card types. |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
328 |
|
public function types() |
205
|
|
|
{ |
206
|
|
|
return [ |
207
|
328 |
|
static::TYPE_APP, |
208
|
328 |
|
static::TYPE_GALLERY, |
209
|
328 |
|
static::TYPE_PHOTO, |
210
|
328 |
|
static::TYPE_PLAYER, |
211
|
328 |
|
static::TYPE_PRODUCT, |
212
|
328 |
|
static::TYPE_SUMMARY, |
213
|
328 |
|
static::TYPE_SUMMARY_LARGE_IMAGE, |
214
|
164 |
|
]; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/* ------------------------------------------------------------------------------------------------ |
218
|
|
|
| Main Functions |
219
|
|
|
| ------------------------------------------------------------------------------------------------ |
220
|
|
|
*/ |
221
|
|
|
/** |
222
|
|
|
* Render card images. |
223
|
|
|
*/ |
224
|
40 |
|
private function loadImages() |
225
|
|
|
{ |
226
|
40 |
|
if (count($this->images) == 1) { |
227
|
32 |
|
$this->addMeta('image', $this->images[0]); |
228
|
|
|
|
229
|
32 |
|
return; |
230
|
|
|
} |
231
|
|
|
|
232
|
8 |
|
foreach ($this->images as $number => $url) { |
233
|
8 |
|
$this->addMeta("image{$number}", $url); |
234
|
4 |
|
} |
235
|
8 |
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Reset the card. |
239
|
|
|
* |
240
|
|
|
* @return \Arcanedev\SeoHelper\Entities\Twitter\Card |
241
|
|
|
*/ |
242
|
16 |
|
public function reset() |
243
|
|
|
{ |
244
|
16 |
|
$this->metas->reset(); |
245
|
16 |
|
$this->images = []; |
246
|
|
|
|
247
|
16 |
|
return $this->init(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Render the twitter card. |
252
|
|
|
* |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
232 |
|
public function render() |
256
|
|
|
{ |
257
|
232 |
|
if ( ! empty($this->images)) { |
258
|
40 |
|
$this->loadImages(); |
259
|
20 |
|
} |
260
|
|
|
|
261
|
232 |
|
return $this->metas->render(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Render the tag. |
266
|
|
|
* |
267
|
|
|
* @return string |
268
|
|
|
*/ |
269
|
64 |
|
public function __toString() |
270
|
|
|
{ |
271
|
64 |
|
return $this->render(); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/* ------------------------------------------------------------------------------------------------ |
275
|
|
|
| Check Functions |
276
|
|
|
| ------------------------------------------------------------------------------------------------ |
277
|
|
|
*/ |
278
|
|
|
/** |
279
|
|
|
* Check the card type. |
280
|
|
|
* |
281
|
|
|
* @param string $type |
282
|
|
|
* |
283
|
|
|
* @throws \Arcanedev\SeoHelper\Exceptions\InvalidTwitterCardException |
284
|
|
|
*/ |
285
|
328 |
|
private function checkType(&$type) |
286
|
|
|
{ |
287
|
328 |
|
if ( ! is_string($type)) { |
288
|
8 |
|
throw new InvalidTwitterCardException( |
289
|
8 |
|
'The Twitter card type must be a string value, [' . gettype($type) . '] was given.' |
290
|
4 |
|
); |
291
|
|
|
} |
292
|
|
|
|
293
|
328 |
|
$type = strtolower(trim($type)); |
294
|
|
|
|
295
|
328 |
|
if ( ! in_array($type, $this->types())) { |
296
|
8 |
|
throw new InvalidTwitterCardException("The Twitter card type [$type] is not supported."); |
297
|
|
|
} |
298
|
328 |
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Check the card site. |
302
|
|
|
* |
303
|
|
|
* @param string $site |
304
|
|
|
*/ |
305
|
328 |
|
private function checkSite(&$site) |
306
|
|
|
{ |
307
|
328 |
|
$site = $this->prepareUsername($site); |
308
|
328 |
|
} |
309
|
|
|
|
310
|
|
|
/* ------------------------------------------------------------------------------------------------ |
311
|
|
|
| Other Functions |
312
|
|
|
| ------------------------------------------------------------------------------------------------ |
313
|
|
|
*/ |
314
|
|
|
/** |
315
|
|
|
* Prepare username. |
316
|
|
|
* |
317
|
|
|
* @param string $username |
318
|
|
|
* |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
328 |
|
private function prepareUsername($username) |
322
|
|
|
{ |
323
|
328 |
|
if ( ! starts_with($username, '@')) { |
324
|
328 |
|
$username = '@' . $username; |
325
|
164 |
|
} |
326
|
|
|
|
327
|
328 |
|
return $username; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|