47 lines
1.5 KiB
PHP
47 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\FiestaFutbolInfantil\Resources;
|
|
|
|
use App\Domains\Catalog\Models\CatalogItem;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
/** @mixin CatalogItem */
|
|
class AccommodationResource extends JsonResource
|
|
{
|
|
/** @return array<string, mixed> */
|
|
public function toArray(Request $request): array
|
|
{
|
|
if ($this->resource === null) {
|
|
return [
|
|
'id' => null,
|
|
'name' => 'Alojamiento',
|
|
'variants' => [],
|
|
];
|
|
}
|
|
|
|
$typeAttribute = $this->itemAttributes
|
|
->first(fn ($itemAttribute) => $itemAttribute->attribute?->codigo === 'tipo_alojamiento');
|
|
$options = $typeAttribute?->attribute?->options?->keyBy('value') ?? collect();
|
|
|
|
return [
|
|
'id' => $this->id,
|
|
'name' => $this->nombre,
|
|
'variants' => $this->variants->map(function ($variant) use ($typeAttribute, $options): array {
|
|
$value = $variant->definitions
|
|
->firstWhere('item_attribute_id', $typeAttribute?->id)
|
|
?->value;
|
|
|
|
return [
|
|
'id' => $variant->id,
|
|
'title' => $options->get($value)?->label ?? $value,
|
|
'value' => $value,
|
|
'description' => $variant->descripcion,
|
|
'stock' => $variant->inventory->real_stock,
|
|
'price' => number_format($variant->getPrice(), 2, '.', ''),
|
|
];
|
|
})->values(),
|
|
];
|
|
}
|
|
}
|