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