1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Transformers;
7
8use App\Models\Beatmap;
9use App\Models\BeatmapFailtimes;
10use App\Models\DeletedUser;
11use App\Models\User;
12
13class BeatmapCompactTransformer extends TransformerAbstract
14{
15 protected array $availableIncludes = [
16 'beatmapset',
17 'checksum',
18 'failtimes',
19 'max_combo',
20 'owners',
21 'top_tag_ids',
22 'user',
23 ];
24
25 protected $beatmapsetTransformer = BeatmapsetCompactTransformer::class;
26
27 public function transform(Beatmap $beatmap)
28 {
29 return [
30 'beatmapset_id' => $beatmap->beatmapset_id,
31 'difficulty_rating' => $beatmap->difficultyrating,
32 'id' => $beatmap->beatmap_id,
33 'mode' => $beatmap->mode,
34 'status' => $beatmap->status(),
35 'total_length' => $beatmap->total_length,
36 'user_id' => $beatmap->user_id,
37 'version' => $beatmap->version,
38 ];
39 }
40
41 public function includeBeatmapset(Beatmap $beatmap)
42 {
43 $beatmapset = $beatmap->beatmapset;
44
45 return $beatmapset === null
46 ? $this->primitive(null)
47 : $this->item($beatmap->beatmapset, new $this->beatmapsetTransformer());
48 }
49
50 public function includeChecksum(Beatmap $beatmap)
51 {
52 return $this->primitive($beatmap->checksum);
53 }
54
55 public function includeFailtimes(Beatmap $beatmap)
56 {
57 $failtimes = $beatmap->failtimes;
58
59 $result = [];
60
61 foreach ($failtimes as $failtime) {
62 $result[$failtime->type] = $failtime->data;
63 }
64
65 foreach (['exit', 'fail'] as $type) {
66 if (!isset($result[$type])) {
67 $result[$type] = (new BeatmapFailtimes())->data;
68 }
69 }
70
71 return $this->primitive($result);
72 }
73
74 public function includeMaxCombo(Beatmap $beatmap)
75 {
76 return $this->primitive($beatmap->maxCombo());
77 }
78
79 public function includeOwners(Beatmap $beatmap)
80 {
81 return $this->collection($beatmap->getOwners(), fn (User $user) => [
82 'id' => $user->getKey(),
83 'username' => $user->username,
84 ]);
85 }
86
87 public function includeTopTagIds(Beatmap $beatmap)
88 {
89 return $this->primitive($beatmap->topTagIds());
90 }
91
92 public function includeUser(Beatmap $beatmap)
93 {
94 return $this->item(
95 $beatmap->user ?? new DeletedUser(),
96 new UserCompactTransformer(),
97 );
98 }
99}