ldc-wenn-dann-mit-respekt/app/Models/Range.php
2025-07-17 11:58:12 +02:00

192 lines
4.4 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Ranges\PictoEnum;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\HasTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Spatie\SchemaOrg\Schema;
use Uwil\SchemaOrg\Laravel\Interfaces\SchemaOrgInterface;
use Uwil\SchemaOrg\Laravel\Traits\SchemaOrgTrait;
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
use Uwil\Seo\Laravel\Objects\SeoItem;
use Uwil\Seo\Laravel\Traits\SeoTrait;
/**
* @property string $seo_title
* @property string $seo_description
* @property string $label
* @property string $slug
* @property string $picto
* @property integer $order
* @property boolean $status
* @property string $created_at
* @property string $updated_at
*/
class Range extends Model implements SeoInterface, SchemaOrgInterface
{
use HasFactory;
use HasTranslations;
use SeoTrait;
use SchemaOrgTrait;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'seo_title',
'seo_description',
'label',
'slug',
'picto',
'order',
'status',
'created_at',
'updated_at'
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'picto' => PictoEnum::class,
];
/**
* The translatable columns.
*
* @var array
*/
public $translatable = [
'seo_title',
'seo_description',
'label',
'slug',
];
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::manageFields($model);
self::assertFieldIsUnique('label', $model->label);
self::setOrder($model);
});
static::updating(function (self $model) {
self::manageFields($model);
});
}
// * METHODS
/**
* Set the slug.
*
* @param self $model
*
* @return void
*/
private static function setSlug(self $model)
{
$model->slug = Str::slug($model->label);
}
/**
* Common methods function
*
* @param self $model
* @return void
*/
private static function manageFields(self $model)
{
self::setSlug($model);
}
/**
* 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);
}
/**
* 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;
}
/**
* Create a SchemaOrg.
*
* @return \Spatie\SchemaOrg\DefinedTermSet
*/
public function toSchemaOrg(): \Spatie\SchemaOrg\DefinedTermSet
{
return Schema::definedTermSet()
->hasDefinedTerm(
Schema::definedTerm()
->name($this->seo_title)
->description($this->seo_description)
);
}
// * RELATIONSHIPS
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function recipes()
{
return $this->hasMany('App\Models\Recipe');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function products()
{
return $this->hasMany('App\Models\Product');
}
/**
* Get the SEO object.
*
* @return \Uwil\Seo\Laravel\Objects\SeoItem
*/
public function toSeoItem(): \Uwil\Seo\Laravel\Objects\SeoItem
{
return new SeoItem(
\trans($this->getTable()),
$this->seo_title,
$this->seo_description,
$this->status ?? false,
\route(\config('seo.bo_route_prefix') . $this->getTable() . '.edit', $this),
'#'
);
}
}