2023-07-13 08:34:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2023-08-28 19:08:23 +02:00
|
|
|
use App\Casts\AsArrayOfInput;
|
2023-08-31 12:48:31 +02:00
|
|
|
use App\Lib\Helpers\ToolboxHelper;
|
2023-08-28 19:08:23 +02:00
|
|
|
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;
|
2023-08-31 16:38:02 +02:00
|
|
|
use Illuminate\Support\Str;
|
2023-07-13 08:34:54 +00:00
|
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @property integer $app_user_id
|
2023-08-28 19:08:23 +02:00
|
|
|
* @property string $id
|
2023-07-13 08:34:54 +00:00
|
|
|
* @property string $email
|
2023-09-01 09:30:01 +02:00
|
|
|
* @property string|null $lastname
|
|
|
|
|
* @property string|null $firstname
|
2023-08-29 10:07:36 +02:00
|
|
|
* @property string|null $phone
|
2023-08-28 19:08:23 +02:00
|
|
|
* @property array<\App\Lib\Inputs\Definition\Input> $data
|
2023-08-29 10:07:36 +02:00
|
|
|
* @property string|null $notes
|
2023-09-01 09:30:01 +02:00
|
|
|
* @property array<array<string,string>> $visits
|
2023-09-01 16:50:18 +02:00
|
|
|
* @property \Carbon\Carbon $timestamp
|
2023-07-13 08:34:54 +00:00
|
|
|
*/
|
|
|
|
|
class Contact extends Model
|
|
|
|
|
{
|
|
|
|
|
use CastsEnums;
|
2023-08-28 19:08:23 +02:00
|
|
|
use HasFactory;
|
|
|
|
|
use HasUuidKey;
|
2023-07-13 08:34:54 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* fillable
|
|
|
|
|
* @var string[]
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'app_user_id',
|
|
|
|
|
'email',
|
2023-08-28 19:08:23 +02:00
|
|
|
'lastname',
|
2023-07-13 08:34:54 +00:00
|
|
|
'firstname',
|
2023-08-28 19:08:23 +02:00
|
|
|
'data',
|
2023-07-13 08:34:54 +00:00
|
|
|
'phone',
|
|
|
|
|
'notes',
|
|
|
|
|
'visits',
|
2023-09-01 16:50:18 +02:00
|
|
|
'timestamp',
|
2023-07-13 08:34:54 +00:00
|
|
|
];
|
|
|
|
|
|
2023-08-28 19:08:23 +02:00
|
|
|
/**
|
|
|
|
|
* The attributes that should be cast.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'data' => AsArrayOfInput::class,
|
2023-09-01 16:50:18 +02:00
|
|
|
'timestamp' => 'immutable_datetime',
|
2023-08-28 19:08:23 +02:00
|
|
|
'visits' => 'array'
|
|
|
|
|
];
|
|
|
|
|
|
2023-08-31 12:48:31 +02:00
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
static::creating(function (self $model) {
|
2023-08-31 16:38:02 +02:00
|
|
|
$model->id = Str::uuid();
|
2023-08-31 12:48:31 +02:00
|
|
|
ToolboxHelper::assertContactDataIsMatchingFormulary($model);
|
|
|
|
|
});
|
|
|
|
|
static::deleting(function (self $model) {
|
|
|
|
|
ToolboxHelper::assertContactDataIsMatchingFormulary($model);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 08:34:54 +00:00
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MorphToMany Directories
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
|
|
|
|
public function appUser()
|
|
|
|
|
{
|
2023-08-28 19:08:23 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|