ldc-crm/app/Http/Requests/Api/V4/SendClaimRequest.php

87 lines
3.3 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Http\Requests\Api\V4;
use App\Enums\Country;
use App\Http\Sanitizers\RemoveSpaces;
use ArondeParon\RequestSanitizer\Sanitizers\Capitalize;
use ArondeParon\RequestSanitizer\Sanitizers\Lowercase;
use ArondeParon\RequestSanitizer\Sanitizers\Trim;
use ArondeParon\RequestSanitizer\Sanitizers\TrimDuplicateSpaces;
class SendClaimRequest extends BaseApiRequest
{
/**
* Using these sanitizers before proceeding the request.
*
* @var array
*/
protected $sanitizers = [
'first_name' => [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}"],
2025-08-26 09:34:58 +02:00
'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',
];
}
}