Update nutrient calculation for IngredientAmount

This commit is contained in:
Christopher C. Wells 2021-01-22 22:31:59 -08:00 committed by Christopher Charbonneau Wells
parent 5ded6bfce6
commit 19eac25413
6 changed files with 79 additions and 32 deletions

View File

@ -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
{

View File

@ -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
{

View File

@ -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);

View File

@ -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
{

View File

@ -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;
}
}

View File

@ -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;
}
}