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
6declare(strict_types=1);
7
8namespace App\Transformers\Score;
9
10use App\Models\LegacyMatch;
11use App\Models\Multiplayer\ScoreLink as MultiplayerScoreLink;
12use App\Models\Score\Model as ScoreModel;
13use App\Models\Solo\Score as SoloScore;
14use App\Transformers\TransformerAbstract;
15
16class CurrentUserAttributesTransformer extends TransformerAbstract
17{
18 public function transform(LegacyMatch\Score|MultiplayerScoreLink|ScoreModel|SoloScore $score): array
19 {
20 if ($score instanceof SoloScore) {
21 $pinnable = $score;
22 } elseif ($score instanceof MultiplayerScoreLink) {
23 $pinnable = $score->score;
24 } else {
25 $pinnable = null;
26 }
27
28 return [
29 'pin' => $pinnable !== null && $this->isOwnScore($pinnable)
30 ? [
31 'is_pinned' => app('score-pins')->isPinned($pinnable),
32 'score_id' => $pinnable->getKey(),
33 ] : null,
34 ];
35 }
36
37 private function isOwnScore(LegacyMatch\Score|MultiplayerScoreLink|ScoreModel|SoloScore $score): bool
38 {
39 return $score->user_id === \Auth::user()?->getKey();
40 }
41}