Completed
Push — master ( 53b99e...05e097 )
by Basil
03:48
created

JsonLd   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 293
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 23

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 23
dl 0
loc 293
rs 10
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A article() 0 4 1
A blogPosting() 0 4 1
A thing() 0 4 1
A creativeWork() 0 4 1
A event() 0 4 1
A liveBlogPosting() 0 4 1
A organization() 0 4 1
A person() 0 4 1
A place() 0 4 1
A socialMediaPosting() 0 4 1
A imageObject() 0 4 1
A aggregateRating() 0 4 1
A rating() 0 4 1
A comment() 0 4 1
A contactPoint() 0 4 1
A country() 0 4 1
A offer() 0 4 1
A postalAddress() 0 4 1
A propertyValue() 0 4 1
A addGraph() 0 13 2
A reset() 0 5 1
A registerView() 0 10 2
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...onld\Article($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 61 which is incompatible with the return type documented by luya\web\JsonLd::article of type luya\web\jsonld\Article.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...\BlogPosting($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 73 which is incompatible with the return type documented by luya\web\JsonLd::blogPosting of type luya\web\jsonld\BlogPosting.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...jsonld\Thing($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 84 which is incompatible with the return type documented by luya\web\JsonLd::thing of type luya\web\jsonld\Thing.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...CreativeWork($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 98 which is incompatible with the return type documented by luya\web\JsonLd::creativeWork of type luya\web\jsonld\CreativeWork.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...jsonld\Event($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 111 which is incompatible with the return type documented by luya\web\JsonLd::event of type luya\web\jsonld\Event.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...eBlogPosting($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 125 which is incompatible with the return type documented by luya\web\JsonLd::liveBlogPosting of type luya\web\jsonld\LiveBlogPosting.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...Organization($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 138 which is incompatible with the return type documented by luya\web\JsonLd::organization of type luya\web\jsonld\Organization.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...sonld\Person($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 151 which is incompatible with the return type documented by luya\web\JsonLd::person of type luya\web\jsonld\Person.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...jsonld\Place($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 164 which is incompatible with the return type documented by luya\web\JsonLd::place of type luya\web\jsonld\Place.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...MediaPosting($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 178 which is incompatible with the return type documented by luya\web\JsonLd::socialMediaPosting of type luya\web\jsonld\SocialMediaPosting.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...\ImageObject($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 190 which is incompatible with the return type documented by luya\web\JsonLd::imageObject of type luya\web\jsonld\ImageObject.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...regateRating($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 202 which is incompatible with the return type documented by luya\web\JsonLd::aggregateRating of type luya\web\jsonld\AggregateRating.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...sonld\Rating($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 214 which is incompatible with the return type documented by luya\web\JsonLd::rating of type luya\web\jsonld\Rating.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...onld\Comment($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 226 which is incompatible with the return type documented by luya\web\JsonLd::comment of type luya\web\jsonld\Comment.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...ContactPoint($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 240 which is incompatible with the return type documented by luya\web\JsonLd::contactPoint of type luya\web\jsonld\ContactPoint.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...onld\Country($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 252 which is incompatible with the return type documented by luya\web\JsonLd::country of type luya\web\jsonld\Country.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...jsonld\Offer($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 266 which is incompatible with the return type documented by luya\web\JsonLd::offer of type luya\web\jsonld\Offer.
Loading history...
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 = [])
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
277
    {
278
        return self::addGraph((new PostalAddress()));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...sonld\PostalAddress()); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 278 which is incompatible with the return type documented by luya\web\JsonLd::postalAddress of type luya\web\jsonld\PostalAddress.
Loading history...
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)));
0 ignored issues
show
Bug Compatibility introduced by
The expression self::addGraph(new \luya...ropertyValue($config)); of type luya\web\jsonld\BaseThing|array adds the type array to the return on line 290 which is incompatible with the return type documented by luya\web\JsonLd::propertyValue of type luya\web\jsonld\PropertyValue.
Loading history...
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