ldc-legaulois/app/Models/SectionApproach.php

129 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2026-02-20 17:32:36 +01:00
<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
2026-02-20 17:32:36 +01:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2026-03-31 11:49:27 +02:00
/**
* SectionApproach Model
*
2026-09-25 16:25:31 +02:00
* @property int $id Unique identifier of the section
* @property string $title Title of the section
* @property string $content Textual content of the section
* @property string|null $picture Storage path of the image
* @property string|null $picture_alt Alternative text of the image (accessibility)
* @property string|null $picture_title Title of the image
* @property int $category_id Identifier of the associated category
* @property int $page_id Identifier of the associated static page
2026-03-31 11:49:27 +02:00
* @property-read \Carbon\Carbon $created_at Creation timestamp.
* @property-read \Carbon\Carbon $updated_at Last update timestamp.
* @property-read CategoryApproach $category Associated category
* @property-read StaticPage $page Associated static page
*/
2026-02-24 11:43:07 +01:00
class SectionApproach extends Model
2026-02-20 17:32:36 +01:00
{
2026-03-31 11:49:27 +02:00
/**
* Maximum number of sections allowed
*
* @var int
*/
2026-02-24 11:43:07 +01:00
public const MAX_SECTIONS = 6;
2026-03-03 17:22:33 +01:00
2026-03-31 11:49:27 +02:00
/**
* Minimum number of sections required
*
* @var int
*/
2026-02-24 11:43:07 +01:00
public const MIN_SECTIONS = 2;
2026-03-31 11:49:27 +02:00
/**
* The attributes that are mass assignable
*
* @var array<int, string>
*/
2026-02-20 17:32:36 +01:00
protected $fillable = [
'title',
'content',
'picture',
'picture_alt',
'picture_title',
'category_id',
2026-09-25 16:25:31 +02:00
'page_id',
2026-02-20 17:32:36 +01:00
];
2026-03-31 11:49:27 +02:00
/**
* Validates that the maximum number of sections has not been exceeded.
*
* @throws \Exception If the maximum number of sections has been exceeded.
*/
protected function validateMaxSectionCount(): void
{
$sectionCount = static::where('category_id', $this->category_id)->where('page_id', $this->page_id)->count();
if ($sectionCount >= self::MAX_SECTIONS) {
throw new \Exception('Cannot create more than ' . self::MAX_SECTIONS . ' sections per category.');
}
}
/**
* Validates that the minimum number of sections is maintained.
*
* @throws \Exception If deletion would violate the minimum section requirement.
*/
protected static function validateMinSectionCount(self $approach): void
{
$sectionCount = static::where('category_id', $approach->category_id)->where('page_id', $approach->page_id)->count();
if ($sectionCount <= self::MIN_SECTIONS) {
throw new \Exception('At least ' . self::MIN_SECTIONS . ' sections are required per category.');
}
}
2026-02-20 17:32:36 +01:00
/**
* Perform any actions required after the model boots.
*/
protected static function booted(): void
{
static::creating(function (self $approach) {
2026-03-31 11:49:27 +02:00
$approach->validateMaxSectionCount();
$approach->picture = FileStorageHelper::storeFile($approach, $approach->picture, true);
});
2026-03-31 11:49:27 +02:00
static::updating(function (self $approach) {
$approach->picture = FileStorageHelper::storeFile($approach, $approach->picture, true);
});
2026-03-31 11:49:27 +02:00
static::updated(function (self $approach) {
FileStorageHelper::removeOldFile($approach, 'picture');
});
2026-03-31 11:49:27 +02:00
static::deleting(function (self $approach) {
static::validateMinSectionCount($approach);
});
static::deleted(function (self $approach) {
FileStorageHelper::removeFile($approach, 'picture');
});
}
2026-03-31 11:49:27 +02:00
/**
* Relationship: belongs to a category approach
*
* @return BelongsTo Returns the associated category
*/
2026-02-20 17:32:36 +01:00
public function category(): BelongsTo
{
2026-03-31 11:49:27 +02:00
return $this->belongsTo(CategoryApproach::class, 'category_id');
2026-02-20 17:32:36 +01:00
}
2026-03-31 11:49:27 +02:00
/**
* Relationship: belongs to a static page
*
* @return BelongsTo Returns the associated static page
*/
2026-02-20 17:32:36 +01:00
public function page(): BelongsTo
{
return $this->belongsTo(StaticPage::class);
}
}