, amenity_ids?: list, is_visible?: bool}>|null $rooms */ #[Fillable(['property_id', 'photo_id', 'rooms'])] class FloorPlan extends Model { /** @use HasFactory */ use BelongsToTenant, HasFactory; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'rooms' => 'array', ]; } /** * @return BelongsTo */ public function property(): BelongsTo { return $this->belongsTo(Property::class); } /** * @return BelongsTo */ public function photo(): BelongsTo { return $this->belongsTo(Photo::class); } /** * Rooms shown on the website: visible and with an area of at least three corners. * * @return Collection> */ public function visibleRooms(): Collection { return collect($this->rooms ?? []) ->filter(fn (array $room): bool => ($room['is_visible'] ?? true) && filled($room['name'] ?? null) && count(self::points($room['points'] ?? null)) >= 3) ->values(); } /** * Parses "x,y x,y …" (percent) into points, ignoring anything outside 0–100. * * @return list */ public static function points(?string $points): array { $parsed = []; foreach (preg_split('/\s+/', trim((string) $points)) ?: [] as $pair) { if (preg_match('/^(\d{1,3}(?:\.\d+)?),(\d{1,3}(?:\.\d+)?)$/', $pair, $match) && (float) $match[1] <= 100 && (float) $match[2] <= 100) { $parsed[] = [(float) $match[1], (float) $match[2]]; } } return $parsed; } }