Passed
Push — v1 ( 17b0b9...5234ef )
by Andrew
14:55 queued 06:40
created

Recipe::init()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 11
rs 9.6111
1
<?php
2
/**
3
 * Recipe plugin for Craft CMS 3.x
4
 *
5
 * A comprehensive recipe FieldType for Craft CMS that includes metric/imperial
6
 * conversion, portion calculation, and JSON-LD microdata support
7
 *
8
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\recipe\models;
13
14
use nystudio107\recipe\helpers\Json;
15
use nystudio107\recipe\helpers\PluginTemplate;
16
17
use Craft;
18
use craft\base\Model;
19
use craft\helpers\Template;
20
21
use Twig\Markup;
22
23
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
25
 * @package   Recipe
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
26
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
27
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
28
class Recipe extends Model
29
{
30
    // Constants
31
    // =========================================================================
32
33
    const US_RDA = [
34
        'calories' => 2000,
35
        'carbohydrateContent' => 275,
36
        'cholesterolContent' => 300,
37
        'fatContent' => 78,
38
        'fiberContent' => 28,
39
        'proteinContent' => 50,
40
        'saturatedFatContent' => 20,
41
        'sodiumContent' => 2300,
42
        'sugarContent' => 50,
43
    ];
44
45
    // Mapping to convert any of the incorrect plural values
46
    const NORMALIZE_PLURALS = [
47
        'tsps' => 'tsp',
48
        'tbsps' => 'tbsp',
49
        'flozs' => 'floz',
50
        'cups' => 'c',
51
        'ozs' => 'oz',
52
        'lbs' => 'lb',
53
        'mls' => 'ml',
54
        'ls' => 'l',
55
        'mgs' => 'mg',
56
        'gs' => 'g',
57
        'kg' => 'kg',
58
    ];
59
60
    // Public Properties
61
    // =========================================================================
62
63
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
64
     * @var string
65
     */
66
    public $name;
67
68
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
69
     * @var string
70
     */
71
    public $description;
72
73
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
74
     * @var string
75
     */
76
    public $skill = 'intermediate';
77
78
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
79
     * @var int
80
     */
81
    public $serves = 1;
82
83
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
84
     * @var array
85
     */
86
    public $ingredients = [];
87
88
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
89
     * @var array
90
     */
91
    public $directions = [];
92
93
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
94
     * @var int
95
     */
96
    public $imageId = 0;
97
98
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
99
     * @var int
100
     */
101
    public $prepTime;
102
103
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
104
     * @var int
105
     */
106
    public $cookTime;
107
108
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
109
     * @var int
110
     */
111
    public $totalTime;
112
113
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
114
     * @var array
115
     */
116
    public $ratings = [];
117
118
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
119
     * @var string
120
     */
121
    public $servingSize;
122
123
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
124
     * @var int
125
     */
126
    public $calories;
127
128
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
129
     * @var int
130
     */
131
    public $carbohydrateContent;
132
133
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
134
     * @var int
135
     */
136
    public $cholesterolContent;
137
138
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
139
     * @var int
140
     */
141
    public $fatContent;
142
143
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
144
     * @var int
145
     */
146
    public $fiberContent;
147
148
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
149
     * @var int
150
     */
151
    public $proteinContent;
152
153
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
154
     * @var int
155
     */
156
    public $saturatedFatContent;
157
158
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
159
     * @var int
160
     */
161
    public $sodiumContent;
162
163
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
164
     * @var int
165
     */
166
    public $sugarContent;
167
168
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
169
     * @var int
170
     */
171
    public $transFatContent;
172
173
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
174
     * @var int
175
     */
176
    public $unsaturatedFatContent;
177
178
    // Public Methods
179
    // =========================================================================
180
181
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
182
     * @inheritdoc
183
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
184
    public function init()
185
    {
186
        parent::init();
187
        // Fix any of the incorrect plural values
188
        if (!empty($this->ingredients)) {
189
            foreach ($this->ingredients as &$row) {
190
                if (!empty($row['units']) && !empty(self::NORMALIZE_PLURALS[$row['units']])) {
191
                    $row['units'] = self::NORMALIZE_PLURALS[$row['units']];
192
                }
193
            }
194
            unset($row);
195
        }
196
    }
197
198
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
199
     * @inheritdoc
200
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
201
    public function rules()
