DocumentationDocumentVersion.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Models;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. class DocumentationDocumentVersion extends Model
  7. {
  8. protected $fillable = [
  9. 'document_id',
  10. 'file_id',
  11. 'version',
  12. 'file_size',
  13. 'created_by',
  14. ];
  15. protected function casts(): array
  16. {
  17. return [
  18. 'version' => 'integer',
  19. 'file_size' => 'integer',
  20. ];
  21. }
  22. public function document(): BelongsTo
  23. {
  24. return $this->belongsTo(DocumentationDocument::class, 'document_id');
  25. }
  26. public function file(): BelongsTo
  27. {
  28. return $this->belongsTo(File::class);
  29. }
  30. public function creator(): BelongsTo
  31. {
  32. return $this->belongsTo(User::class, 'created_by');
  33. }
  34. public function isImage(): bool
  35. {
  36. return str_starts_with(strtolower((string) $this->file?->mime_type), 'image/');
  37. }
  38. public function iconClass(): string
  39. {
  40. $mimeType = strtolower((string) $this->file?->mime_type);
  41. return match (true) {
  42. str_starts_with($mimeType, 'image/') => 'bi bi-file-earmark-image',
  43. $mimeType === 'application/pdf' => 'bi bi-file-earmark-pdf',
  44. $mimeType === 'application/msword', str_contains($mimeType, 'wordprocessingml') => 'bi bi-file-earmark-word',
  45. $mimeType === 'application/vnd.ms-excel', str_contains($mimeType, 'spreadsheetml') => 'bi bi-file-earmark-excel',
  46. $mimeType === 'application/vnd.ms-powerpoint', str_contains($mimeType, 'presentationml') => 'bi bi-file-earmark-ppt',
  47. str_starts_with($mimeType, 'audio/') => 'bi bi-file-earmark-music',
  48. str_starts_with($mimeType, 'video/') => 'bi bi-file-earmark-play',
  49. str_starts_with($mimeType, 'text/'), str_contains($mimeType, 'json'), str_contains($mimeType, 'xml') => 'bi bi-file-earmark-code',
  50. str_contains($mimeType, 'zip'), str_contains($mimeType, 'rar'), str_contains($mimeType, '7z'), str_contains($mimeType, 'gzip') => 'bi bi-file-earmark-zip',
  51. default => 'bi bi-file-earmark',
  52. };
  53. }
  54. }