ldc-legaulois/app/Models/CategoryApproach.php
2026-09-25 16:25:31 +02:00

91 lines
2.4 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* CategoryApproach Model
*
* @property string $name Name of the category approach.
* @property-read \Carbon\Carbon $created_at Creation timestamp.
* @property-read \Carbon\Carbon $updated_at Last update timestamp.
*/
class CategoryApproach extends Model
{
/**
* Maximum number of category approaches allowed in the system.
*
* @var int
*/
public const MAX_CATEGORIES = 5;
/**
* Minimum number of category approaches required in the system.
*
* @var int
*/
public const MIN_CATEGORIES = 1;
/**
* The attributes that are mass assignable.
*
* Defines which attributes can be filled via mass assignment operations.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
];
/**
* Validate that the maximum number of category approaches has not been exceeded.
*
* @throws \Exception If the maximum number of categories has been exceeded.
*/
protected function validateMaxCategoryCount(): void
{
if (static::count() > self::MAX_CATEGORIES) {
throw new \Exception('Cannot create more than ' . self::MAX_CATEGORIES . ' category approaches.');
}
}
/**
* Validate that the minimum number of category approaches is maintained.
*
* @throws \Exception If deletion would violate the minimum category requirement.
*/
protected static function validateMinCategoryCount(): void
{
if (static::count() <= self::MIN_CATEGORIES) {
throw new \Exception('At least ' . self::MIN_CATEGORIES . ' category approach is required.');
}
}
/**
* Perform any actions required after the model boots.
*/
protected static function booting(): void
{
static::creating(function (self $model) {
$model->validateMaxCategoryCount();
});
static::updating(function (self $model) {
$model->validateMaxCategoryCount();
});
static::deleting(function (self $model) {
static::validateMinCategoryCount();
});
}
/**
* Get all section approaches associated with this category approach.
*/
public function sectionsApproaches(): HasMany
{
return $this->hasMany(SectionApproach::class, 'category_id');
}
}