4444 */
4545class 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