UserResource.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Http\Resources;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
  7. use Illuminate\Support\Facades\Storage;
  8. class UserResource extends JsonResource
  9. {
  10. /**
  11. * Transform the resource into an array.
  12. *
  13. * @return array<string, mixed>
  14. */
  15. public function toArray(Request $request): array
  16. {
  17. return [
  18. 'id' => $this->id,
  19. 'name' => $this->name,
  20. 'email' => $this->email,
  21. 'phone' => $this->phone,
  22. 'language' => $this->language,
  23. 'type' => $this->type,
  24. 'provider_id' => $this->provider?->id,
  25. 'provider_daily_price_8h' => $this->provider?->daily_price_8h,
  26. 'provider_daily_price_6h' => $this->provider?->daily_price_6h,
  27. 'provider_daily_price_4h' => $this->provider?->daily_price_4h,
  28. 'provider_daily_price_2h' => $this->provider?->daily_price_2h,
  29. 'client_id' => $this->client?->id,
  30. 'profile_photo' => $this->resolveProfilePhoto(),
  31. 'client_document' => $this->client?->document,
  32. 'registration_complete' => $this->registration_complete,
  33. 'provider' => new ProviderResource($this->whenLoaded('provider')),
  34. 'client' => new ClientResource($this->whenLoaded('client')),
  35. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i'),
  36. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i'),
  37. ];
  38. }
  39. private function resolveProfilePhoto(): ?string
  40. {
  41. $path = $this->provider?->profileMedia?->path ?? $this->client?->profileMedia?->path;
  42. return $path ? Storage::temporaryUrl($path, now()->addMinutes(60)) : null;
  43. }
  44. public static function collection($resource): AnonymousResourceCollection
  45. {
  46. return parent::collection($resource);
  47. }
  48. }