ActivityUpdateRequest.php 13.2 KB
<?php

namespace App\Http\Container\AdminSection\Requests\Audition;

use App\Enums\ActivitySongTypeEnum;
use App\Http\Request\ActivityExpandRequest;
use App\Http\Service\ActivityService;
use Hikoon\LaravelApi\Exceptions\ValidationException;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Support\Str;

class ActivityUpdateRequest extends ActivityExpandRequest
{
    /**
     * @throws \Hikoon\LaravelApi\Exceptions\ValidationException
     */
    protected function prepareForValidation(): void
    {
        if (!in_array($this->integer('song_type'), [ActivitySongTypeEnum::SONG->value, ActivitySongTypeEnum::DEMO->value], true)) {
            throw new ValidationException(ApiCode::VALIDATION_ERROR, '请选择歌曲类型');
        }
        $this->merge(['is_lyric' => (int)ActivityService::isLyric($this->input('lyric'))]);
    }

    /**
     * @return array<string,mixed>
     */
    public function rules(): array
    {
        return match ($this->integer('song_type')) {
            1 => [
                'cover'                         => 'required|url',
                'song_name'                     => 'required|string|between:0,100',
                'expand.tag_ids'                => 'required|array|between:1,3',
                'expand.tag_ids.*'              => 'numeric',
                'lang'                          => 'required|array|between:1,2',
                'speed'                         => 'required|string',
                'sub_title'                     => 'required|string|between:0,400',
                'project_id'                    => 'required|numeric',
                'expand.lyricist.ids'           => 'required_without:expand.lyricist.supplement|array|max:2',
                'expand.lyricist.ids.*'         => 'numeric',
                'expand.lyricist.supplement'    => 'required_without:expand.lyricist.ids|array|max:2',
                'expand.lyricist.supplement.*'  => 'string',
                'expand.composer.ids'           => 'required_without:expand.composer.supplement|array|max:2',
                'expand.composer.ids.*'         => 'numeric',
                'expand.composer.supplement'    => 'required_without:expand.composer.ids|array|max:2',
                'expand.composer.supplement.*'  => 'string',
                'expand.arranger.ids'           => 'required_without:expand.arranger.supplement|array|max:2',
                'expand.arranger.ids.*'         => 'numeric',
                'expand.arranger.supplement'    => 'required_without:expand.arranger.ids|array|max:2',
                'expand.arranger.supplement.*'  => 'string',
                'sex'                           => 'required|string',
                'estimate_release_at'           => 'required|date|date_format:Y-m-d',
                'expand.guide_source.url'       => 'required|url',
                'expand.guide_source.name'      => 'string',
                'expand.guide_source.size'      => 'integer',
                'expand.karaoke_source.url'     => 'required|url',
                'expand.karaoke_source.name'    => 'string',
                'expand.karaoke_source.size'    => 'integer',
                'expand.track_source.url'       => 'sometimes|url',
                'expand.track_source.name'      => 'sometimes|string',
                'expand.track_source.size'      => 'sometimes|numeric',
                'is_official'                   => 'required|in:0,1',
                'is_push'                       => 'required|in:0,1',
                'mark'                          => 'sometimes|string',
                'expand.push_type'              => 'required_if:is_push,1|array',
                'expand.push_user'              => 'array',
                'weight'                        => 'required|in:0,30,60,90',
                'out_side_manages'              => "sometimes|array",
                'out_side_manages.*.user_id'    => 'required|numeric',
                'out_side_manages.*.permission' => 'required|array|in:view,price,audit',
                'is_lyric'                      => 'required|in:1',
                'lyric'                         => 'required',
                'clip_lyric'                    => ['required', function ($attribute, $value, $fail) {
                    if (!ActivityService::isLyric($value)) {
                        $fail('推荐片段歌词文件类型错误');
                    }
                    if (!Str::contains($this->input('lyric', ''), $value)) {
                        $fail('推荐片段不在歌词中');
                    }
                }],
            ],
            2 => [
                'cover'                      => 'required|url',
                'song_name'                  => 'required|string|between:0,100',
                'project_id'                 => 'required|int',
                'expand.tag_ids'             => 'array|between:0,3',
                'expand.tag_ids.*'           => 'numeric',
                'expand.lyricist.ids'        => 'array|max:2',
                'expand.lyricist.ids.*'      => 'numeric',
                'expand.composer.ids'        => 'array|max:2',
                'expand.composer.ids.*'      => 'numeric',
                'expand.guide_source.url'    => 'required|url',
                'expand.guide_source.name'   => 'required|string',
                'expand.guide_source.size'   => 'required|integer',
                'expand.karaoke_source.url'  => 'sometimes|url',
                'expand.karaoke_source.name' => 'sometimes|string',
                'expand.karaoke_source.size' => 'sometimes|integer',
                'lyric'                      => 'required',
                'is_lyric'                   => 'required|in:0,1',
            ],
            default => []
        };
    }

