47 lines
1.2 KiB
PHP
47 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources\Intervention;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
|
|
|
class InterventionCollection extends ResourceCollection
|
|
{
|
|
/**
|
|
* Transform the resource collection into an array.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(Request $request): array
|
|
{
|
|
return [
|
|
'data' => $this->collection,
|
|
'meta' => [
|
|
'total' => $this->total(),
|
|
'per_page' => $this->perPage(),
|
|
'current_page' => $this->currentPage(),
|
|
'last_page' => $this->lastPage(),
|
|
'from' => $this->firstItem(),
|
|
'to' => $this->lastItem(),
|
|
'status_summary' => $this->calculateStatusSummary()
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Calculate summary of intervention statuses.
|
|
*
|
|
* @return array
|
|
*/
|
|
protected function calculateStatusSummary(): array
|
|
{
|
|
$statusCounts = $this->collection->groupBy('status')
|
|
->map(function ($group) {
|
|
return $group->count();
|
|
})
|
|
->toArray();
|
|
|
|
return $statusCounts;
|
|
}
|
|
}
|