ldc-boutique-cnc-sable/app/Http/Requests/Bo/ImportRequest.php

53 lines
1.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Http\Requests\Bo;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
class ImportRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize()
{
return Auth::check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'import' => [
'required',
'file',
'mimes:csv,txt',
// phpcs:ignore Generic.Files.LineLength.TooLong
'mimetypes:text/csv,text/plain,application/csv,text/comma-separated-values,text/anytext,application/octet-stream,application/txt',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
2024-04-22 10:00:22 +02:00
/** @var \Illuminate\Http\UploadedFile */
$uploadedFile = $this->file('import');
$content = $uploadedFile->get();
if (
2024-04-22 10:00:22 +02:00
$uploadedFile->getClientMimeType() !== 'text/csv'
&& $uploadedFile->getClientMimeType() !== 'application/vnd.ms-excel'
) {
$fail(__('Le fichier doit être d\'un mime type text/csv ou application/vnd.ms-excel'));
}
2024-04-22 10:36:58 +02:00
if (!$content or mb_detect_encoding($content, ['ISO-8859-1'], true) !== 'ISO-8859-1') {
2024-04-22 10:00:22 +02:00
$fail(__('Le fichier doit etre encodé en ISO-8859-1'));
}
}
]
];
}
}