*/ public function toArray(Request $request): array { /** @var \App\Domains\Shared\Contracts\Buyable|null $buyable */ $buyable = $this->buyable; $productName = $buyable?->getName(); $precio = $buyable?->getPrice(); $imageUrl = null; if ($this->buyable_type === \App\Domains\Catalog\Models\ProductVariant::class && $buyable && $buyable->relationLoaded('attachments')) { $firstAttachment = $buyable->attachments->first(); if ($firstAttachment) { $imageUrl = $firstAttachment->getTemporaryUrl(1440); } } return [ 'id' => $this->id, 'cantidad' => $this->cantidad, 'precio_unitario' => $this->formatMoney($precio), 'buyable_type' => $this->mapBuyableTypeToAlias($this->buyable_type), 'buyable_id' => $this->buyable_id, 'product' => $buyable === null ? null : [ 'nombre' => $productName, 'imagen' => $imageUrl, ], ]; } protected function mapBuyableTypeToAlias(?string $type): string { return match ($type) { \App\Domains\Catalog\Models\ProductVariant::class => 'variant', \App\Domains\Bundle\Models\Bundle::class => 'bundle', default => 'unknown', }; } protected function formatMoney(float|int|string|null $amount): string { return number_format((float) ($amount ?? 0), 2, '.', ''); } }