ActivityExpandRequest.php 2.5 KB
<?php

namespace App\Http\Request;

use Hikoon\LaravelApi\Support\ApiRequest;
use Illuminate\Support\ValidatedInput;

class ActivityExpandRequest extends ApiRequest
{
    protected function getExpand(): array
    {
        $expand = $this->input('expand', []);
        return [
            'push_type'      => ['tag'],
            'push_user'      => [],
            'tag_ids'        => data_get($expand, 'tag_ids', []),
            'lyricist'       => [
                'ids'        => data_get($expand, 'lyricist.ids', []),
                'supplement' => data_get($expand, 'lyricist.supplement', []),
            ],
            'composer'       => [
                'ids'        => data_get($expand, 'composer.ids', []),
                'supplement' => data_get($expand, 'composer.supplement', []),
            ],
            'arranger'       => [
                'ids'        => data_get($expand, 'arranger.ids', []),
                'supplement' => data_get($expand, 'arranger.supplement', []),
            ],
            'track_source'   => [
                'url'  => data_get($expand, 'track_source.url', ''),
                'name' => data_get($expand, 'track_source.name', ''),
                'size' => data_get($expand, 'track_source.size', 0),
            ],
            'guide_source'   => [
                'url'  => data_get($expand, 'guide_source.url', ''),
                'name' => data_get($expand, 'guide_source.name', ''),
                'size' => data_get($expand, 'guide_source.size', 0),
            ],
            'karaoke_source' => [
                'url'  => data_get($expand, 'karaoke_source.url', ''),
                'name' => data_get($expand, 'karaoke_source.name', ''),
                'size' => data_get($expand, 'karaoke_source.size', 0),
            ],
        ];
    }

    /**
     * @inheritDoc
     */
    protected function prepareForValidation(): void
    {
        $this->merge(['expand' => $this->getExpand()]);
    }

    /**
     * @inheritDoc
     */
    public function validated($key = NULL, $default = NULL)
    {
        return array_merge(parent::validated($key, $default), ['expand' => $this->getExpand()]);
    }

    /**
     * @param array|NULL $keys
     * @return array|array[]|\Illuminate\Support\ValidatedInput
     */
    public function safe(array $keys = NULL): array|ValidatedInput
    {
        return is_array($keys)
            ? $this->validator->safe()->only($keys) + ['expand' => $this->getExpand()]
            : $this->validator->safe()->merge(['expand' => $this->getExpand()]);
    }
}