|
20 | 20 | */ |
21 | 21 | class DynamicUserResource extends VersionedJsonResource |
22 | 22 | { |
23 | | - /** |
24 | | - * Define version configurations directly in the resource |
25 | | - */ |
26 | | - protected array $versionConfigs = [ |
27 | | - '1.0' => ['id', 'name'], |
28 | | - '1.1' => ['id', 'name', 'email'], |
29 | | - '2.0' => ['id', 'name', 'email', 'created_at', 'profile'], |
30 | | - '2.1' => ['id', 'name', 'email', 'created_at', 'updated_at', 'profile', 'preferences', 'stats'], |
31 | | - ]; |
| 23 | + /** |
| 24 | + * Define version configurations directly in the resource |
| 25 | + */ |
| 26 | + protected array $versionConfigs = [ |
| 27 | + '1.0' => ['id', 'name'], |
| 28 | + '1.1' => ['id', 'name', 'email'], |
| 29 | + '2.0' => ['id', 'name', 'email', 'created_at', 'profile'], |
| 30 | + '2.1' => ['id', 'name', 'email', 'created_at', 'updated_at', 'profile', 'preferences', 'stats'], |
| 31 | + ]; |
32 | 32 |
|
33 | | - protected function toArrayDefault(Request $request): array |
34 | | - { |
35 | | - $version = $this->getCurrentApiVersion(); |
36 | | - $config = $this->versionConfigs[$version] ?? $this->versionConfigs['2.1']; |
| 33 | + protected function toArrayDefault(Request $request): array |
| 34 | + { |
| 35 | + $version = $this->getCurrentApiVersion(); |
| 36 | + $config = $this->versionConfigs[$version] ?? $this->versionConfigs['2.1']; |
37 | 37 |
|
38 | | - $data = []; |
39 | | - foreach ($config as $field) { |
40 | | - $data[$field] = $this->getFieldValue($field); |
41 | | - } |
| 38 | + $data = []; |
| 39 | + foreach ($config as $field) { |
| 40 | + $data[$field] = $this->getFieldValue($field); |
| 41 | + } |
42 | 42 |
|
43 | | - return $data; |
44 | | - } |
| 43 | + return $data; |
| 44 | + } |
45 | 45 |
|
46 | | - private function getFieldValue(string $field): mixed |
47 | | - { |
48 | | - return match($field) { |
49 | | - 'id' => $this->id, |
50 | | - 'name' => $this->name, |
51 | | - 'email' => $this->email, |
52 | | - 'created_at' => $this->created_at->toISOString(), |
53 | | - 'updated_at' => $this->updated_at->toISOString(), |
54 | | - 'profile' => [ |
55 | | - 'avatar' => $this->avatar_url, |
56 | | - 'bio' => $this->bio, |
57 | | - ], |
58 | | - 'preferences' => [ |
59 | | - 'theme' => $this->theme ?? 'light', |
60 | | - 'language' => $this->language ?? 'en', |
61 | | - ], |
62 | | - 'stats' => [ |
63 | | - 'login_count' => $this->login_count ?? 0, |
64 | | - 'posts_count' => $this->posts_count ?? 0, |
65 | | - ], |
66 | | - default => $this->resource->$field ?? null, |
67 | | - }; |
68 | | - } |
| 46 | + private function getFieldValue(string $field): mixed |
| 47 | + { |
| 48 | + return match($field) { |
| 49 | + 'id' => $this->id, |
| 50 | + 'name' => $this->name, |
| 51 | + 'email' => $this->email, |
| 52 | + 'created_at' => $this->created_at->toISOString(), |
| 53 | + 'updated_at' => $this->updated_at->toISOString(), |
| 54 | + 'profile' => [ |
| 55 | + 'avatar' => $this->avatar_url, |
| 56 | + 'bio' => $this->bio, |
| 57 | + ], |
| 58 | + 'preferences' => [ |
| 59 | + 'theme' => $this->theme ?? 'light', |
| 60 | + 'language' => $this->language ?? 'en', |
| 61 | + ], |
| 62 | + 'stats' => [ |
| 63 | + 'login_count' => $this->login_count ?? 0, |
| 64 | + 'posts_count' => $this->posts_count ?? 0, |
| 65 | + ], |
| 66 | + default => data_get($this->resource, $field), |
| 67 | + }; |
| 68 | + } |
69 | 69 | } |
0 commit comments