ldc-com-app-bo/database/factories/ContactFactory.php

82 lines
3 KiB
PHP
Raw Normal View History

<?php
namespace Database\Factories;
use App\Enums\Formularies\InputTypeEnum;
use App\Lib\Inputs\Definition\InputData;
use App\Models\Contact;
use App\Models\Formulary;
use Illuminate\Database\Eloquent\Factories\Factory;
class ContactFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Contact::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Formulary> */
$formularies = $this->factoryForModel(Formulary::class)->count($this->faker->numberBetween(3, 9))->make();
return [
'email' => $this->faker->unique()->email(),
'lastname' => $this->faker->boolean() ? $this->faker->unique()->lastName() : '',
'firstname' => $this->faker->boolean() ? $this->faker->unique()->firstName() : '',
'phone' => $this->faker->boolean() ? $this->faker->unique()->phoneNumber() : '',
'notes' => $this->faker->boolean() ? $this->faker->text() : null,
'visits' => \collect(range(0, $this->faker->numberBetween(1, 5)))->mapWithKeys(function () {
return [
'title' => 'Visite du ' . $this->faker->dateTime()->format('D/M/Y H:i'),
'note' => $this->faker->text()
];
}),
'data' => $formularies->map(function (Formulary $formulary) {
$inputClass = $formulary->type->inputClass;
$details = \collect($formulary->details)->map(function (array $details) {
return $details['fr'];
});
$data = $this->faker->words(3, true);
if ($formulary->type->equals(InputTypeEnum::email())) {
$data = $this->faker->unique()->email();
}
if ($formulary->type->equals(InputTypeEnum::phone())) {
$data = $this->faker->phoneNumber();
}
if (
in_array($formulary->type->value, [
InputTypeEnum::selectMultiple()->value,
InputTypeEnum::checkbox()->value
])
) {
$data = \collect($details->random(min($details->count(), 2)))->all();
}
if (
in_array($formulary->type->value, [
InputTypeEnum::select()->value,
InputTypeEnum::radio()->value
])
) {
$data = $details->random();
}
return $inputClass::from(InputData::make(
$formulary->placeholder['fr'],
$formulary->type,
$data,
$formulary->required
), $formulary->required);
})
];
}
}