Files
eatvio-infra/database/factories/ProductNutritionalValueFactory.php

58 lines
2.1 KiB
PHP

<?php
namespace Database\Factories;
use FakerRestaurant\Provider\en_US\Restaurant;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\File;
use Faker\Generator;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\ProductNutritionalValue>
* @property Generator|Restaurant $faker
*/ class ProductNutritionalValueFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*
*/
public function definition(): array
{
$faker = \Faker\Factory::create();
$this->faker->addProvider(new Restaurant($faker));
$active = $this->faker->boolean();
return [
'name' => $this->faker->regexify('[A-Za-z0-9]{20}'),
'image' => $this->generateImageFile(),
'description' => $this->faker->text(),
'formulations' => $this->faker->text(),
'energy_on_100gr' => $this->faker->numberBetween(0,100),
'proteins_on_100gr' => $this->faker->randomFloat(1, 20, 50),
'fats_on_100gr' => $this->faker->randomFloat(1, 20, 50),
'carbohydrates_on_100gr' => $this->faker->randomFloat(1, 20, 50),
'portion_mass_gr' => $this->faker->numberBetween(0, 100),
'energy_on_portion' => $this->faker->randomFloat(1,20,50),
'proteins_on_portion' => $this->faker->randomFloat(1,20,50),
'fats_on_portion' => $this->faker->randomFloat(1,20,50),
'carbohydrates_on_portion' => $this->faker->randomFloat(1,20,50),
];
}
private function generateImageFile(string $name = 'plans'): string
{
$images = ['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg', '7.jpg', '8.jpg', '9.jpg', '10.jpg'];
Storage::fake($name);
$imageFileKey = $this->faker->numberBetween(0, 9);
$imageFilePath = 'database/assets/images/plans/' . $images[$imageFileKey];
$file = new File($imageFilePath);
$fileLocalPath = Storage::putFile($name, $file, 'public');
return $fileLocalPath;
}
}