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\Build;
9use App\Models\ChangelogEntry;
10
11class BuildTransformer extends TransformerAbstract
12{
13 protected array $availableIncludes = [
14 'changelog_entries',
15 'update_stream',
16 'versions',
17 ];
18
19 protected array $defaultIncludes = [
20 'update_stream',
21 ];
22
23 public function transform(Build $build)
24 {
25 return [
26 'created_at' => json_time($build->date),
27 'display_version' => $build->displayVersion(),
28 'id' => $build->getKey(),
29 'users' => $build->users ?? 0,
30 'version' => $build->version,
31 'youtube_id' => $build->youtube_id,
32 ];
33 }
34
35 public function includeChangelogEntries(Build $build)
36 {
37 $legacyEntries = $build
38 ->defaultChangelogs
39 ->filter(fn ($item) => $item->stream_id === $build->stream_id)
40 ->map(fn ($item) => ChangelogEntry::convertLegacy($item));
41
42 $entries = $build
43 ->defaultChangelogEntries
44 ->concat($legacyEntries)
45 ->sortBy('created_at');
46
47 if ($entries->count() === 0) {
48 $entries = collect([ChangelogEntry::placeholder()]);
49 }
50
51 return $this->collection($entries, new ChangelogEntryTransformer());
52 }
53
54 public function includeUpdateStream(Build $build)
55 {
56 return $this->item($build->updateStream, new UpdateStreamTransformer());
57 }
58
59 public function includeVersions(Build $build)
60 {
61 $versions = [];
62
63 $next = $build->versionNext();
64 if ($next !== null) {
65 $versions['next'] = json_item($next, $this);
66 }
67
68 $previous = $build->versionPrevious();
69 if ($previous !== null) {
70 $versions['previous'] = json_item($previous, $this);
71 }
72
73 return $this->primitive($versions);
74 }
75}