From 2f91c320efcdaa59cdf318e6978a9aa602bb65c9 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sun, 21 Feb 2021 08:23:30 -0800 Subject: [PATCH] Refactor nutrients data array as a collection --- app/Http/Controllers/FoodController.php | 6 +- .../Controllers/JournalEntryController.php | 18 +++--- app/Models/Goal.php | 2 +- app/Models/IngredientAmount.php | 2 +- app/Support/Nutrients.php | 60 ++++++++++++++++--- resources/views/foods/edit.blade.php | 2 +- .../create-from-nutrients.blade.php | 2 +- .../views/journal-entries/index.blade.php | 4 +- 8 files changed, 69 insertions(+), 27 deletions(-) diff --git a/app/Http/Controllers/FoodController.php b/app/Http/Controllers/FoodController.php index b5010d0..1334a41 100644 --- a/app/Http/Controllers/FoodController.php +++ b/app/Http/Controllers/FoodController.php @@ -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; } } diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index e0db472..a6c19cc 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -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'] ); diff --git a/app/Models/Goal.php b/app/Models/Goal.php index 691e6b7..31c2af1 100644 --- a/app/Models/Goal.php +++ b/app/Models/Goal.php @@ -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'], diff --git a/app/Models/IngredientAmount.php b/app/Models/IngredientAmount.php index 2eb869d..3931629 100644 --- a/app/Models/IngredientAmount.php +++ b/app/Models/IngredientAmount.php @@ -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}"; } diff --git a/app/Support/Nutrients.php b/app/Support/Nutrients.php index 8d4ecda..346e39d 100644 --- a/app/Support/Nutrients.php +++ b/app/Support/Nutrients.php @@ -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. * diff --git a/resources/views/foods/edit.blade.php b/resources/views/foods/edit.blade.php index 106b8ff..749080e 100644 --- a/resources/views/foods/edit.blade.php +++ b/resources/views/foods/edit.blade.php @@ -110,7 +110,7 @@
- @foreach (\App\Support\Nutrients::$all as $nutrient) + @foreach (\App\Support\Nutrients::all()->sortBy('weight') as $nutrient)
- @foreach (\App\Support\Nutrients::$all as $nutrient) + @foreach (\App\Support\Nutrients::all()->sortBy('weight') as $nutrient)

- @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 @@
nutrients: - @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