Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
20 / 22
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
AddressCompleteHierarchyResource
90.91% covered (success)
90.91%
20 / 22
0.00% covered (danger)
0.00%
0 / 1
3.01
0.00% covered (danger)
0.00%
0 / 1
 toArray
90.91% covered (success)
90.91%
20 / 22
0.00% covered (danger)
0.00%
0 / 1
3.01
1<?php
2
3namespace Domain\Address\Resources;
4
5use App\Models\Transferable\Transferable;
6use Domain\Address\Enums\AddressCategoryEnum;
7use Domain\Address\Services\AddressHierarchyService;
8use Domain\Item\Resource\ItemResource;
9use Domain\Shared\Enums\SectorEnum;
10use Illuminate\Http\Request;
11use Illuminate\Http\Resources\Json\JsonResource;
12use 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 */
26class AddressCompleteHierarchyResource 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' => AddressCompleteHierarchyResource::collection(
42                $this->relationLoaded('allChildren')
43                    ? $this->allChildren
44                    : ($this->relationLoaded('allChildrenWithTrashed')
45                    ? $this->allChildrenWithTrashed
46                    : $this->whenLoaded('children'))
47            ),
48            'children_count' => $this->whenCounted('children'),
49            'items' => ItemResource::collection($this->whenLoaded('items')),
50            'items_sum_amount' => $this->whenCounted('items'),
51        ];
52    }
53}