Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| ProductResource | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
| toArray | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Resources\V1; |
| 4 | |
| 5 | use Illuminate\Http\Request; |
| 6 | use Illuminate\Http\Resources\Json\JsonResource; |
| 7 | |
| 8 | /** Card-sized shape for listing endpoints — see ProductDetailResource for the full product. */ |
| 9 | class ProductResource extends JsonResource |
| 10 | { |
| 11 | public function toArray(Request $request): array |
| 12 | { |
| 13 | return [ |
| 14 | 'id' => $this->id, |
| 15 | 'name' => $this->name, |
| 16 | 'slug' => $this->slug, |
| 17 | 'thumbnail' => $this->thumbnail ? (str_starts_with($this->thumbnail, 'http') ? $this->thumbnail : asset($this->thumbnail)) : null, |
| 18 | 'price' => (float) $this->price, |
| 19 | 'selling_price' => (float) ($this->selling_price ?: $this->price), |
| 20 | 'is_featured' => (bool) $this->is_featured, |
| 21 | 'in_stock' => $this->stock_status !== 'out_of_stock', |
| 22 | 'category' => $this->whenLoaded('category', fn () => $this->category ? [ |
| 23 | 'id' => $this->category->id, |
| 24 | 'name' => $this->category->name, |
| 25 | 'slug' => $this->category->slug, |
| 26 | ] : null), |
| 27 | ]; |
| 28 | } |
| 29 | } |