UserResource.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. 'gender' => $this->provider?->gender?->value,
  23. 'language' => $this->language,
  24. 'type' => $this->type,
  25. 'provider_id' => $this->provider?->id,
  26. 'provider_daily_price_8h' => $this->provider?->daily_price_8h,
  27. 'provider_daily_price_6h' => $this->provider?->daily_price_6h,
  28. 'provider_daily_price_4h' => $this->provider?->daily_price_4h,
  29. 'provider_daily_price_2h' => $this->provider?->daily_price_2h,
  30. 'client_id' => $this->client?->id,
  31. 'profile_photo' => $this->resolveProfilePhoto(),
  32. 'client_document' => $this->client?->document,
  33. 'registration_complete' => $this->registration_complete,
  34. 'provider' => new ProviderResource($this->whenLoaded('provider')),
  35. 'client' => new ClientResource($this->whenLoaded('client')),
  36. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i'),
  37. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i'),
  38. ];
  39. }
  40. private function resolveProfilePhoto(): ?string
  41. {
  42. if ($this->provider) {
  43. $path = $this->provider->profileMedia?->path;
  44. } else {
  45. $path = $this->client?->profileMedia?->path;
  46. }
  47. return $path ? Storage::temporaryUrl($path, now()->addMinutes(60)) : null;
  48. }
  49. public static function collection($resource): AnonymousResourceCollection
  50. {
  51. return parent::collection($resource);
  52. }
  53. }