forked from eatvio/eatvio-infra
92 lines
2.9 KiB
PHP
92 lines
2.9 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace Database\Factories;
|
|
|
|
use Faker\Provider\Youtube;
|
|
use FakerRestaurant\Provider\en_US\Restaurant;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
use Illuminate\Http\File;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Plan>
|
|
*/
|
|
class RecipeFactory 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));
|
|
$this->faker->addProvider(new Youtube($faker));
|
|
|
|
return [
|
|
'title' => $this->faker->foodName(),
|
|
'image' => $this->generateImageFile('recipes'),
|
|
'state' => $this->faker->randomElement(['active', 'archived']),
|
|
'cooking_time' => $this->faker->randomElement(['10', '20', '30', '60', '120', '14400']),
|
|
'ingredients' => $this->createQuentityIngredients($this->faker->randomDigitNotNull()), // quentity - от 1 до 10 randomDigitNotNull
|
|
'type' => $this->faker->randomElement(['first_meals', 'second_meals', 'salads', 'snacks', 'drinks', 'other']),
|
|
'description' => $this->faker->text(),
|
|
'steps' => $this->createQuentitySteps($this->faker->randomDigitNotNull()),
|
|
'video_url' => $this->faker->youtubeRandomUri(),
|
|
];
|
|
}
|
|
|
|
private function generateImageFile(string $name)
|
|
{
|
|
$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/recipes/' . $images[$imageFileKey];
|
|
$file = new File($imageFilePath);
|
|
$fileLocalPath = Storage::putFile($name, $file, 'public');
|
|
|
|
return $fileLocalPath;
|
|
}
|
|
|
|
private function generateIngredients(): array
|
|
{
|
|
return [
|
|
'name' => $this->faker->foodName(),
|
|
'unit' => $this->faker->randomElement(['gr', 'teespons', 'tablespoon', 'ml', 'pcs', 'clove', 'to_taste']),
|
|
'portion' => $this->faker->randomNumber(3, true),
|
|
];
|
|
}
|
|
|
|
private function generateSteps(): array
|
|
{
|
|
return [
|
|
'image' => $this->generateImageFile('steps'),
|
|
'description' => $this->faker->text(),
|
|
];
|
|
}
|
|
|
|
private function createQuentitySteps($count)
|
|
{
|
|
$result = [];
|
|
|
|
for ($i = 0; $i < $count; ++$i) {
|
|
$result[] = $this->generateSteps();
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
private function createQuentityIngredients($count)
|
|
{
|
|
$result = [];
|
|
|
|
for ($i = 0; $i < $count; ++$i) {
|
|
$result[] = $this->generateIngredients();
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|