1
|
|
|
<?php namespace Arcanedev\SeoHelper\Entities\OpenGraph; |
2
|
|
|
|
3
|
|
|
use Arcanedev\SeoHelper\Contracts\Entities\OpenGraph as OpenGraphContract; |
4
|
|
|
use Arcanedev\Support\Traits\Configurable; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Graph |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\SeoHelper\Entities\OpenGraph |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Graph implements OpenGraphContract |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Traits |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Configurable; |
20
|
|
|
|
21
|
|
|
/* ----------------------------------------------------------------- |
22
|
|
|
| Properties |
23
|
|
|
| ----------------------------------------------------------------- |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The Open Graph meta collection. |
28
|
|
|
* |
29
|
|
|
* @var \Arcanedev\SeoHelper\Contracts\Entities\MetaCollection |
30
|
|
|
*/ |
31
|
|
|
protected $metas; |
32
|
|
|
|
33
|
|
|
/* ----------------------------------------------------------------- |
34
|
|
|
| Constructor |
35
|
|
|
| ----------------------------------------------------------------- |
36
|
|
|
*/ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Make Graph instance. |
40
|
|
|
* |
41
|
|
|
* @param array $configs |
42
|
|
|
*/ |
43
|
90 |
|
public function __construct(array $configs = []) |
44
|
|
|
{ |
45
|
90 |
|
$this->setConfigs($configs); |
46
|
90 |
|
$this->metas = new MetaCollection; |
47
|
|
|
|
48
|
90 |
|
$this->init(); |
49
|
90 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Start the engine. |
53
|
|
|
*/ |
54
|
90 |
|
private function init() |
55
|
|
|
{ |
56
|
90 |
|
$this->setPrefix($this->getConfig('prefix', 'og:')); |
57
|
90 |
|
$this->setType($this->getConfig('type', '')); |
58
|
90 |
|
$this->setTitle($this->getConfig('title', '')); |
59
|
90 |
|
$this->setDescription($this->getConfig('description', '')); |
60
|
90 |
|
$this->setSiteName($this->getConfig('site-name', '')); |
61
|
90 |
|
$this->addProperties($this->getConfig('properties', [])); |
62
|
90 |
|
} |
63
|
|
|
|
64
|
|
|
/* ----------------------------------------------------------------- |
65
|
|
|
| Getters & Setters |
66
|
|
|
| ----------------------------------------------------------------- |
67
|
|
|
*/ |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set the open graph prefix. |
71
|
|
|
* |
72
|
|
|
* @param string $prefix |
73
|
|
|
* |
74
|
|
|
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph |
75
|
|
|
*/ |
76
|
90 |
|
public function setPrefix($prefix) |
77
|
|
|
{ |
78
|
90 |
|
$this->metas->setPrefix($prefix); |
79
|
|
|
|
80
|
90 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set type property. |
85
|
|
|
* |
86
|
|
|
* @param string $type |
87
|
|
|
* |
88
|
|
|
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph |
89
|
|
|
*/ |
90
|
90 |
|
public function setType($type) |
91
|
|
|
{ |
92
|
90 |
|
return $this->addProperty('type', $type); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set title property. |
97
|
|
|
* |
98
|
|
|
* @param string $title |
99
|
|
|
* |
100
|
|
|
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph |
101
|
|
|
*/ |
102
|
90 |
|
public function setTitle($title) |
103
|
|
|
{ |
104
|
90 |
|
return $this->addProperty('title', $title); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set description property. |
109
|
|
|
* |
110
|
|
|
* @param string $description |
111
|
|
|
* |
112
|
|
|
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph |
113
|
|
|
*/ |
114
|
90 |
|
public function setDescription($description) |
115
|
|
|
{ |
116
|
90 |
|
return $this->addProperty('description', $description); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set url property. |
121
|
|
|
* |
122
|
|
|
* @param string $url |
123
|
|
|
* |
124
|
|
|
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph |
125
|
|
|
*/ |
126
|
4 |
|
public function setUrl($url) |
127
|
|
|
{ |
128
|
4 |
|
return $this->addProperty('url', $url); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set image property. |
133
|
|
|
* |
134
|
|
|
* @param string $image |
135
|
|
|
* |
136
|
|
|
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph |
137
|
|
|
*/ |
138
|
6 |
|
public function setImage($image) |
139
|
|
|
{ |
140
|
6 |
|
return $this->addProperty('image', $image); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set site name property. |
145
|
|
|
* |
146
|
|
|
* @param string $siteName |
147
|
|
|
* |
148
|
|
|
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph |
149
|
|
|
*/ |
150
|
90 |
|
public function setSiteName($siteName) |
151
|
|
|
{ |
152
|
90 |
|
return $this->addProperty('site_name', $siteName); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set the locale. |
157
|
|
|
* |
158
|
|
|
* @param string $locale |
159
|
|
|
* |
160
|
|
|
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph |
161
|
|
|
*/ |
162
|
2 |
|
public function setLocale($locale) |
163
|
|
|
{ |
164
|
2 |
|
return $this->addProperty('locale', $locale); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set the alternative locales. |
169
|
|
|
* |
170
|
|
|
* @param array $locales |
171
|
|
|
* |
172
|
|
|
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph |
173
|
|
|
*/ |
174
|
2 |
|
public function setAlternativeLocales(array $locales) |
175
|
|
|
{ |
176
|
2 |
|
return $this->addProperty('locale:alternate', $locales); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Add many open graph properties. |
181
|
|
|
* |
182
|
|
|
* @param array $properties |
183
|
|
|
* |
184
|
|
|
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph |
185
|
|
|
*/ |
186
|
90 |
|
public function addProperties(array $properties) |
187
|
|
|
{ |
188
|
90 |
|
$this->metas->addMany($properties); |
189
|
|
|
|
190
|
90 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Add an open graph property. |
195
|
|
|
* |
196
|
|
|
* @param string $property |
197
|
|
|
* @param string|array $content |
198
|
|
|
* |
199
|
|
|
* @return \Arcanedev\SeoHelper\Entities\OpenGraph\Graph |
200
|
|
|
*/ |
201
|
90 |
|
public function addProperty($property, $content) |
202
|
|
|
{ |
203
|
90 |
|
$this->metas->add($property, $content); |
204
|
|
|
|
205
|
90 |
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/* ----------------------------------------------------------------- |
209
|
|
|
| Main Methods |
210
|
|
|
| ----------------------------------------------------------------- |
211
|
|
|
*/ |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Render the tag. |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
70 |
|
public function render() |
219
|
|
|
{ |
220
|
70 |
|
return $this->metas->render(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Render the tag. |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
16 |
|
public function __toString() |
229
|
|
|
{ |
230
|
16 |
|
return $this->render(); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|