authService->check(); } public function getSalarieImportFile(): CsvImportFile { throw_if( $this->importedFile === null, \UnexpectedValueException::class, 'importedFile should not be null' ); return $this->importedFile; } public function toImportEmployeesInput(): ImportEmployeesInput { return new ImportEmployeesInput( file: $this->getSalarieImportFile(), ); } /** @return array */ public function rules(): array { return [ 'import' => [ 'required', 'file', 'mimes:csv,txt', 'mimetypes:text/csv,text/plain,application/csv,text/comma-separated-values,text/anytext,application/octet-stream,application/txt', 'max:5120', function ($attribute, $__, $fail): void { $uploadedFile = $this->file('import'); $content = $uploadedFile->get(); if ( $content === false || $uploadedFile->getSize() === 0 || ($uploadedFile->getMimeType() !== 'text/plain' && $uploadedFile->getClientMimeType() !== 'text/csv' && $uploadedFile->getClientMimeType() !== 'application/vnd.ms-excel') ) { $fail(trans('validation.mimes', [ 'attribute' => $attribute, 'mimes' => 'text/csv, application/vnd.ms-excel', ])); return; } try { StringEncoder::detectAndConvertToUtf8($content, 'Windows-1252'); } catch (\ValueError) { $fail(trans('validation.encoding', [ 'attribute' => $attribute, 'encoding' => 'ISO-8859-1(5), Windows-1252, UTF-8', ])); } }, ], ]; } #[\Override] protected function passedValidation() { try { $uploadedFile = $this->file('import'); if ($uploadedFile instanceof UploadedFile) { $content = $uploadedFile->get(); if ($content === false) { throw new \UnexpectedValueException('Cannot read file'); } $utf8Content = StringEncoder::detectAndConvertToUtf8($content, 'Windows-1252'); $this->importedFile = new CsvImportFile( $uploadedFile->getRealPath(), $utf8Content ); } } catch (\Throwable) { $this->importedFile = null; } } }