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\NewsPost;
9
10class NewsPostTransformer extends TransformerAbstract
11{
12 protected array $availableIncludes = [
13 'content',
14 'navigation',
15 'preview',
16 ];
17
18 public function transform(NewsPost $post)
19 {
20 $firstImage = $post->firstImageWith2x();
21
22 return [
23 'id' => $post->id,
24
25 'author' => $post->author(),
26 'edit_url' => $post->editUrl(),
27 'first_image' => $firstImage['1x'],
28 'first_image@2x' => $firstImage['2x'],
29 'published_at' => json_time($post->published_at),
30 'updated_at' => json_time($post->updated_at),
31 'slug' => $post->slug,
32 'title' => $post->title(),
33 ];
34 }
35
36 public function includeContent(NewsPost $post)
37 {
38 return $this->primitive($post->bodyHtml());
39 }
40
41 public function includeNavigation(NewsPost $post)
42 {
43 $ret = [];
44
45 $newer = $post->newer();
46 if ($newer !== null) {
47 $ret['newer'] = json_item($newer, $this);
48 }
49
50 $older = $post->older();
51 if ($older !== null) {
52 $ret['older'] = json_item($older, $this);
53 }
54
55 return $this->primitive($ret);
56 }
57
58 public function includePreview(NewsPost $post)
59 {
60 return $this->primitive($post->previewText());
61 }
62}