1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace luya\web; |
4
|
|
|
|
5
|
|
|
use luya\web\jsonld\Service; |
6
|
|
|
use Yii; |
7
|
|
|
use yii\helpers\Json; |
8
|
|
|
use yii\base\BaseObject; |
9
|
|
|
use luya\Exception; |
10
|
|
|
use luya\web\jsonld\Person; |
11
|
|
|
use luya\web\jsonld\Event; |
12
|
|
|
use luya\web\jsonld\Place; |
13
|
|
|
use luya\web\jsonld\LiveBlogPosting; |
14
|
|
|
use luya\web\jsonld\Article; |
15
|
|
|
use luya\web\jsonld\BlogPosting; |
16
|
|
|
use luya\web\jsonld\CreativeWork; |
17
|
|
|
use luya\web\jsonld\Organization; |
18
|
|
|
use luya\web\jsonld\SocialMediaPosting; |
19
|
|
|
use luya\web\jsonld\Thing; |
20
|
|
|
use luya\web\jsonld\ImageObject; |
21
|
|
|
use luya\web\jsonld\AggregateRating; |
22
|
|
|
use luya\web\jsonld\Rating; |
23
|
|
|
use luya\web\jsonld\Comment; |
24
|
|
|
use luya\web\jsonld\ContactPoint; |
25
|
|
|
use luya\web\jsonld\Country; |
26
|
|
|
use luya\web\jsonld\Offer; |
27
|
|
|
use luya\web\jsonld\PostalAddress; |
28
|
|
|
use luya\web\jsonld\PropertyValue; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Registerin Microdata as JsonLD. |
32
|
|
|
* |
33
|
|
|
* In order to register a json ld tag just call: |
34
|
|
|
* |
35
|
|
|
* ```php |
36
|
|
|
* JsonLd::person() |
37
|
|
|
* ->setGivenName('Albert') |
38
|
|
|
* ->setFamilyName('Einstein') |
39
|
|
|
* ->setBirthPlace('Ulm, Germany'); |
40
|
|
|
* ``` |
41
|
|
|
* |
42
|
|
|
* Or any other tags. This will register the json ld output into the layout file of the view. |
43
|
|
|
* |
44
|
|
|
* @see https://schema.org/docs/schemas.html |
45
|
|
|
* @author Basil Suter <[email protected]> |
46
|
|
|
* @since 1.0.0 |
47
|
|
|
*/ |
48
|
|
|
class JsonLd extends BaseObject |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* Register new Article. |
52
|
|
|
* |
53
|
|
|
* An article, such as a news article or piece of investigative report. |
54
|
|
|
* Newspapers and magazines have articles of many different types and this is intended to cover them all. |
55
|
|
|
* |
56
|
|
|
* @param array $config Optional config array to provided article data via setter methods. |
57
|
|
|
* @return \luya\web\jsonld\Article |
58
|
|
|
* @since 1.0.1 |
59
|
|
|
*/ |
60
|
|
|
public static function article(array $config = []) |
61
|
|
|
{ |
62
|
|
|
return self::addGraph((new Article($config))); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Register new Blog Posting. |
67
|
|
|
* |
68
|
|
|
* @param array $config Optional config array to provided blog posting data via setter methods. |
69
|
|
|
* @return \luya\web\jsonld\BlogPosting |
70
|
|
|
* @since 1.0.1 |
71
|
|
|
*/ |
72
|
|
|
public static function blogPosting(array $config = []) |
73
|
|
|
{ |
74
|
|
|
return self::addGraph((new BlogPosting($config))); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Register new Thing. |
79
|
|
|
* |
80
|
|
|
* @param array $config Optional config array to provided person data via setter methods. |
81
|
|
|
* @return \luya\web\jsonld\Thing |
82
|
|
|
*/ |
83
|
|
|
public static function thing(array $config = []) |
84
|
|
|
{ |
85
|
|
|
return self::addGraph((new Thing($config))); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Register new CreativeWork. |
90
|
|
|
* |
91
|
|
|
* The most generic kind of creative work, including books, movies, photographs, software programs, etc. |
92
|
|
|
* |
93
|
|
|
* @param array $config Optional config array to provided creative work data via setter methods. |
94
|
|
|
* @return \luya\web\jsonld\CreativeWork |
95
|
|
|
* @since 1.0.1 |
96
|
|
|
*/ |
97
|
|
|
public static function creativeWork(array $config = []) |
98
|
|
|
{ |
99
|
|
|
return self::addGraph((new CreativeWork($config))); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Register new Event. |
104
|
|
|
* |
105
|
|
|
* An event happening at a certain time and location, such as a concert, lecture, or festival. |
106
|
|
|
* |
107
|
|
|
* @param array $config |
108
|
|
|
* @return \luya\web\jsonld\Event |
109
|
|
|
*/ |
110
|
|
|
public static function event(array $config = []) |
111
|
|
|
{ |
112
|
|
|
return self::addGraph((new Event($config))); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Register new Live Blog Posting. |
117
|
|
|
* |
118
|
|
|
* A blog post intended to provide a rolling textual coverage of an ongoing event through continuous updates. |
119
|
|
|
* |
120
|
|
|
* @param array $config Optional config array to provided live blog posting data via setter methods. |
121
|
|
|
* @return \luya\web\jsonld\LiveBlogPosting |
122
|
|
|
* @since 1.0.1 |
123
|
|
|
*/ |
124
|
|
|
public static function liveBlogPosting(array $config = []) |
125
|
|
|
{ |
126
|
|
|
return self::addGraph((new LiveBlogPosting($config))); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Register new Organization. |
131
|
|
|
* |
132
|
|
|
* An organization such as a school, NGO, corporation, club, etc. |
133
|
|
|
* |
134
|
|
|
* @param array $config Optional config array to provided organization data via setter methods. |
135
|
|
|
* @return \luya\web\jsonld\Organization |
136
|
|
|
*/ |
137
|
|
|
public static function organization(array $config = []) |
138
|
|
|
{ |
139
|
|
|
return self::addGraph((new Organization($config))); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Register new Person. |
144
|
|
|
* |
145
|
|
|
* A person (alive, dead, undead, or fictional). |
146
|
|
|
* |
147
|
|
|
* @param array $config Optional config array to provided person data via setter methods. |
148
|
|
|
* @return \luya\web\jsonld\Person |
149
|
|
|
*/ |
150
|
|
|
public static function person(array $config = []) |
151
|
|
|
{ |
152
|
|
|
return self::addGraph((new Person($config))); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Register new Place |
157
|
|
|
* |
158
|
|
|
* Entities that have a somewhat fixed, physical extension. |
159
|
|
|
* |
160
|
|
|
* @param array $config Optional config array to provided place data via setter methods. |
161
|
|
|
* @return \luya\web\jsonld\Place |
162
|
|
|
*/ |
163
|
|
|
public static function place(array $config = []) |
164
|
|
|
{ |
165
|
|
|
return self::addGraph((new Place($config))); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Register new Social Media Posting. |
170
|
|
|
* |
171
|
|
|
* A post to a social media platform, including blog posts, tweets, Facebook posts, etc. |
172
|
|
|
* |
173
|
|
|
* @param array $config Optional config array to provided social media posting data via setter methods. |
174
|
|
|
* @return \luya\web\jsonld\SocialMediaPosting |
175
|
|
|
* @since 1.0.1 |
176
|
|
|
*/ |
177
|
|
|
public static function socialMediaPosting(array $config = []) |
178
|
|
|
{ |
179
|
|
|
return self::addGraph((new SocialMediaPosting($config))); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Register new Image Object. |
184
|
|
|
* |
185
|
|
|
* @param array $config |
186
|
|
|
* @return \luya\web\jsonld\ImageObject |
187
|
|
|
* @since 1.0.3 |
188
|
|
|
*/ |
189
|
|
|
public static function imageObject(array $config = []) |
190
|
|
|
{ |
191
|
|
|
return self::addGraph((new ImageObject($config))); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Register new Aggregated Rating. |
196
|
|
|
* |
197
|
|
|
* @param array $config |
198
|
|
|
* @return \luya\web\jsonld\AggregateRating |
199
|
|
|
* @since 1.0.3 |
200
|
|
|
*/ |
201
|
|
|
public static function aggregateRating(array $config = []) |
202
|
|
|
{ |
203
|
|
|
return self::addGraph((new AggregateRating($config))); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Register new Rating. |
208
|
|
|
* |
209
|
|
|
* @param array $config |
210
|
|
|
* @return \luya\web\jsonld\Rating |
211
|
|
|
* @since 1.0.3 |
212
|
|
|
*/ |
213
|
|
|
public static function rating(array $config = []) |
214
|
|
|
{ |
215
|
|
|
return self::addGraph((new Rating($config))); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Register new Comment. |
220
|
|
|
* |
221
|
|
|
* @param array $config |
222
|
|
|
* @return \luya\web\jsonld\Comment |
223
|
|
|
* @since 1.0.3 |
224
|
|
|
*/ |
225
|
|
|
public static function comment(array $config = []) |
226
|
|
|
{ |
227
|
|
|
return self::addGraph((new Comment($config))); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Register new Contact Point. |
232
|
|
|
* |
233
|
|
|
* This is mainly used for addresses or user coordinates like email, telephone etc. |
234
|
|
|
* |
235
|
|
|
* @param array $config |
236
|
|
|
* @return \luya\web\jsonld\ContactPoint |
237
|
|
|
* @since 1.0.3 |
238
|
|
|
*/ |
239
|
|
|
public static function contactPoint(array $config = []) |
240
|
|
|
{ |
241
|
|
|
return self::addGraph((new ContactPoint($config))); |
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Register new Country. |
246
|
|
|
* |
247
|
|
|
* @param array $config |
248
|
|
|
* @return \luya\web\jsonld\Country |
249
|
|
|
* @since 1.0.3 |
250
|
|
|
*/ |
251
|
|
|
public static function country(array $config = []) |
252
|
|
|
{ |
253
|
|
|
return self::addGraph((new Country($config))); |
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Register new Offer. |
258
|
|
|
* |
259
|
|
|
* This is used for selling products. |
260
|
|
|
* |
261
|
|
|
* @param array $config |
262
|
|
|
* @return \luya\web\jsonld\Offer |
263
|
|
|
* @since 1.0.3 |
264
|
|
|
*/ |
265
|
|
|
public static function offer(array $config = []) |
266
|
|
|
{ |
267
|
|
|
return self::addGraph((new Offer($config))); |
|
|
|
|
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Register new Postal Address. |
272
|
|
|
* |
273
|
|
|
* @param array $config |
274
|
|
|
* @return \luya\web\jsonld\PostalAddress |
275
|
|
|
* @since 1.0.3 |
276
|
|
|
*/ |
277
|
|
|
public static function postalAddress(array $config = []) |
|
|
|
|
278
|
|
|
{ |
279
|
|
|
return self::addGraph((new PostalAddress())); |
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Register new Property Value. |
284
|
|
|
* |
285
|
|
|
* @param array $config |
286
|
|
|
* @return \luya\web\jsonld\PropertyValue |
287
|
|
|
* @since 1.0.3 |
288
|
|
|
*/ |
289
|
|
|
public static function propertyValue(array $config = []) |
290
|
|
|
{ |
291
|
|
|
return self::addGraph((new PropertyValue($config))); |
|
|
|
|
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Register a new service |
296
|
|
|
* |
297
|
|
|
* @param array $config |
298
|
|
|
* @return \luya\web\jsonld\Service |
299
|
|
|
* @since 1.0.19 |
300
|
|
|
*/ |
301
|
|
|
public static function service(array $config = []) |
302
|
|
|
{ |
303
|
|
|
return self::addGraph((new Service($config))); |
|
|
|
|
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Register graph data. |
308
|
|
|
* |
309
|
|
|
* @param \luya\web\jsonld\BaseThing|array $data Can be either an array or an object based on {{luya\web\jsonld\BaseThing}} which contains the Arrayable Inteface. |
310
|
|
|
* @return array|object |
311
|
|
|
*/ |
312
|
|
|
public static function addGraph($data) |
313
|
|
|
{ |
314
|
|
|
self::registerView(); |
315
|
|
|
|
316
|
|
|
if (is_scalar($data)) { |
317
|
|
|
throw new Exception("Data must be either an array or an object of type luya\web\jsonld\BaseThing."); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
Yii::$app->view->params['@context'] = 'https://schema.org'; |
321
|
|
|
Yii::$app->view->params['@graph'][] = $data; |
322
|
|
|
|
323
|
|
|
return $data; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Reset the JsonLd Data. |
328
|
|
|
* |
329
|
|
|
* This method is mainly usefull when working with unit tests for JsonLd. |
330
|
|
|
*/ |
331
|
|
|
public static function reset() |
332
|
|
|
{ |
333
|
|
|
self::$_view = null; |
334
|
|
|
Yii::$app->view->params['@graph'] = []; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
private static $_view; |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Register the view file an observe the event which then reads the data from @graph params key. |
341
|
|
|
*/ |
342
|
|
|
protected static function registerView() |
343
|
|
|
{ |
344
|
|
|
if (self::$_view === null) { |
345
|
|
|
Yii::$app->view->on(View::EVENT_BEGIN_BODY, function ($event) { |
346
|
|
|
echo '<script type="application/ld+json">' . Json::encode($event->sender->params) . '</script>'; |
347
|
|
|
}); |
348
|
|
|
|
349
|
|
|
self::$_view = true; |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|