202
    {
203
        return [
204
            ['name', 'string'],
205
            ['name', 'default', 'value' => ''],
206
            ['description', 'string'],
207
            ['skill', 'string'],
208
            ['serves', 'integer'],
209
            ['imageId', 'integer'],
210
            ['prepTime', 'integer'],
211
            ['cookTime', 'integer'],
212
            ['totalTime', 'integer'],
213
            ['servingSize', 'string'],
214
            ['calories', 'integer'],
215
            ['carbohydrateContent', 'integer'],
216
            ['cholesterolContent', 'integer'],
217
            ['fatContent', 'integer'],
218
            ['fiberContent', 'integer'],
219
            ['proteinContent', 'integer'],
220
            ['saturatedFatContent', 'integer'],
221
            ['sodiumContent', 'integer'],
222
            ['sugarContent', 'integer'],
223
            ['transFatContent', 'integer'],
224
            ['unsaturatedFatContent', 'integer'],
225
        ];
226
    }
227
228
    /**
229
     * Render the JSON-LD Structured Data for this recipe
230
     *
231
     * @param bool $raw
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
232
     *
233
     * @return string|\Twig_Markup
234
     */
235
    public function renderRecipeJSONLD($raw = true)
236
    {
237
        $recipeJSONLD = [
238
            'context' => 'http://schema.org',
239
            'type' => 'Recipe',
240
            'name' => $this->name,
241
            'image' => $this->getImageUrl(),
242
            'description' => $this->description,
243
            'recipeYield' => $this->serves,
244
            'recipeIngredient' => $this->getIngredients('imperial', 0, false),
245
            'recipeInstructions' => $this->getDirections(false),
246
        ];
247
        $recipeJSONLD = array_filter($recipeJSONLD);
248
249
        $nutrition = [
250
            'type' => 'NutritionInformation',
251
            'servingSize' => $this->servingSize,
252
            'calories' => $this->calories,
253
            'carbohydrateContent' => $this->carbohydrateContent,
254
            'cholesterolContent' => $this->cholesterolContent,
255
            'fatContent' => $this->fatContent,
256
            'fiberContent' => $this->fiberContent,
257
            'proteinContent' => $this->proteinContent,
258
            'saturatedFatContent' => $this->saturatedFatContent,
259
            'sodiumContent' => $this->sodiumContent,
260
            'sugarContent' => $this->sugarContent,
261
            'transFatContent' => $this->transFatContent,
262
            'unsaturatedFatContent' => $this->unsaturatedFatContent,
263
        ];
264
        $nutrition = array_filter($nutrition);
265
        $recipeJSONLD['nutrition'] = $nutrition;
266
        if (count($recipeJSONLD['nutrition']) === 1) {
267
            unset($recipeJSONLD['nutrition']);
268
        }
269
        $aggregateRating = $this->getAggregateRating();
270
        if ($aggregateRating) {
271
            $aggregateRatings = [
272
                'type' => 'AggregateRating',
273
                'ratingCount' => $this->getRatingsCount(),
274
                'bestRating' => '5',
275
                'worstRating' => '1',
276
                'ratingValue' => $aggregateRating,
277
            ];
278
            $aggregateRatings = array_filter($aggregateRatings);
279
            $recipeJSONLD['aggregateRating'] = $aggregateRatings;
280
281
            $reviews = [];
282
            foreach ($this->ratings as $rating) {
283
                $review = [
284
                    'type' => 'Review',
285
                    'author' => $rating['author'],
286
                    'name' => $this->name . ' ' . Craft::t('recipe', 'Review'),
287
                    'description' => $rating['review'],
288
                    'reviewRating' => [
289
                        'type' => 'Rating',
290
                        'bestRating' => '5',
291
                        'worstRating' => '1',
292
                        'ratingValue' => $rating['rating'],
293
                    ],
294
                ];
295
                $reviews[] = $review;
296
            }
297
            $reviews = array_filter($reviews);
298
            $recipeJSONLD['review'] = $reviews;
299
        }
300
301
        if ($this->prepTime) {
302
            $recipeJSONLD['prepTime'] = 'PT' . $this->prepTime . 'M';
303
        }
304
        if ($this->cookTime) {
305
            $recipeJSONLD['cookTime'] = 'PT' . $this->cookTime . 'M';
306
        }
307
        if ($this->totalTime) {
308
            $recipeJSONLD['totalTime'] = 'PT' . $this->totalTime . 'M';
309
        }
310
311
        return $this->renderJsonLd($recipeJSONLD, $raw);
312
    }
313
314
    /**
315
     * Get the URL to the recipe's image
316
     *
317
     * @param null $transform
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $transform is correct as it would always require null to be passed?
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
318
     *
319
     * @return null|string
320
     */
