ldc-com-app-bo/app/Models/Contact.php

102 lines
2.4 KiB
PHP
Raw Normal View History

2023-07-13 08:34:54 +00:00
<?php
namespace App\Models;
use App\Casts\AsArrayOfInput;
use App\Lib\Inputs\InputEmail;
use App\Lib\Inputs\InputPhone;
use App\Lib\Inputs\InputText;
use App\Traits\Models\HasUuidKey;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-07-13 08:34:54 +00:00
use Illuminate\Database\Eloquent\Model;
use Kwaadpepper\Enum\Traits\CastsEnums;
/**
* @property integer $app_user_id
* @property string $id
2023-07-13 08:34:54 +00:00
* @property string $email
* @property string $lastname
2023-07-13 08:34:54 +00:00
* @property string $firstname
* @property string $phone
* @property array<\App\Lib\Inputs\Definition\Input> $data
2023-07-13 08:34:54 +00:00
* @property string $notes
2023-08-29 10:00:27 +02:00
* @property array<array<string,string>> $visits
2023-07-13 08:34:54 +00:00
*/
class Contact extends Model
{
use CastsEnums;
use HasFactory;
use HasUuidKey;
2023-07-13 08:34:54 +00:00
/**
* fillable
* @var string[]
*/
protected $fillable = [
'app_user_id',
'email',
'lastname',
2023-07-13 08:34:54 +00:00
'firstname',
'data',
2023-07-13 08:34:54 +00:00
'phone',
'notes',
'visits',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'data' => AsArrayOfInput::class,
'visits' => 'array'
];
2023-07-13 08:34:54 +00:00
// * RELATIONSHIPS
/**
* MorphToMany Directories
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function appUser()
{
return $this->belongsTo(AppUser::class, 'app_user_id', 'id');
}
// * METHODS
/**
* Get all inputs
*
* @return array
* @throws \RuntimeException This should not happen since data was validated on contact insertion.
* ! It may happen if this model does not have an email.
* ! It may only happen if input validation rules are changed.
*/
public function allInputs(): array
{
return $this->mandatoryInputs() + $this->data;
}
/**
* Get mandatory inputs
*
* @return array<\App\Lib\Inputs\Definition\Input>
* @throws \RuntimeException If this model does have an email.
*/
public function mandatoryInputs(): array
{
// * firsname
$data[] = InputText::make('Prénom', $this->firstname, false);
// * lastname
$data[] = InputText::make('Nom', $this->lastname, false);
// * email
$data[] = InputEmail::make('E-mail', $this->email, true);
// * phone
$data[] = InputPhone::make('Téléphone', $this->phone, false);
return $data;
2023-07-13 08:34:54 +00:00
}
}