From 29638c711f4e74eb29f00a706a0c70a8bfe34f03 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Tue, 30 Mar 2021 16:14:31 -0700 Subject: [PATCH] Replace meals static array with Collection --- .../Controllers/JournalEntryController.php | 17 +++++++--- app/Models/JournalEntry.php | 32 +++++++++++++++---- ...31_180016_create_journal_entries_table.php | 3 +- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index 6bf2dc3..43df249 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -96,7 +96,7 @@ class JournalEntryController extends Controller return view('journal-entries.create') ->with('ingredients', $ingredients) - ->with('meals', JournalEntry::$meals) + ->with('meals', JournalEntry::meals()->toArray()) ->with('units', Nutrients::units()->toArray()) ->with('default_date', Carbon::createFromFormat('Y-m-d', $date)); } @@ -108,7 +108,7 @@ class JournalEntryController extends Controller { $date = $request->date ?? Carbon::now()->toDateString(); return view('journal-entries.create-from-nutrients') - ->with('meals', JournalEntry::$meals) + ->with('meals', JournalEntry::meals()->toArray()) ->with('units', Nutrients::units()->toArray()) ->with('default_date', Carbon::createFromFormat('Y-m-d', $date)); } @@ -122,7 +122,12 @@ class JournalEntryController extends Controller 'ingredients.date' => ['required', 'array', new ArrayNotEmpty], 'ingredients.date.*' => ['nullable', 'date', 'required_with:ingredients.id.*'], 'ingredients.meal' => ['required', 'array', new ArrayNotEmpty], - 'ingredients.meal.*' => ['nullable', 'string', 'required_with:ingredients.id.*', new InArray(array_column(JournalEntry::$meals, 'value'))], + 'ingredients.meal.*' => [ + 'nullable', + 'string', + 'required_with:ingredients.id.*', + new InArray(JournalEntry::meals()->pluck('value')->toArray()) + ], 'ingredients.amount' => ['required', 'array', new ArrayNotEmpty], 'ingredients.amount.*' => ['required_with:ingredients.id.*', 'nullable', new StringIsDecimalOrFraction], 'ingredients.unit' => ['required', 'array'], @@ -225,7 +230,11 @@ class JournalEntryController extends Controller public function storeFromNutrients(Request $request): RedirectResponse { $attributes = $request->validate([ 'date' => ['required', 'date'], - 'meal' => ['required', 'string', new InArray(array_column(JournalEntry::$meals, 'value'))], + 'meal' => [ + 'required', + 'string', + new InArray(JournalEntry::meals()->pluck('value')->toArray()) + ], 'summary' => ['required', 'string'], 'calories' => ['nullable', 'required_without_all:fat,cholesterol,sodium,carbohydrates,protein', 'numeric'], 'fat' => ['nullable', 'required_without_all:calories,cholesterol,sodium,carbohydrates,protein', 'numeric'], diff --git a/app/Models/JournalEntry.php b/app/Models/JournalEntry.php index 1b07d08..71c347a 100644 --- a/app/Models/JournalEntry.php +++ b/app/Models/JournalEntry.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphToMany; +use Illuminate\Support\Collection; /** * App\Models\JournalEntry @@ -88,14 +89,31 @@ final class JournalEntry extends Model protected $with = ['user', 'foods:id,name,slug', 'recipes:id,name,slug']; /** - * Valid meal options. + * Get all supported meals and metadata. + * + * Each entry has two keys: + * - value: Machine name for the meal. + * - label: Human-readable name for the meal. + * + * @return \Illuminate\Support\Collection */ - public static array $meals = [ - ['value' => 'breakfast', 'label' => 'Breakfast'], - ['value' => 'lunch', 'label' => 'Lunch'], - ['value' => 'dinner', 'label' => 'Dinner'], - ['value' => 'snacks', 'label' => 'Snacks'], - ]; + public static function meals(): Collection { + return new Collection([ + [ + 'value' => 'breakfast', + 'label' => 'Breakfast' + ], + [ + 'value' => 'lunch', + 'label' => 'Lunch'], + [ + 'value' => 'dinner', + 'label' => 'Dinner'], + [ + 'value' => 'snacks', + 'label' => 'Snacks'], + ]); + } /** * Get the User this entry belongs to. diff --git a/database/migrations/2020_12_31_180016_create_journal_entries_table.php b/database/migrations/2020_12_31_180016_create_journal_entries_table.php index 01836f4..064e7b8 100644 --- a/database/migrations/2020_12_31_180016_create_journal_entries_table.php +++ b/database/migrations/2020_12_31_180016_create_journal_entries_table.php @@ -1,5 +1,6 @@ unsignedFloat('sodium')->default(0); $table->unsignedFloat('carbohydrates')->default(0); $table->unsignedFloat('protein')->default(0); - $table->enum('meal', ['breakfast', 'lunch', 'dinner', 'snacks']); + $table->enum('meal', JournalEntry::meals()->pluck('value')->toArray()); $table->timestamps(); }); }