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\Models;
7
8use Illuminate\Database\Eloquent\Builder;
9use Illuminate\Database\Eloquent\Relations\HasMany;
10
11/**
12 * @property string $acronym
13 * @property int $display
14 * @property string $name
15 * @property int $playcount
16 * @property int $pp
17 * @property \Illuminate\Database\Eloquent\Collection $profileBanners ProfileBanner
18 * @property int $rankedscore
19 * @property float $shipping_rate
20 * @property int $usercount
21 */
22class Country extends Model
23{
24 const UNKNOWN = 'XX';
25
26 protected $table = 'osu_countries';
27 protected $primaryKey = 'acronym';
28 protected $keyType = 'string';
29 public $incrementing = false;
30
31 public $timestamps = false;
32
33 public function scopeForStore($query)
34 {
35 return $query->select('acronym', 'name', 'display')
36 ->where('display', '>', 0)
37 ->orderBy('display', 'desc')
38 ->orderBy('name');
39 }
40
41 public function scopeWhereHasRuleset(Builder $query, string $ruleset): Builder
42 {
43 return $query->whereHas(
44 'statistics',
45 fn ($q) => $q
46 ->where('display', true)
47 ->where('mode', Beatmap::MODES[$ruleset]),
48 );
49 }
50
51 public function profileBanners()
52 {
53 return $this->hasMany(ProfileBanner::class, 'country_acronym');
54 }
55
56 public function statistics(): HasMany
57 {
58 return $this->hasMany(CountryStatistics::class, 'country_code');
59 }
60
61 public function getAttribute($key)
62 {
63 return match ($key) {
64 'acronym',
65 'display',
66 'name',
67 'playcount',
68 'pp',
69 'rankedscore',
70 'shipping_rate',
71 'usercount' => $this->getRawAttribute($key),
72
73 'profileBanners',
74 'statistics' => $this->getRelationValue($key),
75 };
76 }
77}