- formulary api output - Formulary seeder slug unique - rename Formulary visible column because the prop is hidden by Model trait Concerns\HideAttributes - mandatory input phone was not type of phone - form factory to be status default - formulary factory type
136 lines
3 KiB
PHP
136 lines
3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Formularies\InputStatusEnum;
|
|
use App\Enums\Formularies\InputTypeEnum;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
|
|
/**
|
|
* @property integer $id
|
|
* @property \App\Enums\Formularies\InputTypeEnum $type
|
|
* @property string $name
|
|
* @property string $slug
|
|
* @property array<string,string> $placeholder
|
|
* @property array<string,array<string,string>> $details
|
|
* @property boolean $is_visible
|
|
* @property boolean $required
|
|
* @property \App\Enums\Formularies\InputStatusEnum $status
|
|
* @property integer $order
|
|
*/
|
|
class Formulary extends Model
|
|
{
|
|
use HasFactory;
|
|
use CastsEnums;
|
|
|
|
/**
|
|
* fillable
|
|
* @var string[]
|
|
*/
|
|
protected $fillable = [
|
|
'type',
|
|
'name',
|
|
'slug',
|
|
'placeholder',
|
|
'details',
|
|
'is_visible',
|
|
'required',
|
|
'status',
|
|
];
|
|
|
|
/**
|
|
* @var string[]
|
|
*/
|
|
protected $casts = [
|
|
'required' => 'bool',
|
|
'is_visible' => 'bool',
|
|
'placeholder' => 'json',
|
|
'details' => 'json',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $enumCasts = [
|
|
'type' => InputTypeEnum::class,
|
|
'status' => InputStatusEnum::class,
|
|
];
|
|
|
|
/**
|
|
* The "booted" method of the model.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
static::setSlug($model);
|
|
static::setStatusField($model);
|
|
static::setOrder($model);
|
|
static::makeRequiredSense($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
static::setSlug($model);
|
|
static::makeRequiredSense($model);
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Set the slug.
|
|
*
|
|
* @param self $model
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function setSlug(self $model)
|
|
{
|
|
$model->slug = Str::slug($model->name);
|
|
}
|
|
|
|
/**
|
|
* Set required logical
|
|
*
|
|
* @param self $model
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function makeRequiredSense(self $model)
|
|
{
|
|
if ($model->is_visible == false) {
|
|
$model->required = false;
|
|
\session()->flash(trans('Le champ ne peut être obligatoire si il n\'est pas visible'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set the status field.
|
|
*
|
|
* @param self $model
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function setStatusField(self $model)
|
|
{
|
|
if (!isset($model->status)) {
|
|
$model->status = InputStatusEnum::default();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function setOrder(self $model): void
|
|
{
|
|
$model->order = \intval(self::query()->max('order')) + 1;
|
|
}
|
|
}
|