|null $chapters */ #[Fillable(['property_id', 'title', 'chapters'])] class Video extends Model implements HasMedia { /** @use HasFactory */ use BelongsToTenant, HasFactory, InteractsWithMedia; public const COLLECTION = 'video'; public const POSTER = 'poster'; /** Largest accepted video file in KB (Strato allows uploads up to 128 MB). */ public const MAX_KB = 100 * 1024; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'chapters' => 'array', ]; } public function registerMediaCollections(): void { $this->addMediaCollection(self::COLLECTION) ->useDisk('public') ->singleFile() ->acceptsMimeTypes(['video/mp4', 'application/mp4', 'video/webm']); $this->addMediaCollection(self::POSTER) ->useDisk('public') ->singleFile() ->acceptsMimeTypes(['image/jpeg', 'image/png', 'image/webp']); } public function registerMediaConversions(?Media $media = null): void { $this->addMediaConversion('poster') ->fit(Fit::Max, 1920, 1920) ->format('webp') ->quality(80) ->performOnCollections(self::POSTER); } /** * @return BelongsTo */ public function property(): BelongsTo { return $this->belongsTo(Property::class); } public function url(): ?string { return $this->getFirstMediaUrl(self::COLLECTION) ?: null; } public function mimeType(): string { return $this->getFirstMedia(self::COLLECTION)?->mime_type ?? 'video/mp4'; } public function posterUrl(): ?string { return $this->getFirstMediaUrl(self::POSTER, 'poster') ?: null; } /** * Chapters in time order with the start in seconds; entries without a valid time or label are left out. * * @return list */ public function chapterList(): array { $chapters = []; foreach ($this->chapters ?? [] as $chapter) { $seconds = self::seconds($chapter['at'] ?? null); if ($seconds === null || blank($chapter['label'] ?? null)) { continue; } $chapters[] = [ 'seconds' => $seconds, 'time' => sprintf('%d:%02d', intdiv($seconds, 60), $seconds % 60), 'label' => (string) $chapter['label'], 'room' => filled($chapter['room'] ?? null) ? (string) $chapter['room'] : null, ]; } usort($chapters, fn (array $a, array $b): int => $a['seconds'] <=> $b['seconds']); return $chapters; } /** * The chapter showing a floor plan room (matched by name), if any. * * @return array{seconds: int, time: string, label: string, room: string|null}|null */ public function chapterForRoom(string $room): ?array { foreach ($this->chapterList() as $chapter) { if ($chapter['room'] !== null && mb_strtolower($chapter['room']) === mb_strtolower($room)) { return $chapter; } } return null; } /** * "1:05" or "65" → 65; anything else → null. */ public static function seconds(mixed $value): ?int { $value = trim((string) $value); if (preg_match('/^(\d{1,3}):([0-5]\d)$/', $value, $match)) { return (int) $match[1] * 60 + (int) $match[2]; } return preg_match('/^\d{1,4}$/', $value) ? (int) $value : null; } }