store-manage/database/factories/QuestionFactory.php

43 lines
1.1 KiB
PHP

<?php
namespace Database\Factories;
use App\Enums\QuestionCate;
use App\Models\Train\Question;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Model>
*/
class QuestionFactory extends Factory
{
protected $model = Question::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
$cate = $this->faker->randomElement(QuestionCate::class);
$options = [];
$max = 4;
$index = [];
if ($cate == QuestionCate::Radio) {
$index = [$this->faker->randomElement(range(0, $max - 1))];
} elseif ($cate == QuestionCate::Checkbox) {
$index = $this->faker->randomElements(range(0, $max - 1), $this->faker->numberBetween(2, 4));
}
for ($i = 0; $i < $max; $i++) {
array_push($options, ['text' => $this->faker->word, 'is_true' => in_array($i, $index)]);
}
return [
'title' => $this->faker->sentence,
'cate' => $cate,
'options' => $options,
];
}
}