ldc-com-app-bo/app/Models/Formulary.php
2023-07-20 14:37:00 +02:00

115 lines
2.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 InputTypeEnum $type
* @property string $name
* @property string $placeholder
* @property string $details
* @property boolean $visible
* @property boolean $required
* @property InputStatusEnum $status
*/
class Formulary extends Model
{
use HasFactory;
use CastsEnums;
/**
* fillable
* @var string[]
*/
protected $fillable = [
'type',
'name',
'slug',
'placeholder',
'details',
'visible',
'required',
'status',
];
/**
* @var string[]
*/
protected $casts = [
'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::updating(function (self $model) {
static::setSlug($model);
});
}
// * METHODS
/**
* Set the slug.
*
* @param self $model
*
* @return void
*/
private static function setSlug(self $model)
{
$model->slug = Str::slug($model->name);
}
/**
* 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;
}
}