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\Casts;
9
10use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
11use Illuminate\Database\Eloquent\Model;
12
13class LegacyFilename implements CastsAttributes
14{
15 public static function makeFromAttributes(?array $attributes): ?string
16 {
17 if (!isset($attributes['hash'])) {
18 return null;
19 }
20
21 $filename = $attributes['hash'];
22 if (isset($attributes['ext'])) {
23 $filename .= ".{$attributes['ext']}";
24 }
25
26 return $filename;
27 }
28
29 public function get(Model $model, string $key, mixed $value, array $attributes)
30 {
31 return static::makeFromAttributes($attributes);
32 }
33
34 public function set(Model $model, string $key, mixed $value, array $attributes)
35 {
36 return [
37 'ext' => null,
38 'hash' => $value,
39 ];
40 }
41}