ClearBowl/api/app/Helpers/NutrictionCalculator.php

30 lines
680 B
PHP
Raw Normal View History

2020-01-06 17:14:54 +01:00
<?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];
}
}
2020-01-06 17:58:55 +01:00
foreach($nutrition as $k => $v){
$nutrition[$k] = round($v, 2);
}
2020-01-06 17:14:54 +01:00
return $nutrition;
}
}