api/app/Http/Resources/UserResource.php

50 lines
1.9 KiB
PHP

<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Storage;
class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id, // à voir si suppression par la suite
'name' => $this->name,
'email' => $this->email,
'locale' => $this->locale,
'emailVerified' => $this->hasVerifiedEmail(),
'emailVerifiedAt' => $this->email_verified_at?->toISOString(),
'height' => $this->height,
'avatarUrl' => $this->avatar_url ? asset(Storage::url($this->avatar_url)) : null,
'bio' => $this->bio,
'role' => $this->role,
'accountVerified' => $this->account_verified_at !== null,
'aiCreditsBalance' => (int) $this->ai_credits_balance,
'suspendedAt' => $this->suspended_at?->toISOString(),
'physicalActivityLevel' => $this->physical_activity_level,
'physicalActivityLevelLabel' => $this->physical_activity_level?->getLabel(),
'weightGoal' => $this->weight_goal,
'weightGoalLabel' => $this->weight_goal?->getLabel(),
'pacePreference' => $this->pace_preference,
'pacePreferenceLabel' => $this->pace_preference?->getLabel(),
'sex' => $this->sex,
'sexLabel' => $this->sex?->getLabel(),
'dateOfBirth' => $this->date_of_birth?->toDateString(),
'nutritionGoals' => [
'calories' => (int) $this->daily_calorie_goal,
'proteins' => (float) $this->daily_protein_goal,
'carbs' => (float) $this->daily_carbs_goal,
'fats' => (float) $this->daily_fats_goal,
],
];
}
}