59 lines
1.7 KiB
PHP
59 lines
1.7 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Http\Requests\Storage;
|
|
|
|
use App\Contracts\FormRequestDto;
|
|
use App\Dto\Service\Storage\Upload;
|
|
use App\Enums\Morph;
|
|
use App\Enums\StorageType;
|
|
use Illuminate\Contracts\Validation\Validator;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
use Illuminate\Validation\Rules\Enum;
|
|
|
|
final class ImageRequest extends FormRequest implements FormRequestDto
|
|
{
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$mimes = '';
|
|
if ($this->has('storage_type')) {
|
|
$storageType = StorageType::tryFrom((int) $this->input('storage_type')) ?? [];
|
|
$mimes = implode(',', $storageType->getAcceptMimes());
|
|
}
|
|
|
|
return [
|
|
'file' => ['required', 'file', 'mimes:' . $mimes],
|
|
'storage_type' => ['required', 'numeric', new Enum(StorageType::class)],
|
|
'morph' => ['required', 'numeric', new Enum(Morph::class)],
|
|
];
|
|
}
|
|
|
|
public function getDto(): Upload
|
|
{
|
|
return new Upload(
|
|
file: $this->file('file'),
|
|
storageType: StorageType::from((int) $this->input('storage_type')),
|
|
morph: Morph::from((int) $this->input('morph')),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get the error messages for the defined validation rules.*
|
|
* @return array
|
|
*/
|
|
protected function failedValidation(Validator $validator): array
|
|
{
|
|
/**
|
|
* To always return json
|
|
*/
|
|
throw new HttpResponseException(response()->json([
|
|
'errors' => $validator->errors(),
|
|
'status' => true
|
|
], 422));
|
|
}
|
|
|
|
}
|