    /**
     * @return array<string,string>
     */
    public function messages(): array
    {
        return match ($this->integer('song_type')) {
            1 => [
                'cover.required'                              => '请上传封面',
                'cover.url'                                   => '无效的封面地址',
                'song_name.required'                          => '请输入歌曲名称',
                'song_name.between'                           => '歌曲名称长度应在:min~:max字节内',
                'project_id.required'                         => '请选择关联厂牌',
                'project_id.int'                              => '关联厂牌类型错误',
                'expand.tag_ids.required'                     => '请选择关联标签',
                'expand.tag_ids.digits_between'               => '关联标签数量限制:min~:max个',
                'tag_ids.*.numeric'                           => '关联标签类型错误',
                'lang.required'                               => '请选择语种',
                'speed.required'                              => '请选择语速',
                'sub_title.required'                          => '请输入歌曲简介',
                'sub_title.between'                           => '歌曲简介长度应在:min~:max字节内',
                'sex.required'                                => '请选择性别要求',
                'out_side_manages.*'                          => '外部管理员类型错误',
                'estimate_release_at.required'                => '请选择预计发行时间',
                'expand.guide_source.url.required'            => '请上传导唱文件',
                'expand.guide_source.url.url'                 => '无效的导唱文件链接',
                'expand.karaoke_source.url.required'          => '请上传伴奏文件',
                'expand.karaoke_source.url.url'               => '无效的伴奏文件链接',
                'expand.track_source.url.url'                 => '无效的分轨文件链接',
                'is_lyric.in'                                 => '歌词文件类型错误',
                'lyric.required'                              => '请上传歌词文件',
                'clip_lyric.required'                         => '请上传推荐片段歌词文件',
                'expand.lyricist.ids.required_without'        => '词作者人数不能低于1人',
                'expand.lyricist.ids.array'                   => '词作者(用户)类型错误',
                'expand.lyricist.ids.max'                     => '词作者(用户)最多:max人',
                'expand.lyricist.ids.*.numeric'               => '词作者(用户)类型错误',
                'expand.lyricist.supplement.required_without' => '词作者人数不能低于1人',
                'expand.lyricist.supplement.array'            => '词作者(未注册)类型错误',
                'expand.lyricist.supplement.max'              => '词作者(未注册)最多:max人',
                'expand.lyricist.supplement.*.numeric'        => '词作者(未注册)类型错误',
                'expand.composer.ids.required_without'        => '曲作者人数不能低于1人',
                'expand.composer.ids.array'                   => '曲作者(用户)类型错误',
                'expand.composer.ids.max'                     => '曲作者(用户)最多:max人',
                'expand.composer.ids.*.numeric'               => '曲作者(用户)类型错误',
                'expand.composer.supplement.required_without' => '曲作者人数不能低于1人',
                'expand.composer.supplement.array'            => '曲作者(未注册)类型错误',
                'expand.composer.supplement.max'              => '曲作者(未注册)最多:max人',
                'expand.composer.supplement.*.numeric'        => '曲作者(未注册)类型错误',
                'expand.arranger.ids.required_without'        => '编曲人数不能低于1人',
                'expand.arranger.ids.array'                   => '编曲(用户)类型错误',
                'expand.arranger.ids.max'                     => '编曲(用户)最多:max人',
                'expand.arranger.ids.*.numeric'               => '编曲(用户)类型错误',
                'expand.arranger.supplement.required_without' => '编曲人数不能低于1人',
                'expand.arranger.supplement.array'            => '编曲(未注册)类型错误',
                'expand.arranger.supplement.max'              => '编曲(未注册)最多:max人',
                'expand.arranger.supplement.*.numeric'        => '编曲(未注册)类型错误',
                'is_official.required'                        => '请选择奖励标识',
                'is_official.in'                              => '奖励标识不在指定范围内',
                'is_push.required'                            => '请选择是否开启推送',
                'is_push.in'                                  => '推送值不在指定范围内',
                'expand.push_type.required_if'                => '请选择推送方式',
                'expand.push_type.array'                      => '推送方式类型错误',
                'weight.required'                             => '请选择推荐权重',
                'weight.in'                                   => '推荐权重不在指定范围内'
            ],
            2 => [
                'cover.required'                       => '请上传封面',
                'cover.url'                            => '无效的封面地址',
                'song_name.required'                   => '请输入歌曲名称',
                'song_name.between'                    => '歌曲名称长度应在:min~:max字节内',
                'project_id.required'                  => '请选择关联厂牌',
                'project_id.int'                       => '关联厂牌类型错误',
                'expand.lyricist.ids.array'            => '词作者(用户)类型错误',
                'expand.lyricist.ids.max'              => '词作者(用户)最多:max人',
                'expand.lyricist.ids.*.numeric'        => '词作者(用户)类型错误',
                'expand.lyricist.supplement.array'     => '词作者(未注册)类型错误',
                'expand.lyricist.supplement.max'       => '词作者(未注册)最多:max人',
                'expand.lyricist.supplement.*.numeric' => '词作者(未注册)类型错误',
                'expand.composer.ids.array'            => '曲作者(用户)类型错误',
                'expand.composer.ids.max'              => '曲作者(用户)最多:max人',
                'expand.composer.ids.*.numeric'        => '曲作者(用户)类型错误',
                'expand.composer.supplement.array'     => '曲作者(未注册)类型错误',
                'expand.composer.supplement.max'       => '曲作者(未注册)最多:max人',
                'expand.composer.supplement.*.numeric' => '曲作者(未注册)类型错误',
                'expand.guide_source.url.required'     => '请上传导唱文件',
                'expand.guide_source.url.url'          => '无效的导唱文件链接',
                'expand.karaoke_source.url'            => '请上传伴奏文件',
                'expand.karaoke_source.url.url'        => '无效的伴奏文件链接',
                'lyric.required'                       => '请上传歌词文件',
            ],
            default => []
        };
    }
}