/
itp_practice
/
itp_backend
Обзор
Документация
Войти
/
itp_practice
/
itp_backend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
app/Http/Resources/UserResource.php
65 строк
2 KB
vivanenko
profile edit
29 май 2025, 19:57
29 май 2025, 19:57
2ec716b
Код
Авторство
О чём код?
<?php namespace App\Http\Resources; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; /** * User Resource * * Transforms User model data for API responses following SOLID principles: * - Single Responsibility: Only handles user data transformation * - Open/Closed: Can be extended with additional fields */ class UserResource extends JsonResource { /** * Transform the resource into an array. * * @return array<string, mixed> */ public function toArray(Request $request): array { return [ 'id' => $this->id, 'name' => $this->name, 'email' => $this->email, 'email_verified_at' => $this->email_verified_at, 'telegram_id' => $this->telegram_id, 'telegram_username' => $this->telegram_username, 'is_telegram_verified' => $this->is_telegram_verified, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, // Include roles if they are loaded 'roles' => $this->whenLoaded('roles'), 'roles_list' => $this->when( $this->relationLoaded('roles'), fn() => $this->roles->pluck('name')->toArray() ), // Include current tariff if available 'current_tariff' => $this->current_tariff ?? null, // Include subscription data if loaded 'subscriptions' => $this->whenLoaded('subscriptions'), 'active_subscription' => $this->whenLoaded('activeSubscription'), ]; } /** * Get additional data that should be returned with the resource array. * * @return array<string, mixed> */ public function with(Request $request): array { return [ 'meta' => [ 'version' => '1.0', 'generated_at' => now()->toISOString() ] ]; } }