From e3e84c2e13585f3ade4b85fad0aea0cdbdb22bee Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Tue, 22 Dec 2020 15:11:13 -0800 Subject: [PATCH] Add nutrients per serving methods to Recipe --- app/Models/IngredientAmount.php | 5 +++++ app/Models/Recipe.php | 36 +++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/app/Models/IngredientAmount.php b/app/Models/IngredientAmount.php index 2d58993..0e71806 100644 --- a/app/Models/IngredientAmount.php +++ b/app/Models/IngredientAmount.php @@ -84,6 +84,11 @@ class IngredientAmount extends Model /** * Get the multiplier for the ingredient unit based on weight. + * + * Unit weight will be specified for ingredients that are added by unit + * (e.g. eggs, vegetables, etc.) and cup weight (the weight of the + * ingredient equal to one cup) will be specified for ingredients that are + * measured (e.g. flour, milk, etc.). */ private function unitMultiplier(): float { return match ($this->unit) { diff --git a/app/Models/Recipe.php b/app/Models/Recipe.php index a643d5e..8d18b56 100644 --- a/app/Models/Recipe.php +++ b/app/Models/Recipe.php @@ -53,33 +53,61 @@ class Recipe extends Model } /** - * Get total recipe calories. + * Get total calories. */ public function caloriesTotal(): float { return $this->sumNutrient('calories'); } /** - * Get total recipe protein. + * Get per serving calories. + */ + public function caloriesPerServing(): float { + return $this->caloriesTotal() / $this->servings; + } + + /** + * Get total protein. */ public function proteinTotal(): float { return $this->sumNutrient('protein'); } /** - * Get total recipe fat. + * Get per serving protein. + */ + public function proteinPerServing(): float { + return $this->proteinTotal() / $this->servings; + } + + /** + * Get total fat. */ public function fatTotal(): float { return $this->sumNutrient('fat'); } /** - * Get total recipe carbohydrates. + * Get per serving fat. + */ + public function fatPerServing(): float { + return $this->fatTotal() / $this->servings; + } + + /** + * Get total carbohydrates. */ public function carbohydratesTotal(): float { return $this->sumNutrient('carbohydrates'); } + /** + * Get per serving carbohydrates. + */ + public function carbohydratesPerServing(): float { + return $this->carbohydratesTotal() / $this->servings; + } + /** * Sum a specific nutrient for all ingredient amounts. *