66 lines
1.6 KiB
PHP
66 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Http\Requests\Concerns\ModeratesUserContent;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class UpdateUserRequest extends FormRequest
|
|
{
|
|
use ModeratesUserContent;
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'name' => [
|
|
'sometimes',
|
|
'string',
|
|
'min:3',
|
|
'max:32',
|
|
'regex:/^[a-zA-Z0-9_.-]+$/',
|
|
Rule::unique('users', 'name')->ignore($this->user()),
|
|
],
|
|
'bio' => ['sometimes', 'nullable', 'string'],
|
|
'locale' => ['sometimes', 'string', Rule::in(config('app.supported_locales', ['fr', 'en']))],
|
|
'avatar' => ['sometimes', 'nullable', 'image', 'max:2048'],
|
|
];
|
|
}
|
|
|
|
public function after(): array
|
|
{
|
|
return $this->moderationChecks(
|
|
[$this->input('name'), $this->input('bio')],
|
|
[$this->file('avatar')],
|
|
);
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'avatar.max' => __('api.validation.image_max_2mb'),
|
|
];
|
|
}
|
|
|
|
protected function prepareForValidation(): void
|
|
{
|
|
if ($this->has('locale')) {
|
|
$this->merge([
|
|
'locale' => $this->normalizeLocale((string) $this->input('locale')),
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function normalizeLocale(string $locale): string
|
|
{
|
|
$locale = strtolower(str_replace('_', '-', $locale));
|
|
|
|
return explode('-', $locale)[0];
|
|
}
|
|
}
|