ldc-lionor/app/Models/Shop.php
noah.lancien 8278419606 Phpstan
2024-09-06 08:30:55 +00:00

69 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use App\Enums\SignsEnum;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Shop extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'signs',
'address',
'city',
'postcode',
'latitude',
'longitude',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'signs' => SignsEnum::class,
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
static::creating(function (self $model) {
// * ASSERTIONS
self::assertFieldIsUnique('name', $model->name);
});
static::updating(function (self $model) {
// * ASSERTIONS
self::assertFieldIsUnique('name', $model->name, $model->id);
});
}
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
* @param string $valueField
* @param integer|null $id
* @return void
*/
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
}
}