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\services; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\base\Component; |
16
|
|
|
use Exception; |
17
|
|
|
use nystudio107\recipe\models\Settings; |
18
|
|
|
use nystudio107\recipe\Recipe; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
22
|
|
|
* @package Recipe |
|
|
|
|
23
|
|
|
* @since 1.3.0 |
|
|
|
|
24
|
|
|
*/ |
|
|
|
|
25
|
|
|
class NutritionApi extends Component |
26
|
|
|
{ |
27
|
|
|
/** |
|
|
|
|
28
|
|
|
* Returns nutritional information about a recipe. |
29
|
|
|
* |
30
|
|
|
* @param int|null $serves |
|
|
|
|
31
|
|
|
*/ |
|
|
|
|
32
|
|
|
public function getNutritionalInfo(array $ingredients, int $serves = null): array |
33
|
|
|
{ |
34
|
|
|
/** @var Settings $settings */ |
|
|
|
|
35
|
|
|
$settings = Recipe::$plugin->getSettings(); |
|
|
|
|
36
|
|
|
if (!$settings->hasApiCredentials()) { |
37
|
|
|
return []; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$url = 'https://api.edamam.com/api/nutrition-details' |
41
|
|
|
. '?app_id=' . Craft::parseEnv($settings->apiApplicationId) |
|
|
|
|
42
|
|
|
. '&app_key=' . Craft::parseEnv($settings->apiApplicationKey); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$data = [ |
45
|
|
|
'ingr' => $ingredients, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
if ($serves) { |
|
|
|
|
49
|
|
|
$data['yield'] = $serves; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
$response = Craft::createGuzzleClient()->post($url, ['json' => $data]); |
54
|
|
|
|
55
|
|
|
$result = json_decode($response->getBody(), null, 512, JSON_THROW_ON_ERROR); |
56
|
|
|
|
57
|
|
|
$yield = $result->yield ?: 1; |
58
|
|
|
|
59
|
|
|
return [ |
60
|
|
|
'servingSize' => round($result->totalWeight ?? 0 / $yield, 0) . ' grams', |
61
|
|
|
'calories' => round($result->totalNutrients->ENERC_KCAL->quantity ?? 0 / $yield, 0), |
62
|
|
|
'carbohydrateContent' => round($result->totalNutrients->CHOCDF->quantity ?? 0 / $yield, 1), |
63
|
|
|
'cholesterolContent' => round($result->totalNutrients->CHOLE->quantity ?? 0 / $yield, 1), |
64
|
|
|
'fatContent' => round($result->totalNutrients->FAT->quantity ?? 0 / $yield, 1), |
65
|
|
|
'fiberContent' => round($result->totalNutrients->FIBTG->quantity ?? 0 / $yield, 1), |
66
|
|
|
'proteinContent' => round($result->totalNutrients->PROCNT->quantity ?? 0 / $yield, 1), |
67
|
|
|
'saturatedFatContent' => round($result->totalNutrients->FASAT->quantity ?? 0 / $yield, 1), |
68
|
|
|
'sodiumContent' => round($result->totalNutrients->NA->quantity ?? 0 / $yield, 1), |
69
|
|
|
'sugarContent' => round($result->totalNutrients->SUGAR->quantity ?? 0 / $yield, 1), |
70
|
|
|
'transFatContent' => round($result->totalNutrients->FATRN->quantity ?? 0 / $yield, 1), |
71
|
|
|
'unsaturatedFatContent' => round((($result->totalNutrients->FAMS->quantity ?? 0) + ($result->totalNutrients->FAPU->quantity ?? 0)) / $yield, 1), |
72
|
|
|
]; |
73
|
|
|
} catch (Exception $exception) { |
74
|
|
|
$message = 'Error fetching nutritional information from API. '; |
75
|
|
|
|
76
|
|
|
if ($exception->getCode() == 401) { |
77
|
|
|
$message .= 'Please verify your API credentials.'; |
78
|
|
|
} elseif ($exception->getCode() == 555) { |
79
|
|
|
$message .= 'One or more ingredients could not be recognized.'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
Craft::error($message . $exception->getMessage(), __METHOD__); |
83
|
|
|
|
84
|
|
|
return ['error' => $message]; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|