321
    public function getImageUrl($transform = null)
322
    {
323
        $result = '';
324
        if (isset($this->imageId) && $this->imageId) {
325
            $image = Craft::$app->getAssets()->getAssetById($this->imageId[0]);
326
            if ($image) {
0 ignored issues
show
introduced by
$image is of type craft\elements\Asset, thus it always evaluated to true.
Loading history...
327
                $result = $image->getUrl($transform);
328
            }
329
        }
330
331
        return $result;
332
    }
333
334
    /**
335
     * Render the Nutrition Facts template
336
     *
337
     * @param array $rda
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
338
     * @return Markup
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
339
     */
340
    public function renderNutritionFacts(array $rda = self::US_RDA): Markup {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
341
        return PluginTemplate::renderPluginTemplate(
342
            'recipe-nutrition-facts',
343
            [
344
                'value' => $this,
345
                'rda' => $rda,
346
            ]
347
        );
348
    }
349
350
    /**
351
     * Get all of the ingredients for this recipe
352
     *
353
     * @param string $outputUnits
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
354
     * @param int    $serving
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
355
     * @param bool   $raw
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
356
     *
357
     * @return array
358
     */
359
    public function getIngredients($outputUnits = 'imperial', $serving = 0, $raw = true): array
360
    {
361
        $result = [];
362
363
        if (!empty($this->ingredients)) {
364
            foreach ($this->ingredients as $row) {
365
                $convertedUnits = '';
366
                $ingredient = '';
367
                if ($row['quantity']) {
368
                    // Multiply the quantity by how many servings we want
369
                    $multiplier = 1;
370
                    if ($serving > 0) {
371
                        $multiplier = $serving / $this->serves;
372
                    }
373
                    $quantity = $row['quantity'] * $multiplier;
374
                    $originalQuantity = $quantity;
375
376
                    // Do the imperial->metric units conversion
377
                    if ($outputUnits === 'imperial') {
378
                        switch ($row['units']) {
379
                            case 'ml':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
380
                                $convertedUnits = 'tsp';
381
                                $quantity *= 0.2;
382
                                break;
383
                            case 'l':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
384
                                $convertedUnits = 'c';
385
                                $quantity *= 4.2;
386
                                break;
387
                            case 'mg':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
388
                                $convertedUnits = 'oz';
389
                                $quantity *= 0.000035274;
390
                                break;
391
                            case 'g':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
392
                                $convertedUnits = 'oz';
393
                                $quantity *= 0.035274;
394
                                break;
395
                            case 'kg':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
396
                                $convertedUnits = 'lb';
397
                                $quantity *= 2.2046226218;
398
                                break;
399
                        }
400
                    }
401
                    // Do the metric->imperial units conversion
402
                    if ($outputUnits === 'metric') {
403
                        switch ($row['units']) {
404
                            case 'tsp':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
405
                                $convertedUnits = 'ml';
406
                                $quantity *= 4.929;
407
                                break;
408
                            case 'tbsp':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
409
                                $convertedUnits = 'ml';
410
                                $quantity *= 14.787;
411
                                break;
412
                            case 'floz':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
413
                                $convertedUnits = 'ml';
414
                                $quantity *= 29.574;
415
                                break;
416
                            case 'c':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
417
                                $convertedUnits = 'l';
418
                                $quantity *= 0.236588;
419
                                break;
420
                            case 'oz':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
421
                                $convertedUnits = 'g';
422
                                $quantity *= 28.3495;
423
                                break;
424
                            case 'lb':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
425
                                $convertedUnits = 'kg';
426
                                $quantity *= 0.45359237;
427
                                break;
428
                        }
429
430
                        $quantity = round($quantity, 1);
431
                    }
432
433
                    // Convert imperial units to nice fractions
434
                    if ($outputUnits === 'imperial') {
435
                        $quantity = $this->convertToFractions($quantity);
436
                    }
437
                    $ingredient .= $quantity;
438
439
                    if ($row['units']) {
440
                        $units = $row['units'];
441
                        if ($convertedUnits) {
442
                            $units = $convertedUnits;
443
                        }
444
                        if ($originalQuantity <= 1) {
445
                            $units = rtrim($units);
446
                            $units = rtrim($units, 's');
447
                        }
448
                        $ingredient .= ' ' . $units;
449
                    }
450
                }
451
                if ($row['ingredient']) {
452
                    $ingredient .= ' ' . $row['ingredient'];
453
                }
454
                if ($raw) {
455
                    $ingredient = Template::raw($ingredient);
456
                }
457
                $result[] = $ingredient;
458
            }
459
        }
460
461
        return $result;
462
    }
