Skip to content

Commit 5e76451

Browse files
committed
Try to guess the character encodings and convert it to UTF-8 on importing
This should fix issue #749
1 parent a873ad3 commit 5e76451

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/Services/ImportExportSystem/EntityImporter.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
*/
4545
class EntityImporter
4646
{
47+
48+
/**
49+
* The encodings that are supported by the importer, and that should be autodeceted.
50+
*/
51+
private const ENCODINGS = ["ASCII", "UTF-8", "ISO-8859-1", "ISO-8859-15", "Windows-1252", "UTF-16", "UTF-32"];
52+
4753
public function __construct(protected SerializerInterface $serializer, protected EntityManagerInterface $em, protected ValidatorInterface $validator)
4854
{
4955
}
@@ -65,6 +71,9 @@ public function __construct(protected SerializerInterface $serializer, protected
6571
*/
6672
public function massCreation(string $lines, string $class_name, ?AbstractStructuralDBElement $parent = null, array &$errors = []): array
6773
{
74+
//Try to detect the text encoding of the data and convert it to UTF-8
75+
$lines = mb_convert_encoding($lines, 'UTF-8', mb_detect_encoding($lines, self::ENCODINGS));
76+
6877
//Expand every line to a single entry:
6978
$names = explode("\n", $lines);
7079

@@ -159,6 +168,9 @@ public function massCreation(string $lines, string $class_name, ?AbstractStructu
159168
*/
160169
public function importString(string $data, array $options = [], array &$errors = []): array
161170
{
171+
//Try to detect the text encoding of the data and convert it to UTF-8
172+
$data = mb_convert_encoding($data, 'UTF-8', mb_detect_encoding($data, self::ENCODINGS));
173+
162174
$resolver = new OptionsResolver();
163175
$this->configureOptions($resolver);
164176
$options = $resolver->resolve($options);

0 commit comments

Comments
 (0)