From 3060bdbb7c07aa817114d0b08c8e1ca243bd756e Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Tue, 22 Dec 2020 19:55:33 -0800 Subject: [PATCH] Add support for sodium and cholesterol tracking --- app/Models/Ingredient.php | 22 ++++--- app/Models/IngredientAmount.php | 57 +++++++++++-------- ..._12_21_214128_create_ingredients_table.php | 6 +- database/seeders/IngredientSeeder.php | 32 +++++------ 4 files changed, 67 insertions(+), 50 deletions(-) diff --git a/app/Models/Ingredient.php b/app/Models/Ingredient.php index 76714e3..47ca476 100644 --- a/app/Models/Ingredient.php +++ b/app/Models/Ingredient.php @@ -9,10 +9,12 @@ use Illuminate\Database\Eloquent\Model; * @property int id * @property string name Ingredient base name. * @property ?string detail Some additional detail about the ingredient (e.g. "small" with the name "onion"). - * @property float calories (per 100g). - * @property float protein (per 100g). - * @property float fat (per 100g). * @property float carbohydrates (per 100g). + * @property float calories (per 100g). + * @property float cholesterol (per 100g). + * @property float fat (per 100g). + * @property float protein (per 100g). + * @property float sodium (per 100g). * @property ?float unit_weight Weight of one cup of the ingredient. * @property ?float cup_weight Weight of one "unit" (e.g. an egg, onion, etc.) of the ingredient. */ @@ -27,9 +29,11 @@ class Ingredient extends Model 'name', 'detail', 'calories', - 'protein', - 'fat', 'carbohydrates', + 'cholesterol', + 'fat', + 'protein', + 'sodium', 'unitWeight', 'cupWeight', ]; @@ -39,10 +43,12 @@ class Ingredient extends Model */ protected array $casts = [ 'calories' => 'float', - 'protein' => 'float', - 'fat' => 'float', 'carbohydrates' => 'float', - 'unit_weight' => 'float', + 'cholesterol' => 'float', 'cup_weight' => 'float', + 'fat' => 'float', + 'protein' => 'float', + 'sodium' => 'float', + 'unit_weight' => 'float', ]; } diff --git a/app/Models/IngredientAmount.php b/app/Models/IngredientAmount.php index 0e71806..f6eb7f4 100644 --- a/app/Models/IngredientAmount.php +++ b/app/Models/IngredientAmount.php @@ -13,6 +13,12 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; * @property int weight Weight of ingredient in full ingredient list (lowest first). * @property \App\Models\Ingredient ingredient * @property \App\Models\Recipe recipe + * @method float calories Get total calories. + * @method float carbohydrates Get total carbohydrates. + * @method float cholesterol Get total cholesterol. + * @method float fat Get total fat. + * @method float protein Get total protein. + * @method float sodium Get total sodium. */ class IngredientAmount extends Model { @@ -40,6 +46,18 @@ class IngredientAmount extends Model */ protected array $with = ['ingredient']; + /** + * Nutrient calculation methods. + */ + private array $nutrientMethods = [ + 'calories', + 'carbohydrates', + 'cholesterol', + 'fat', + 'protein', + 'sodium', + ]; + /** * Get the Ingredient this amount belongs to. */ @@ -55,31 +73,22 @@ class IngredientAmount extends Model } /** - * Get total calories for the ingredient amount. + * Add nutrient calculations handling overloading. + * + * @param string $method + * @param array $parameters + * + * @return mixed + * + * @noinspection PhpMissingParamTypeInspection */ - public function calories(): float { - return $this->ingredient->calories * $this->unitMultiplier(); - } - - /** - * Get total protein for the ingredient amount. - */ - public function protein(): float { - return $this->ingredient->protein * $this->unitMultiplier(); - } - - /** - * Get total fat for the ingredient amount. - */ - public function fat(): float { - return $this->ingredient->fat * $this->unitMultiplier(); - } - - /** - * Get total carbohydrates for the ingredient amount. - */ - public function carbohydrates(): float { - return $this->ingredient->carbohydrates * $this->unitMultiplier(); + public function __call($method, $parameters): mixed { + if (in_array($method, $this->nutrientMethods)) { + return $this->ingredient->{$method} * $this->unitMultiplier(); + } + else { + return parent::__call($method, $parameters); + } } /** diff --git a/database/migrations/2020_12_21_214128_create_ingredients_table.php b/database/migrations/2020_12_21_214128_create_ingredients_table.php index 025ee8d..bb7140d 100644 --- a/database/migrations/2020_12_21_214128_create_ingredients_table.php +++ b/database/migrations/2020_12_21_214128_create_ingredients_table.php @@ -16,9 +16,11 @@ class CreateIngredientsTable extends Migration $table->string('name'); $table->string('detail')->nullable(); $table->unsignedFloat('calories')->default(0); - $table->unsignedFloat('protein')->default(0); - $table->unsignedFloat('fat')->default(0); $table->unsignedFloat('carbohydrates')->default(0); + $table->unsignedFloat('cholesterol')->default(0); + $table->unsignedFloat('fat')->default(0); + $table->unsignedFloat('protein')->default(0); + $table->unsignedFloat('sodium')->default(0); $table->unsignedFloat('unit_weight')->nullable(); $table->unsignedFloat('cup_weight')->nullable(); $table->timestamps(); diff --git a/database/seeders/IngredientSeeder.php b/database/seeders/IngredientSeeder.php index e74d5a1..8aa4eca 100644 --- a/database/seeders/IngredientSeeder.php +++ b/database/seeders/IngredientSeeder.php @@ -16,62 +16,62 @@ class IngredientSeeder extends Seeder [ 'name' => 'baking powder', 'calories' => 53, - 'protein' => 0, - 'fat' => 0, 'carbohydrates' => 27.7, + 'sodium' => 10.6, 'cup_weight' => 220.8, ], [ 'name' => 'egg', 'detail' => 'large', 'calories' => 147, - 'protein' => 12.4, - 'fat' => 9.96, 'carbohydrates' => 0.96, + 'cholesterol' => 0.411, + 'fat' => 9.96, + 'protein' => 12.4, + 'sodium' => 0.129, 'unit_weight' => 50.3, ], [ 'name' => 'flour', 'detail' => 'all-purpose', 'calories' => 364, - 'protein' => 10.33, - 'fat' => 0.98, 'carbohydrates' => 76.31, + 'fat' => 0.98, + 'protein' => 10.33, + 'sodium' => 0.004, 'cup_weight' => 125, ], [ 'name' => 'milk', 'detail' => 'whole', 'calories' => 60, - 'protein' => 3.28, - 'fat' => 3.2, 'carbohydrates' => 4.67, + 'cholesterol' => 0.012, + 'fat' => 3.2, + 'protein' => 3.28, + 'sodium' => 0.038, 'cup_weight' => 244, ], [ 'name' => 'salt', 'detail' => 'table', - 'calories' => 0, - 'protein' => 0, - 'fat' => 0, - 'carbohydrates' => 0, + 'sodium' => 38.758, 'cup_weight' => 292, ], [ 'name' => 'sugar', 'detail' => 'white', 'calories' => 385, - 'protein' => 0, - 'fat' => 0.32, 'carbohydrates' => 99.6, + 'fat' => 0.32, + 'protein' => 0, + 'sodium' => 0.001, 'cup_weight' => 200, ], [ 'name' => 'vegetable oil', 'calories' => 886, - 'protein' => 0, 'fat' => 100, - 'carbohydrates' => 0, 'cup_weight' => 224, ], ];