463
464
    /**
465
     * Convert decimal numbers into fractions
466
     *
467
     * @param $quantity
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
468
     *
469
     * @return string
470
     */
471
    private function convertToFractions($quantity)
0 ignored issues
show
Coding Style introduced by
Private method name "Recipe::convertToFractions" must be prefixed with an underscore
Loading history...
472
    {
473
        $whole = floor($quantity);
474
        // Round the mantissa so we can do a floating point comparison without
475
        // weirdness, per: https://www.php.net/manual/en/language.types.float.php#113703
476
        $fraction = round($quantity - $whole, 3);
477
        switch ($fraction) {
478
            case 0:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
479
                $fraction = '';
480
                break;
481
            case 0.25:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
482
                $fraction = ' &frac14;';
483
                break;
484
            case 0.33:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
485
                $fraction = ' &frac13;';
486
                break;
487
            case 0.66:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
488
                $fraction = ' &frac23;';
489
                break;
490
            case 0.165:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
491
                $fraction = ' &frac16;';
492
                break;
493
            case 0.5:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
494
                $fraction = ' &frac12;';
495
                break;
496
            case 0.75:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
497
                $fraction = ' &frac34;';
498
                break;
499
            case 0.125:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
500
                $fraction = ' &#x215B;';
501
                break;
502
            case 0.375:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
503
                $fraction = ' &#x215C;';
504
                break;
505
            case 0.625:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
506
                $fraction = ' &#x215D;';
507
                break;
508
            case 0.875:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
509
                $fraction = ' &#x215E;';
510
                break;
511
            default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
512
                $precision = 5;
513
                $pnum = round($fraction, $precision);
514
                $denominator = 10 ** $precision;
515
                $numerator = $pnum * $denominator;
516
                $fraction = '<sup>'
517
                    .$numerator
518
                    . '</sup>&frasl;<sub>'
519
                    .$denominator
520
                    . '</sub>';
521
                break;
522
        }
523
        if ($whole === 0) {
524
            $whole = '';
525
        }
526
527
        return $whole.$fraction;
528
    }
529
530
    /**
531
     * Get all of the directions for this recipe
532
     *
533
     * @param bool $raw
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
534
     *
535
     * @return array
536
     */
537
    public function getDirections($raw = true)
538
    {
539
        $result = [];
540
        if (!empty($this->directions)) {
541
            foreach ($this->directions as $row) {
542
                $direction = $row['direction'];
543
                if ($raw) {
544
                    $direction = Template::raw($direction);
545
                }
546
                $result[] = $direction;
547
            }
548
        }
549
550
        return $result;
551
    }
552
553
    /**
554
     * Get the aggregate rating from all of the ratings
555
     *
556
     * @return float|int|string
557
     */
558
    public function getAggregateRating()
559
    {
560
        $result = 0;
561
        $total = 0;
562
        if (isset($this->ratings) && !empty($this->ratings)) {
563
            foreach ($this->ratings as $row) {
564
                $result += $row['rating'];
565
                $total++;
566
            }
567
            $result /= $total;
568
        } else {
569
            $result = '';
570
        }
571
572
        return $result;
573
    }
574
575
    /**
576
     * Get the total number of ratings
577
     *
578
     * @return int
579
     */
580
    public function getRatingsCount(): int
581
    {
582
        return count($this->ratings);
583
    }
584
585
    // Private Methods
586
    // =========================================================================
587
588
    /**
589
     * Renders a JSON-LD representation of the schema
590
     *
591
     * @param      $json
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 1 spaces but found 6
Loading history...
592
     * @param bool $raw
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
593
     *
594
     * @return string|\Twig_Markup
595
     */
596
    private function renderJsonLd($json, $raw = true)
0 ignored issues
show
Coding Style introduced by
Private method name "Recipe::renderJsonLd" must be prefixed with an underscore
Loading history...
597
    {
598
        $linebreak = '';
599
600
        // If `devMode` is enabled, make the JSON-LD human-readable
601
        if (Craft::$app->getConfig()->getGeneral()->devMode) {
602
            $linebreak = PHP_EOL;
603
        }
604
605
        // Render the resulting JSON-LD
606
        $result = '<script type="application/ld+json">'
607
            .$linebreak
608
            .Json::encode($json)
609
            .$linebreak
610
            .'</script>';
611
612
        if ($raw === true) {
613
            $result = Template::raw($result);
614
        }
615
616
        return $result;
617
    }
618
}
619