65 lines
2.3 KiB
PHP
65 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Catalog\Resources;
|
|
|
|
use App\Domains\Catalog\Enums\InventoryPolicy;
|
|
use App\Domains\Catalog\Enums\ProductLayout;
|
|
use App\Domains\Catalog\Models\CatalogItem;
|
|
use App\Domains\Catalog\Models\FeaturedItem;
|
|
use App\Domains\Catalog\Models\Variant;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
/** @mixin FeaturedItem */
|
|
class CatalogFeaturedItemResource extends JsonResource
|
|
{
|
|
/** @return array<string, mixed> */
|
|
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,
|
|
'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<string, mixed> */
|
|
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),
|
|
];
|
|
}
|
|
}
|