ClearBowl/api/app/Helpers/NutrictionCalculator.php

30 lines
680 B
PHP

<?php
namespace App\Helpers;
class NutritionCalculator
{
public static function calculateNutritionForRecipe(object $ingredients)
{
$nutrition = [
'kcal' => 0,
'carbohydrates' => 0,
'protein' => 0,
'fat' => 0,
];
foreach($ingredients as $ingredient)
{
foreach(array_keys($nutrition) as $nutritionValue) {
$nutrition[$nutritionValue] += $ingredient['weight'] / 100 * $ingredient['product'][$nutritionValue];
}
}
foreach($nutrition as $k => $v){
$nutrition[$k] = round($v, 2);
}
return $nutrition;
}
}