ClearBowl/api/app/Helpers/NutrictionCalculator.php

30 lines
660 B
PHP
Raw Permalink Normal View History

2020-01-06 17:14:54 +01:00
<?php
namespace App\Helpers;
class NutritionCalculator
{
public static function calculateNutritionForProducts($products)
2020-01-06 17:14:54 +01:00
{
$nutrition = [
'kcal' => 0,
'carbohydrates' => 0,
'protein' => 0,
'fat' => 0,
];
foreach($products as $product)
2020-01-06 17:14:54 +01:00
{
foreach(array_keys($nutrition) as $nutritionValue) {
$nutrition[$nutritionValue] += $product['weight'] / 100 * $product['product'][$nutritionValue];
2020-01-06 17:14:54 +01:00
}
}
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;
}
}