TaskResource.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Http\Resources\API;
  3. use App\Models\Enums\ObjectApprovalStatus;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. class TaskResource extends JsonResource
  7. {
  8. /**
  9. * Transform the resource into an array.
  10. *
  11. * @return array<string, mixed>
  12. */
  13. public function toArray(Request $request): array
  14. {
  15. return [
  16. "id" => $this->id,
  17. "name" => $this->name,
  18. "parent_id" => $this->parent_id,
  19. "begin" => $this->begin,
  20. "end" => $this->end,
  21. "status" => $this->status,
  22. 'approval_status' => match ($this->approval_status) {
  23. ObjectApprovalStatus::WAIT->value => 'Not Submitted',
  24. ObjectApprovalStatus::DOING->value => 'Pending for Approval',
  25. ObjectApprovalStatus::APPROVED->value => 'A-Approved & B-Approved w/comment',
  26. ObjectApprovalStatus::REJECTED->value => 'D-Rejected',
  27. ObjectApprovalStatus::CANCELED->value => 'Cancelled',
  28. ObjectApprovalStatus::RESUBMISSION->value => "C-Resubmission Reg'd",
  29. default => 'Not Submitted'
  30. },
  31. "assign_to" => new UserProfileResource($this->assignTo),
  32. "created_by" => new UserProfileResource($this->createdBy),
  33. //"children" => TaskResource::collection($this->children),
  34. 'display_id'=>$this->display_id,
  35. 'children' => $this->when($this->children->isNotEmpty(),function (){
  36. return $this->children->map(function ($child){
  37. return new TaskResource($child);
  38. })->all();
  39. }),
  40. 'created_at'=>(string)$this->created_at,
  41. 'belong_project' =>new ProjectSimpleResource($this->project),
  42. ];
  43. }
  44. }