2024-01-15 15:39:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2025-05-27 09:11:43 +02:00
|
|
|
use App\Traits\Models\ActivityLog;
|
2024-01-15 15:39:09 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Illuminate\Validation\ValidationException;
|
2025-05-20 12:51:38 +02:00
|
|
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
2025-05-27 09:11:43 +02:00
|
|
|
use Uwil\LaravelCommon\Helpers\FileHelper;
|
2024-01-15 15:39:09 +01:00
|
|
|
|
2024-01-16 10:09:53 +01:00
|
|
|
/**
|
|
|
|
|
* @property string $font_name The slugified font name.
|
|
|
|
|
* @property string|null $font The font path.
|
2025-05-27 09:11:43 +02:00
|
|
|
* @property string $tenant_id Id of the related tenant.
|
2024-01-16 10:09:53 +01:00
|
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
|
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
|
|
|
|
*/
|
2024-01-15 15:39:09 +01:00
|
|
|
class Font extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
2025-05-20 12:51:38 +02:00
|
|
|
use BelongsToTenant;
|
2025-05-27 09:11:43 +02:00
|
|
|
use ActivityLog;
|
2024-01-15 15:39:09 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Default available fonts
|
|
|
|
|
*/
|
|
|
|
|
public const DEFAULT_FONTS = ['serif', 'sans-serif'];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
2025-05-15 11:47:41 +02:00
|
|
|
* @var list<string>
|
2024-01-15 15:39:09 +01:00
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'font_name',
|
|
|
|
|
'font',
|
2025-05-27 09:11:43 +02:00
|
|
|
'tenant_id',
|
2024-01-15 15:39:09 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected static function booted(): void
|
|
|
|
|
{
|
|
|
|
|
static::creating(function (self $model) {
|
|
|
|
|
// * ASSERTIONS
|
2024-01-30 11:51:13 +01:00
|
|
|
self::assertFontIsWoff2($model->font);
|
2024-01-15 15:39:09 +01:00
|
|
|
|
|
|
|
|
// * TRANSFORMATIONS
|
|
|
|
|
if ($model->font) {
|
2025-05-27 09:11:43 +02:00
|
|
|
$model->font = FileHelper::storeFile($model, $model->font, true, prefix: $model->tenant_id);
|
2024-01-15 15:39:09 +01:00
|
|
|
$font_basename = Str::of(\pathinfo($model->font, \PATHINFO_BASENAME))->explode('.')->get(0);
|
|
|
|
|
$model->font_name = Str::slug($font_basename);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
static::updating(function (self $model) {
|
|
|
|
|
// * ASSERTIONS
|
2024-01-30 11:51:13 +01:00
|
|
|
self::assertFontIsWoff2($model->font);
|
2024-01-15 15:39:09 +01:00
|
|
|
|
|
|
|
|
// * TRANSFORMATIONS
|
|
|
|
|
if ($model->font) {
|
2025-05-27 09:11:43 +02:00
|
|
|
$model->font = FileHelper::storeFile($model, $model->font, true, prefix: $model->tenant_id);
|
2024-01-15 15:39:09 +01:00
|
|
|
$font_basename = Str::of(\pathinfo($model->font, \PATHINFO_BASENAME))->explode('.')->get(0);
|
|
|
|
|
$model->font_name = Str::slug($font_basename);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
static::updated(function (self $model) {
|
2025-05-27 09:11:43 +02:00
|
|
|
FileHelper::removeOldFile($model, 'font');
|
2024-01-15 15:39:09 +01:00
|
|
|
});
|
|
|
|
|
static::deleted(function (self $model) {
|
2025-05-27 09:11:43 +02:00
|
|
|
FileHelper::removeFile($model, 'font');
|
2024-01-15 15:39:09 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assert font is Woff2
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\UploadedFile|\SplFileInfo|string|null $font
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws \Illuminate\Validation\ValidationException If font is not Woff 2 file type.
|
|
|
|
|
*/
|
|
|
|
|
private static function assertFontIsWoff2(
|
|
|
|
|
\Illuminate\Http\UploadedFile|\SplFileInfo|string|null $font
|
|
|
|
|
): void {
|
|
|
|
|
$exception = ValidationException::withMessages(['font' => trans(
|
|
|
|
|
'La police doit être au format Woff2, utilisez transfonter.org'
|
|
|
|
|
)]);
|
|
|
|
|
if (!$font) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (is_string($font) and Str::of($font)->lower()->explode('.')->last() !== 'woff2') {
|
|
|
|
|
throw $exception;
|
|
|
|
|
}
|
|
|
|
|
if ($font instanceof \Illuminate\Http\UploadedFile and $font->getMimeType() !== 'font/woff2') {
|
|
|
|
|
throw $exception;
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
!($font instanceof \Illuminate\Http\UploadedFile) and
|
|
|
|
|
$font instanceof \SplFileInfo and
|
|
|
|
|
Str::lower($font->getExtension()) !== 'woff2'
|
|
|
|
|
) {
|
|
|
|
|
throw $exception;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|