Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| StorageResource | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
| toArray | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Storage\Resources; |
| 4 | |
| 5 | use App\Models\Transferable\Transferable; |
| 6 | use Domain\Product\Resources\ProductResource; |
| 7 | use Domain\Storage\Enums\StorageTypeEnum; |
| 8 | use Domain\User\Resources\UserResource; |
| 9 | use Illuminate\Database\Eloquent\Collection; |
| 10 | use Illuminate\Http\Request; |
| 11 | use Illuminate\Http\Resources\Json\JsonResource; |
| 12 | |
| 13 | /** |
| 14 | * @property Collection $items |
| 15 | * @property int $id |
| 16 | * @property Transferable|null $transferable |
| 17 | * @property string $created_at |
| 18 | * @property StorageTypeEnum $type |
| 19 | */ |
| 20 | class StorageResource extends JsonResource |
| 21 | { |
| 22 | |
| 23 | public function toArray(Request $request): array |
| 24 | { |
| 25 | return [ |
| 26 | 'id' => $this->id, |
| 27 | 'transferable_id' => $this->whenLoaded('transferable', $this->transferable?->getKey()), |
| 28 | 'type' => $this->type->value, |
| 29 | 'user' => UserResource::make($this->whenLoaded('user')), |
| 30 | 'products' => ProductResource::collection($this->whenLoaded('items', $this->items->pluck('product')->filter())), |
| 31 | 'storages' => StorageResource::collection($this->whenLoaded('items', $this->items->pluck('storage')->filter())), |
| 32 | 'created_at' => $this->created_at, |
| 33 | ]; |
| 34 | } |
| 35 | |
| 36 | } |