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