|
2 | 2 |
|
3 | 3 | namespace Phpsa\LaravelApiController\Http\Api\Contracts; |
4 | 4 |
|
| 5 | +use Illuminate\Http\Request; |
5 | 6 | use Illuminate\Support\Collection; |
6 | 7 | use Illuminate\Support\Facades\DB; |
7 | 8 | use Phpsa\LaravelApiController\Events\Created; |
8 | 9 | use Phpsa\LaravelApiController\Events\Updated; |
| 10 | +use Illuminate\Http\Resources\Json\JsonResource; |
| 11 | +use Illuminate\Http\Resources\Json\ResourceCollection; |
9 | 12 | use Phpsa\LaravelApiController\Exceptions\ApiException; |
10 | 13 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
| 14 | +use Phpsa\LaravelApiController\Http\Resources\ApiResource; |
11 | 15 |
|
12 | 16 | trait HasBatchActions |
13 | 17 | { |
14 | 18 |
|
| 19 | + protected function getBatchKey(): string |
| 20 | + { |
| 21 | + return isset($this->batchKey) ? $this->batchKey : 'data'; |
| 22 | + } |
| 23 | + |
15 | 24 | public function getBatchData(): Collection |
16 | 25 | { |
17 | | - return collect($this->getRequestArray()['data'] ?? []); |
| 26 | + return collect($this->getRequestArray()[$this->getBatchKey()] ?? []); |
18 | 27 | } |
19 | 28 |
|
20 | | - /** |
21 | | - * @param \Illuminate\Http\Request|\Illuminate\Foundation\Http\FormRequest $request |
22 | | - * @param array $extraParams |
23 | | - * |
24 | | - * @return mixed |
25 | | - */ |
26 | | - public function handleBatchStoreAction($request, array $extraParams = []) |
| 29 | + public function handleBatchStoreAction(?Request $request, array $extraParams = []): ResourceCollection|JsonResource |
27 | 30 | { |
28 | 31 | $this->validateRequestType($request); |
29 | 32 | $this->addCustomParams($extraParams); |
@@ -66,12 +69,7 @@ protected function processStoreQuery(Collection $items): Collection |
66 | 69 | }); |
67 | 70 | } |
68 | 71 |
|
69 | | - /** |
70 | | - * @param \Illuminate\Http\Request|\Illuminate\Foundation\Http\FormRequest $request |
71 | | - * |
72 | | - * @return mixed |
73 | | - */ |
74 | | - public function handleBatchUpdateAction($request, array $extraParams = []) |
| 72 | + public function handleBatchUpdateAction(?Request $request, array $extraParams = []): ResourceCollection|JsonResource |
75 | 73 | { |
76 | 74 | $this->validateRequestType($request); |
77 | 75 | $this->addCustomParams($extraParams); |
@@ -127,12 +125,7 @@ protected function processUpdateQuery(Collection $items): Collection |
127 | 125 | } |
128 | 126 |
|
129 | 127 |
|
130 | | - /** |
131 | | - * @param \Illuminate\Http\Request|\Illuminate\Foundation\Http\FormRequest $request |
132 | | - * |
133 | | - * @return mixed |
134 | | - */ |
135 | | - public function handleBatchStoreOrUpdateAction($request, array $extraParams = []) |
| 128 | + public function handleBatchStoreOrUpdateAction(?Request $request = null, array $extraParams = []): ResourceCollection|JsonResource |
136 | 129 | { |
137 | 130 | $this->validateRequestType($request); |
138 | 131 | $this->addCustomParams($extraParams); |
@@ -168,27 +161,17 @@ public function handleBatchStoreOrUpdateAction($request, array $extraParams = [] |
168 | 161 | } |
169 | 162 |
|
170 | 163 |
|
171 | | - |
172 | | - /** |
173 | | - * @return mixed Response|jsonResponse |
174 | | - */ |
175 | | - protected function handleBatchStoreResponse(Collection $items) |
| 164 | + protected function handleBatchStoreResponse(Collection $items): ResourceCollection|JsonResource |
176 | 165 | { |
177 | | - return $this->respondWithResource($this->/** @scrutinizer ignore-call */getResourceCollection(), $items, 201); |
| 166 | + return $this->respondWithResource($this->getResourceCollection(), $items, 201); |
178 | 167 | } |
179 | 168 |
|
180 | | - /** |
181 | | - * @return mixed Response|jsonResponse |
182 | | - */ |
183 | | - protected function handleBatchUpdateResponse(Collection $items) |
| 169 | + protected function handleBatchUpdateResponse(Collection $items): ResourceCollection|JsonResource |
184 | 170 | { |
185 | | - return $this->respondWithResource($this->/** @scrutinizer ignore-call */getResourceCollection(), $items, 200); |
| 171 | + return $this->respondWithResource($this->getResourceCollection(), $items, 200); |
186 | 172 | } |
187 | 173 |
|
188 | | - /** |
189 | | - * @return mixed Response|jsonResponse |
190 | | - */ |
191 | | - protected function handleBatchStoreOrUpdateResponse(Collection $items) |
| 174 | + protected function handleBatchStoreOrUpdateResponse(Collection $items): ResourceCollection|JsonResource |
192 | 175 | { |
193 | 176 | return $this->respondWithResource($this->getResourceCollection(), $items, 200); |
194 | 177 | } |
|
0 commit comments