69 lines
3.0 KiB
PHP
69 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Catalog\Resources;
|
|
|
|
use App\Domains\Bundle\Models\Bundle;
|
|
use App\Domains\Catalog\Models\GroupItem;
|
|
use App\Domains\Catalog\Models\ProductVariant;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class CatalogFeaturedGroupResource extends JsonResource
|
|
{
|
|
public function toArray(Request $request): array
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'title' => $this->group_name,
|
|
'layout' => $this->product_layout,
|
|
'group_order' => $this->group_order,
|
|
'items' => $this->whenLoaded('groupItems', function () use ($request) {
|
|
return $this->groupItems->map(function (GroupItem $groupItem) use ($request) {
|
|
$groupable = $groupItem->groupable;
|
|
|
|
if ($groupable instanceof Bundle) {
|
|
return [
|
|
'id' => $groupable->id,
|
|
'groupable_type' => 'bundle',
|
|
'groupable_id' => $groupable->id,
|
|
'nombre' => $groupable->nombre,
|
|
'descripcion' => $groupable->descripcion,
|
|
'precio' => $groupable->precio,
|
|
'cantidad_maxima' => $groupable->stock_tecnico,
|
|
'items' => $groupable->items->map(fn ($item) => [
|
|
'cantidad' => $item->cantidad,
|
|
'variant' => (new ProductVariantResource($item->variant))->toArray($request),
|
|
])->values(),
|
|
];
|
|
}
|
|
|
|
if (! $groupable instanceof ProductVariant) {
|
|
return null;
|
|
}
|
|
|
|
$variantResource = (new ProductVariantResource($groupable))->toArray($request);
|
|
$variantResource['groupable_type'] = 'variant';
|
|
$variantResource['groupable_id'] = $groupable->id;
|
|
|
|
if ($this->product_layout === 'row' || $this->product_layout === 'column_with_cart' || $this->product_layout === 'vertical_with_cart') {
|
|
// Devuelve el Product Variant Resource completo como pidio el usuario
|
|
// "Que todo devuelva el product variant resource"
|
|
return $variantResource;
|
|
}
|
|
|
|
// Para column_with_image o vertical_with_image
|
|
if ($this->product_layout === 'column_with_image' || $this->product_layout === 'vertical_with_image') {
|
|
if (isset($variantResource['images']) && count($variantResource['images']) > 0) {
|
|
$variantResource['images'] = [$variantResource['images'][0]];
|
|
}
|
|
|
|
return $variantResource;
|
|
}
|
|
|
|
return $variantResource;
|
|
})->filter()->values();
|
|
}),
|
|
];
|
|
}
|
|
}
|