*/ public function toArray(Request $request): array { $catalogItem = $this->catalogItem; if ($this->featuredGroup->product_layout === ProductLayout::ColumnWithImage) { return $this->columnWithImageData($catalogItem); } return [ 'id' => $catalogItem->id, 'type' => $catalogItem->type->value, 'nombre' => $catalogItem->nombre, 'descripcion' => $catalogItem->descripcion, 'precio' => $catalogItem->precio, 'stock_tecnico' => $catalogItem->availableStock(), 'variants' => $catalogItem->variants ->map(fn (Variant $variant): array => [ 'id' => $variant->id, 'event_date_id' => $variant->event_date_id, 'event_date' => $variant->eventDate?->date?->format('Y-m-d'), 'stock_tecnico' => $catalogItem->inventory_policy === InventoryPolicy::Unlimited ? null : $variant->inventory->availableStock(), 'values' => $variant->definitions ->mapWithKeys(fn ($definition) => [ $definition->itemAttribute?->attribute?->codigo => $definition->value, ]) ->filter(fn ($value, $key): bool => $key !== null), ]) ->values(), ]; } /** @return array */ private function columnWithImageData(CatalogItem $catalogItem): array { $attachment = $catalogItem->attachments->first() ?? $catalogItem->variants ->flatMap(fn (Variant $variant) => $variant->attachments) ->first(); return [ 'id' => $catalogItem->id, 'type' => $catalogItem->type->value, 'nombre' => $catalogItem->nombre, 'precio' => $catalogItem->precio, 'image' => $attachment?->getTemporaryUrl(1440), ]; } }