From 6b406a63cad9ab7168a918dd81e6ac0c4b8a9fbb Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Fri, 22 Jan 2021 22:31:59 -0800 Subject: [PATCH] Update nutrient calculation for IngredientAmount --- app/Models/Food.php | 2 ++ app/Models/FoodAmount.php | 1 + app/Models/IngredientAmount.php | 47 +++++++++++++++++++++++++--- app/Models/JournalEntry.php | 2 ++ app/Models/Recipe.php | 31 +++--------------- app/Models/Traits/HasIngredients.php | 28 +++++++++++++++++ 6 files changed, 79 insertions(+), 32 deletions(-) diff --git a/app/Models/Food.php b/app/Models/Food.php index 7dbb243..86f6539 100644 --- a/app/Models/Food.php +++ b/app/Models/Food.php @@ -55,6 +55,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany; * @method static \Illuminate\Database\Eloquent\Builder|Food whereUpdatedAt($value) * @mixin \Eloquent * @property-read string $serving_size_formatted + * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmounts + * @property-read int|null $ingredient_amounts_count */ class Food extends Model { diff --git a/app/Models/FoodAmount.php b/app/Models/FoodAmount.php index 232a568..85e32bf 100644 --- a/app/Models/FoodAmount.php +++ b/app/Models/FoodAmount.php @@ -35,6 +35,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; * @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereUpdatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereWeight($value) * @mixin \Eloquent + * @property-read string $amount_formatted */ class FoodAmount extends Model { diff --git a/app/Models/IngredientAmount.php b/app/Models/IngredientAmount.php index 4bf80b9..5cb3dd7 100644 --- a/app/Models/IngredientAmount.php +++ b/app/Models/IngredientAmount.php @@ -8,6 +8,39 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +/** + * App\Models\IngredientAmount + * + * @property int $id + * @property int $ingredient_id + * @property string $ingredient_type + * @property float $amount + * @property string|null $unit + * @property string|null $detail + * @property int $weight + * @property int $parent_id + * @property string $parent_type + * @property \Illuminate\Support\Carbon|null $created_at + * @property \Illuminate\Support\Carbon|null $updated_at + * @property-read string $amount_formatted + * @property-read Model|\Eloquent $ingredient + * @property-read Model|\Eloquent $parent + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount query() + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereAmount($value) + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereDetail($value) + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereIngredientId($value) + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereIngredientType($value) + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereParentId($value) + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereParentType($value) + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereUnit($value) + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereUpdatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereWeight($value) + * @mixin \Eloquent + */ class IngredientAmount extends Model { use HasFactory; @@ -85,11 +118,15 @@ class IngredientAmount extends Model */ public function __call($method, $parameters): mixed { if (in_array($method, $this->nutrientMethods)) { - return $this->ingredient->{$method} * Nutrients::calculateFoodNutrientMultiplier( - $this->ingredient, - $this->amount, - $this->unit - ); + return match ($this->ingredient::class) { + Food::class => $this->ingredient->{$method} * Nutrients::calculateFoodNutrientMultiplier( + $this->ingredient, + $this->amount, + $this->unit + ), + Recipe::class => $this->ingredient->{"{$method}Total"}(), + default => 0 + }; } else { return parent::__call($method, $parameters); diff --git a/app/Models/JournalEntry.php b/app/Models/JournalEntry.php index 090d50a..8f771a5 100644 --- a/app/Models/JournalEntry.php +++ b/app/Models/JournalEntry.php @@ -46,6 +46,8 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany; * @method static \Illuminate\Database\Eloquent\Builder|JournalEntry whereUpdatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|JournalEntry whereUserId($value) * @mixin \Eloquent + * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredients + * @property-read int|null $ingredients_count */ class JournalEntry extends Model { diff --git a/app/Models/Recipe.php b/app/Models/Recipe.php index c7906af..adc9711 100644 --- a/app/Models/Recipe.php +++ b/app/Models/Recipe.php @@ -40,6 +40,10 @@ use Illuminate\Database\Eloquent\Relations\HasMany; * @method static \Illuminate\Database\Eloquent\Builder|Recipe whereSource($value) * @method static \Illuminate\Database\Eloquent\Builder|Recipe whereUpdatedAt($value) * @mixin \Eloquent + * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmounts + * @property-read int|null $ingredient_amounts_count + * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredients + * @property-read int|null $ingredients_count */ class Recipe extends Model { @@ -62,18 +66,6 @@ class Recipe extends Model 'servings' => 'int', ]; - /** - * Nutrient total calculation methods. - */ - private array $nutrientTotalMethods = [ - 'caloriesTotal', - 'carbohydratesTotal', - 'cholesterolTotal', - 'fatTotal', - 'proteinTotal', - 'sodiumTotal', - ]; - /** * Nutrient per serving methods. */ @@ -136,19 +128,4 @@ class Recipe extends Model } } - /** - * Sum a specific nutrient for all food amounts. - * - * @param string $nutrient - * Nutrient to sum. - * - * @return float - */ - private function sumNutrient(string $nutrient): float { - $sum = 0; - foreach ($this->foodAmounts as $foodAmount) { - $sum += $foodAmount->{$nutrient}(); - } - return $sum; - } } diff --git a/app/Models/Traits/HasIngredients.php b/app/Models/Traits/HasIngredients.php index 58643c5..c573133 100644 --- a/app/Models/Traits/HasIngredients.php +++ b/app/Models/Traits/HasIngredients.php @@ -7,10 +7,38 @@ use Illuminate\Database\Eloquent\Relations\MorphMany; trait HasIngredients { + /** + * Nutrient total calculation methods. + */ + protected array $nutrientTotalMethods = [ + 'caloriesTotal', + 'carbohydratesTotal', + 'cholesterolTotal', + 'fatTotal', + 'proteinTotal', + 'sodiumTotal', + ]; + /** * Get all of the ingredients. */ public function ingredients(): MorphMany { return $this->morphMany(IngredientAmount::class, 'parent'); } + + /** + * Sum a specific nutrient for all ingredients. + * + * @param string $nutrient + * Nutrient to sum. + * + * @return float + */ + private function sumNutrient(string $nutrient): float { + $sum = 0; + foreach ($this->ingredients as $ingredientAmount) { + $sum += $ingredientAmount->{$nutrient}(); + } + return $sum; + } }