generated from liutk/owl-admin-base
41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
use App\Models\Train\Question;
|
|
use App\Enums\QuestionCate;
|
|
|
|
/**
|
|
* @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))];
|
|
} else if ($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,
|
|
];
|
|
}
|
|
}
|