Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| AddressResource | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
| toArray | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Address\Resources; |
| 4 | |
| 5 | use App\Models\Transferable\Transferable; |
| 6 | use Domain\Address\Enums\AddressCategoryEnum; |
| 7 | use Domain\Address\Services\AddressHierarchyService; |
| 8 | use Domain\Item\Resource\ItemResource; |
| 9 | use Domain\Shared\Enums\SectorEnum; |
| 10 | use Illuminate\Http\Request; |
| 11 | use Illuminate\Http\Resources\Json\JsonResource; |
| 12 | use Illuminate\Support\Collection; |
| 13 | |
| 14 | /** |
| 15 | * @property int $id |
| 16 | * @property Transferable|null $transferable |
| 17 | * @property string $name |
| 18 | * @property bool $can_store |
| 19 | * @property bool $can_remove |
| 20 | * @property Collection $items |
| 21 | * @property int $status |
| 22 | * @property int $order |
| 23 | * @property SectorEnum $sector |
| 24 | * @property AddressCategoryEnum $category |
| 25 | */ |
| 26 | class AddressResource extends JsonResource |
| 27 | { |
| 28 | public function toArray(Request $request): array |
| 29 | { |
| 30 | return [ |
| 31 | 'id' => $this->id, |
| 32 | 'transferable_id' => $this->whenLoaded('transferable', fn() => $this->transferable->getKey()), |
| 33 | 'name' => $this->name, |
| 34 | 'can_store' => $this->can_store, |
| 35 | 'can_remove' => $this->can_remove, |
| 36 | 'category' => $this->category->value, |
| 37 | 'order' => $this->order, |
| 38 | 'status' => $this->status, |
| 39 | 'sector' => $this->sector->value, |
| 40 | 'is_trashed' => $this->trashed(), |
| 41 | 'children' => AddressResource::collection($this->whenLoaded('children')), |
| 42 | 'children_count' => $this->whenCounted('children'), |
| 43 | 'hierarchy' => $this->whenLoaded('parent', (new AddressHierarchyService())->get($this->resource)), |
| 44 | 'items' => ItemResource::collection($this->whenLoaded('items')), |
| 45 | 'items_sum_amount' => $this->whenCounted('items'), |
| 46 | 'parent' => AddressResource::make($this->whenLoaded('parent')), |
| 47 | ]; |
| 48 | } |
| 49 | } |