|
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 |
|
|
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
|
|
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
|
|
|
/** |
|
|
|
|
|
|
24
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
25
|
|
|
* @package Recipe |
|
|
|
|
|
|
26
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
27
|
|
|
*/ |
|
|
|
|
|
|
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
|
|
|
/** |
|
|
|
|
|
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
public $name; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
|
|
|
|
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
public $description; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
|
|
|
|
|
74
|
|
|
* @var string |
|
75
|
|
|
*/ |
|
76
|
|
|
public $skill = 'intermediate'; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
|
|
|
|
|
79
|
|
|
* @var int |
|
80
|
|
|
*/ |
|
81
|
|
|
public $serves = 1; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
|
|
|
|
|
84
|
|
|
* @var string |
|
85
|
|
|
*/ |
|
86
|
|
|
public $servesUnit = ''; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
|
|
|
|
|
89
|
|
|
* @var array |
|
90
|
|
|
*/ |
|
91
|
|
|
public $ingredients = []; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
|
|
|
|
|
94
|
|
|
* @var array |
|
95
|
|
|
*/ |
|
96
|
|
|
public $directions = []; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
|
|
|
|
|
99
|
|
|
* @var int |
|
100
|
|
|
*/ |
|
101
|
|
|
public $imageId = 0; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
|
|
|
|
|
104
|
|
|
* @var int |
|
105
|
|
|
*/ |
|
106
|
|
|
public $prepTime; |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
|
|
|
|
|
109
|
|
|
* @var int |
|
110
|
|
|
*/ |
|
111
|
|
|
public $cookTime; |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
|
|
|
|
|
114
|
|
|
* @var int |
|
115
|
|
|
*/ |
|
116
|
|
|
public $totalTime; |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
|
|
|
|
|
119
|
|
|
* @var array |
|
120
|
|
|
*/ |
|
121
|
|
|
public $ratings = []; |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
|
|
|
|
|
124
|
|
|
* @var string |
|
125
|
|
|
*/ |
|
126
|
|
|
public $servingSize; |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
|
|
|
|
|
129
|
|
|
* @var int |
|
130
|
|
|
*/ |
|
131
|
|
|
public $calories; |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
|
|
|
|
|
134
|
|
|
* @var int |
|
135
|
|
|
*/ |
|
136
|
|
|
public $carbohydrateContent; |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
|
|
|
|
|
139
|
|
|
* @var int |
|
140
|
|
|
*/ |
|
141
|
|
|
public $cholesterolContent; |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
|
|
|
|
|
144
|
|
|
* @var int |
|
145
|
|
|
*/ |
|
146
|
|
|
public $fatContent; |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
|
|
|
|
|
149
|
|
|
* @var int |
|
150
|
|
|
*/ |
|
151
|
|
|
public $fiberContent; |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
|
|
|
|
|
154
|
|
|
* @var int |
|
155
|
|
|
*/ |
|
156
|
|
|
public $proteinContent; |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
|
|
|
|
|
159
|
|
|
* @var int |
|
160
|
|
|
*/ |
|
161
|
|
|
public $saturatedFatContent; |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
|
|
|
|
|
164
|
|
|
* @var int |
|
165
|
|
|
*/ |
|
166
|
|
|
public $sodiumContent; |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
|
|
|
|
|
169
|
|
|
* @var int |
|
170
|
|
|
*/ |
|
171
|
|
|
public $sugarContent; |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
|
|
|
|
|
174
|
|
|
* @var int |
|
175
|
|
|
*/ |
|
176
|
|
|
public $transFatContent; |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
|
|
|
|
|
179
|
|
|
* @var int |
|
180
|
|
|
*/ |
|
181
|
|
|
public $unsaturatedFatContent; |
|
182
|
|
|
|
|
183
|
|
|
// Public Methods |
|
184
|
|
|
// ========================================================================= |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
|
|
|
|
|
187
|
|
|
* @inheritdoc |
|
188
|
|
|
*/ |
|
|
|
|
|
|
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
|
|
|
/** |
|
|
|
|
|
|
204
|
|
|
* @inheritdoc |
|
205
|
|
|
*/ |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
343
|
|
|
* @return Markup |
|
|
|
|
|
|
344
|
|
|
*/ |
|
345
|
|
|
public function renderNutritionFacts(array $rda = self::US_RDA): Markup { |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
359
|
|
|
* @param int $serving |
|
|
|
|
|
|
360
|
|
|
* @param bool $raw |
|
|
|
|
|
|
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': |
|
|
|
|
|
|
385
|
|
|
$convertedUnits = 'tsp'; |
|
386
|
|
|
$quantity *= 0.2; |
|
387
|
|
|
break; |
|
388
|
|
|
case 'l': |
|
|
|
|
|
|
389
|
|
|
$convertedUnits = 'cups'; |
|
390
|
|
|
$quantity *= 4.2; |
|
391
|
|
|
break; |
|
392
|
|
|
case 'mg': |
|
|
|
|
|
|
393
|
|
|
$convertedUnits = 'oz'; |
|
394
|
|
|
$quantity *= 0.000035274; |
|
395
|
|
|
break; |
|
396
|
|
|
case 'g': |
|
|
|
|
|
|
397
|
|
|
$convertedUnits = 'oz'; |
|
398
|
|
|
$quantity *= 0.035274; |
|
399
|
|
|
break; |
|
400
|
|
|
case 'kg': |
|
|
|
|
|
|
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': |
|
|
|
|
|
|
410
|
|
|
$convertedUnits = 'ml'; |
|
411
|
|
|
$quantity *= 4.929; |
|
412
|
|
|
break; |
|
413
|
|
|
case 'tbsp': |
|
|
|
|
|
|
414
|
|
|
$convertedUnits = 'ml'; |
|
415
|
|
|
$quantity *= 14.787; |
|
416
|
|
|
break; |
|
417
|
|
|
case 'floz': |
|
|
|
|
|
|
418
|
|
|
$convertedUnits = 'ml'; |
|
419
|
|
|
$quantity *= 29.574; |
|
420
|
|
|
break; |
|
421
|
|
|
case 'cups': |
|
|
|
|
|
|
422
|
|
|
$convertedUnits = 'l'; |
|
423
|
|
|
$quantity *= 0.236588; |
|
424
|
|
|
break; |
|
425
|
|
|
case 'oz': |
|
|
|
|
|
|
426
|
|
|
$convertedUnits = 'g'; |
|
427
|
|
|
$quantity *= 28.3495; |
|
428
|
|
|
break; |
|
429
|
|
|
case 'lb': |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
472
|
|
|
* |
|
473
|
|
|
* @return string |
|
474
|
|
|
*/ |
|
475
|
|
|
private function convertToFractions($quantity) |
|
|
|
|
|
|
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: |
|
|
|
|
|
|
483
|
|
|
$fraction = ''; |
|
484
|
|
|
break; |
|
485
|
|
|
case 0.25: |
|
|
|
|
|
|
486
|
|
|
$fraction = ' ¼'; |
|
487
|
|
|
break; |
|
488
|
|
|
case 0.33: |
|
|
|
|
|
|
489
|
|
|
$fraction = ' ⅓'; |
|
490
|
|
|
break; |
|
491
|
|
|
case 0.66: |
|
|
|
|
|
|
492
|
|
|
$fraction = ' ⅔'; |
|
493
|
|
|
break; |
|
494
|
|
|
case 0.165: |
|
|
|
|
|
|
495
|
|
|
$fraction = ' ⅙'; |
|
496
|
|
|
break; |
|
497
|
|
|
case 0.5: |
|
|
|
|
|
|
498
|
|
|
$fraction = ' ½'; |
|
499
|
|
|
break; |
|
500
|
|
|
case 0.75: |
|
|
|
|
|
|
501
|
|
|
$fraction = ' ¾'; |
|
502
|
|
|
break; |
|
503
|
|
|
case 0.125: |
|
|
|
|
|
|
504
|
|
|
$fraction = ' ⅛'; |
|
505
|
|
|
break; |
|
506
|
|
|
case 0.375: |
|
|
|
|
|
|
507
|
|
|
$fraction = ' ⅜'; |
|
508
|
|
|
break; |
|
509
|
|
|
case 0.625: |
|
|
|
|
|
|
510
|
|
|
$fraction = ' ⅝'; |
|
511
|
|
|
break; |
|
512
|
|
|
case 0.875: |
|
|
|
|
|
|
513
|
|
|
$fraction = ' ⅞'; |
|
514
|
|
|
break; |
|
515
|
|
|
default: |
|
|
|
|
|
|
516
|
|
|
$precision = 1; |
|
517
|
|
|
$pnum = round($fraction, $precision); |
|
518
|
|
|
$denominator = 10 ** $precision; |
|
519
|
|
|
$numerator = $pnum * $denominator; |
|
520
|
|
|
$fraction = ' <sup>' |
|
521
|
|
|
.$numerator |
|
522
|
|
|
. '</sup>⁄<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 |
|
|
|
|
|
|
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
|
|
|
*/ |
|
|
|
|
|
|
592
|
|
|
public function getServes(): string |
|
593
|
|
|
{ |
|
594
|
|
|
if(!empty($this->servesUnit)) { |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
608
|
|
|
* @param bool $raw |
|
|
|
|
|
|
609
|
|
|
* |
|
610
|
|
|
* @return string|\Twig_Markup |
|
611
|
|
|
*/ |
|
612
|
|
|
private function renderJsonLd($json, $raw = true) |
|
|
|
|
|
|
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
|
|
|
|