[Trim::class, TrimDuplicateSpaces::class, Capitalize::class], 'last_name' => [Trim::class, TrimDuplicateSpaces::class, Capitalize::class], 'email' => [Trim::class, RemoveSpaces::class, Lowercase::class], 'address' => [Trim::class, TrimDuplicateSpaces::class], 'city' => [Trim::class, TrimDuplicateSpaces::class], 'postcode' => [Trim::class], 'file' => [Trim::class], 'message' => [Trim::class], ]; /** * Determine if the user is authorized to make this request. * * @return boolean */ public function authorize(): bool { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules(): array { $countryList = ($this->currentCountry && $this->currentCountry->get('iso')) ? $this->currentCountry->get('iso') : implode(',', Country::getValidIsoAplha2Codes()); return [ 'first_name' => 'required|string|between:3,255', 'last_name' => 'required|string|between:3,255', 'email' => 'required|email:rfc,dns,spoof,filter|max:255', 'address' => 'sometimes|nullable|string|max:255', 'city' => 'sometimes|nullable|string|between:2,255', 'postcode' => ['sometimes', 'nullable', 'string', "postal_code:{$countryList}"], 'file' => [ 'sometimes', 'nullable', 'string', // @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed function ($attribute, $value, $fail) { // Base64 validation. $decoded = base64_decode($value, true); if ($decoded === false) { return $fail('Le fichier doit ĂȘtre un encodage base64 valide.'); } // Check the size (5 MB max = 5 * 1024 * 1024). if (strlen($decoded) > 5 * 1024 * 1024) { return $fail('Le fichier est trop volumineux (max 5 Mo).'); } // Check the MIME type. $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_buffer($finfo, $decoded); $allowed = ['image/jpeg', 'image/png', 'application/pdf']; if (!in_array($mime, $allowed)) { return $fail('Le fichier doit ĂȘtre une image JPG/PNG ou un PDF.'); } } ], 'message' => 'required|string|min:3', 'subject_id' => 'required|integer|exists:claim_subjects,id', ]; } }