Passed
Pull Request — develop (#37)
by
unknown
07:42
created

Recipe::getServes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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