Files

62 lines
2.0 KiB
PHP

<?php
namespace App\Http\Resources\Api\Service;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ServiceListResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'uuid' => $this->uuid,
'name' => $this->name,
'slug' => $this->slug,
'short_description' => $this->short_description,
'description' => $this->description,
'thumbnail' => $this->thumbnail ? asset($this->thumbnail) : null,
'product_type' => $this->product_type,
'page_type' => $this->resolvePageType($this->product_type),
'specs' => [
'cpu' => $this->cpu,
'ram' => $this->ram,
'disk' => $this->disk,
'bandwidth' => $this->bandwidth,
'gpu' => $this->gpu
],
'stock' => $this->stock,
'price' => $this->price,
'price_sale' => $this->price_sale,
'pricing' => $this->pricings->mapWithKeys(function ($pricing) {
return [$pricing->billing_cycle => $pricing->price];
}),
'pricing_sale' => $this->pricings->mapWithKeys(function ($pricing) {
return [$pricing->billing_cycle => $pricing->price_sale];
}),
'group' => $this->category->name,
'os' => $this->osVersions->map(function ($os) {
return [
'group' => $os->osGroup->name ?? null,
];
})->unique('group')->values(),
];
}
private function resolvePageType(?string $productType): string
{
return match ($productType) {
'vps', 'cloud' => 'service_vps',
'hosting', 'email', 'ssl' => 'service_hosting',
'domain' => 'service_domain',
'blog' => 'blog',
default => 'service_vps',
};
}
}