133 lines
4.1 KiB
PHP
133 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
/**
|
|
* SectionApproach Model
|
|
*
|
|
* @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
|
|
* @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
|
|
*/
|
|
class SectionApproach extends Model
|
|
{
|
|
/**
|
|
* Maximum number of sections allowed
|
|
*
|
|
* @var int
|
|
*/
|
|
public const MAX_SECTIONS = 6;
|
|
|
|
/**
|
|
* Minimum number of sections required
|
|
*
|
|
* @var int
|
|
*/
|
|
public const MIN_SECTIONS = 2;
|
|
|
|
/**
|
|
* The attributes that are mass assignable
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'title',
|
|
'content',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'category_id',
|
|
'page_id'
|
|
];
|
|
|
|
/**
|
|
* Validates that the maximum number of sections has not been exceeded.
|
|
*
|
|
* @return void
|
|
* @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.
|
|
*
|
|
* @return void
|
|
* @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.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $approach) {
|
|
$approach->validateMaxSectionCount();
|
|
$approach->picture = FileStorageHelper::storeFile($approach, $approach->picture, true);
|
|
});
|
|
|
|
static::updating(function (self $approach) {
|
|
$approach->picture = FileStorageHelper::storeFile($approach, $approach->picture, true);
|
|
});
|
|
|
|
static::updated(function (self $approach) {
|
|
FileStorageHelper::removeOldFile($approach, 'picture');
|
|
});
|
|
|
|
static::deleting(function (self $approach) {
|
|
static::validateMinSectionCount($approach);
|
|
});
|
|
|
|
static::deleted(function (self $approach) {
|
|
FileStorageHelper::removeFile($approach, 'picture');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Relationship: belongs to a category approach
|
|
*
|
|
* @return BelongsTo Returns the associated category
|
|
*/
|
|
public function category(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CategoryApproach::class, 'category_id');
|
|
}
|
|
|
|
/**
|
|
* Relationship: belongs to a static page
|
|
*
|
|
* @return BelongsTo Returns the associated static page
|
|
*/
|
|
public function page(): BelongsTo
|
|
{
|
|
return $this->belongsTo(StaticPage::class);
|
|
}
|
|
}
|