Refactor nutrients data array as a collection

This commit is contained in:
Christopher C. Wells 2021-02-21 08:23:30 -08:00
parent d3a881c359
commit 2f91c320ef
8 changed files with 69 additions and 27 deletions

View File

@ -95,9 +95,9 @@ class FoodController extends Controller
$attributes['name'] = Str::lower($attributes['name']);
// Default nutrients to zero.
foreach (Nutrients::$all as $nutrient) {
if (is_null($attributes[$nutrient['value']])) {
$attributes[$nutrient['value']] = 0;
foreach (Nutrients::all()->pluck('value') as $nutrient) {
if (is_null($attributes[$nutrient])) {
$attributes[$nutrient] = 0;
}
}

View File

@ -37,17 +37,17 @@ class JournalEntryController extends Controller
'date' => $date->toDateString(),
])->get();
$sums = [];
foreach (Nutrients::$all as $nutrient) {
$sums[$nutrient['value']] = round($entries->sum($nutrient['value']));
foreach (Nutrients::all()->pluck('value') as $nutrient) {
$sums[$nutrient] = round($entries->sum($nutrient));
}
// Get daily goals data for user.
$goals = Auth::user()->getGoalsByTime($date);
$dailyGoals = [];
foreach (Nutrients::$all as $nutrient) {
foreach (Nutrients::all()->pluck('value') as $nutrient) {
$goal = $goals['present']
->where('frequency', 'daily')
->where('name', $nutrient['value'])
->where('name', $nutrient)
->first();
if ($goal) {
$dailyGoals[$goal->name] = round($sums[$goal->name] / $goal->goal * 100);
@ -154,17 +154,17 @@ class JournalEntryController extends Controller
Number::floatFromString($ingredient['amount']),
$ingredient['unit']
);
foreach (Nutrients::$all as $nutrient) {
$entries[$entry_key]->{$nutrient['value']} += $item->{$nutrient['value']} * $nutrient_multiplier;
foreach (Nutrients::all()->pluck('value') as $nutrient) {
$entries[$entry_key]->{$nutrient} += $item->{$nutrient} * $nutrient_multiplier;
}
$entries[$entry_key]->foods->add($item);
}
elseif ($ingredient['type'] == Recipe::class) {
$item = Recipe::whereId($ingredient['id'])->first();
foreach (Nutrients::$all as $nutrient) {
$entries[$entry_key]->{$nutrient['value']} += Nutrients::calculateRecipeNutrientAmount(
foreach (Nutrients::all()->pluck('value') as $nutrient) {
$entries[$entry_key]->{$nutrient} += Nutrients::calculateRecipeNutrientAmount(
$item,
$nutrient['value'],
$nutrient,
Number::floatFromString($ingredient['amount']),
$ingredient['unit']
);

View File

@ -90,7 +90,7 @@ final class Goal extends Model
*/
public static function getNameOptions(): array {
$options = [];
foreach (Nutrients::$all as $nutrient) {
foreach (Nutrients::all() as $nutrient) {
$options[$nutrient['value']] = [
'value' => $nutrient['value'],
'label' => $nutrient['label'],

View File

@ -101,7 +101,7 @@ final class IngredientAmount extends Model
*/
public function getNutrientsSummaryAttribute(): string {
$summary = [];
foreach (Nutrients::$all as $nutrient) {
foreach (Nutrients::all() as $nutrient) {
$amount = round($this->{$nutrient['value']}(), 2);
$summary[] = "{$nutrient['label']}: {$amount}";
}

View File

@ -4,20 +4,12 @@ namespace App\Support;
use App\Models\Food;
use App\Models\Recipe;
use Illuminate\Support\Collection;
class Nutrients
{
public static float $gramsPerOunce = 28.349523125;
public static array $all = [
'calories' => ['value' => 'calories', 'label' => 'calories', 'unit' => null],
'carbohydrates' => ['value' => 'carbohydrates', 'label' => 'carbohydrates', 'unit' => 'g'],
'cholesterol' => ['value' => 'cholesterol', 'label' => 'cholesterol', 'unit' => 'mg'],
'fat' => ['value' => 'fat', 'label' => 'fat', 'unit' => 'g'],
'protein' => ['value' => 'protein', 'label' => 'protein', 'unit' => 'g'],
'sodium' => ['value' => 'sodium', 'label' => 'sodium', 'unit' => 'mg'],
];
public static array $units = [
'cup' => ['value' => 'cup', 'label' => 'cup'],
'gram' => ['value' => 'gram', 'label' => 'grams'],
@ -27,6 +19,56 @@ class Nutrients
'tsp' => ['value' => 'tsp', 'label' => 'tsp.'],
];
/**
* Get all trackable "nutrients" (calories are not technically a nutrient).
*
* Each entry has four keys:
* - value: Machine name for the entry.
* - label: Human-readable name for the entry.
* - unit: Unit of measure for the entry.
* - weight: Sort weight for presentation.
*/
public static function all(): Collection {
return new Collection([
'calories' => [
'value' => 'calories',
'label' => 'calories',
'unit' => null,
'weight' => 0,
],
'carbohydrates' => [
'value' => 'carbohydrates',
'label' => 'carbohydrates',
'unit' => 'g',
'weight' => 40,
],
'cholesterol' => [
'value' => 'cholesterol',
'label' => 'cholesterol',
'unit' => 'mg',
'weight' => 20,
],
'fat' => [
'value' => 'fat',
'label' => 'fat',
'unit' => 'g',
'weight' => 10,
],
'protein' => [
'value' => 'protein',
'label' => 'protein',
'unit' => 'g',
'weight' => 50,
],
'sodium' => [
'value' => 'sodium',
'label' => 'sodium',
'unit' => 'mg',
'weight' => 30,
],
]);
}
/**
* Calculate a nutrient multiplier for a Food.
*

View File

@ -110,7 +110,7 @@
</div>
<div class="flex flex-col space-y-4 md:flex-row md:space-y-0">
@foreach (\App\Support\Nutrients::$all as $nutrient)
@foreach (\App\Support\Nutrients::all()->sortBy('weight') as $nutrient)
<!-- {{ ucfirst($nutrient['value']) }} -->
<div class="flex-auto">
<x-inputs.label for="{{ $nutrient['value'] }}"

View File

@ -52,7 +52,7 @@
</div>
<div class="flex flex-col space-y-4 md:flex-row md:space-x-4 md:space-y-0 w-full">
@foreach (\App\Support\Nutrients::$all as $nutrient)
@foreach (\App\Support\Nutrients::all()->sortBy('weight') as $nutrient)
<!-- {{ ucfirst($nutrient['value']) }} -->
<div class="flex-auto">
<x-inputs.label for="{{ $nutrient['value'] }}"

View File

@ -107,7 +107,7 @@
<div class="ml-2 w-full"><hr/></div>
</div>
<span class="text-sm text-gray-500">
@foreach(\App\Support\Nutrients::$all as $nutrient)
@foreach(\App\Support\Nutrients::all()->sortBy('weight') as $nutrient)
{{ round($entries->where('meal', $meal)->sum($nutrient['value']), 2) }}{{ $nutrient['unit'] }}
{{ $nutrient['value'] }}@if(!$loop->last), @endif
@endforeach
@ -127,7 +127,7 @@
</div>
<div>
<span class="font-bold">nutrients:</span>
@foreach(\App\Support\Nutrients::$all as $nutrient)
@foreach(\App\Support\Nutrients::all()->sortBy('weight') as $nutrient)
{{ round($entry->{$nutrient['value']}, 2) }}{{ $nutrient['unit'] }}
{{ $nutrient['value'] }}@if(!$loop->last), @endif
@endforeach