service-captcha/app/Http/Requests/Private/Users/StoreUpdateRequest.php

45 lines
1.2 KiB
PHP

<?php declare(strict_types=1);
namespace App\Http\Requests\Private\Users;
use App\Contracts\FormRequestDto;
use App\Dto\Request\Private\User\StoreUpdate;
use App\Dto\User\ManyRoleDto;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Password;
final class StoreUpdateRequest extends FormRequest implements FormRequestDto
{
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
$rules = [
'name' => ['required', 'max:255'],
'email' => ['required', 'email', 'max:255'],
'is_active' => ['required', 'boolean'],
'roles' => ['array', Rule::exists('roles', 'id')],
];
if ($this->getMethod() === 'POST') {
$rules['password'] = ['required', Password::default()];
}
return $rules;
}
public function getDto(): StoreUpdate
{
return new StoreUpdate(
name: $this->input('name'),
email: $this->input('email'),
isActive: (bool) $this->input('is_active', false),
roles: new ManyRoleDto($this->input('roles', [])),
password: $this->input('password', null),
);
}
}