@@ -69,14 +69,6 @@ class Csv extends BaseReader
69
69
*/
70
70
private ?string $ escapeCharacter = null ;
71
71
72
- /**
73
- * The character that will be supplied to fgetcsv
74
- * when escapeCharacter is null.
75
- * It is anticipated that it will conditionally be set
76
- * to null-string for Php9 and above.
77
- */
78
- private static string $ defaultEscapeCharacter = PHP_VERSION_ID < 90000 ? '\\' : '' ;
79
-
80
72
/**
81
73
* Callback for setting defaults in construction.
82
74
*
@@ -200,7 +192,7 @@ protected function inferSeparator(): void
200
192
return ;
201
193
}
202
194
203
- $ inferenceEngine = new Delimiter ($ this ->fileHandle , $ this ->escapeCharacter ?? self :: $ defaultEscapeCharacter , $ this ->enclosure );
195
+ $ inferenceEngine = new Delimiter ($ this ->fileHandle , $ this ->getEscapeCharacter () , $ this ->enclosure );
204
196
205
197
// If number of lines is 0, nothing to infer : fall back to the default
206
198
if ($ inferenceEngine ->linesCounted () === 0 ) {
@@ -323,10 +315,10 @@ public function setTestAutoDetect(bool $value): self
323
315
return $ this ;
324
316
}
325
317
326
- private function setAutoDetect (?string $ value ): ?string
318
+ private function setAutoDetect (?string $ value, int $ version = PHP_VERSION_ID ): ?string
327
319
{
328
320
$ retVal = null ;
329
- if ($ value !== null && $ this ->testAutodetect && PHP_VERSION_ID < 90000 ) {
321
+ if ($ value !== null && $ this ->testAutodetect && $ version < 90000 ) {
330
322
$ retVal2 = @ini_set ('auto_detect_line_endings ' , $ value );
331
323
if (is_string ($ retVal2 )) {
332
324
$ retVal = $ retVal2 ;
@@ -566,9 +558,9 @@ public function getContiguous(): bool
566
558
* Not yet ready to mark deprecated in order to give users
567
559
* a migration path.
568
560
*/
569
- public function setEscapeCharacter (string $ escapeCharacter ): self
561
+ public function setEscapeCharacter (string $ escapeCharacter, int $ version = PHP_VERSION_ID ): self
570
562
{
571
- if (PHP_VERSION_ID >= 90000 && $ escapeCharacter !== '' ) {
563
+ if ($ version >= 90000 && $ escapeCharacter !== '' ) {
572
564
throw new ReaderException ('Escape character must be null string for Php9+ ' );
573
565
}
574
566
@@ -577,9 +569,9 @@ public function setEscapeCharacter(string $escapeCharacter): self
577
569
return $ this ;
578
570
}
579
571
580
- public function getEscapeCharacter (): string
572
+ public function getEscapeCharacter (int $ version = PHP_VERSION_ID ): string
581
573
{
582
- return $ this ->escapeCharacter ?? self ::$ defaultEscapeCharacter ;
574
+ return $ this ->escapeCharacter ?? self ::getDefaultEscapeCharacter ( $ version ) ;
583
575
}
584
576
585
577
/**
@@ -705,10 +697,11 @@ private static function getCsv(
705
697
?int $ length = null ,
706
698
string $ separator = ', ' ,
707
699
string $ enclosure = '" ' ,
708
- ?string $ escape = null
700
+ ?string $ escape = null ,
701
+ int $ version = PHP_VERSION_ID
709
702
): array |false {
710
- $ escape = $ escape ?? self ::$ defaultEscapeCharacter ;
711
- if (PHP_VERSION_ID >= 80400 && $ escape !== '' ) {
703
+ $ escape = $ escape ?? self ::getDefaultEscapeCharacter () ;
704
+ if ($ version >= 80400 && $ escape !== '' ) {
712
705
return @fgetcsv ($ stream , $ length , $ separator , $ enclosure , $ escape );
713
706
}
714
707
@@ -720,10 +713,11 @@ public static function affectedByPhp9(
720
713
string $ inputEncoding = 'UTF-8 ' ,
721
714
?string $ delimiter = null ,
722
715
string $ enclosure = '" ' ,
723
- string $ escapeCharacter = '\\'
716
+ string $ escapeCharacter = '\\' ,
717
+ int $ version = PHP_VERSION_ID
724
718
): bool {
725
- if (PHP_VERSION_ID < 70400 || PHP_VERSION_ID >= 90000 ) {
726
- throw new ReaderException ('Function valid only for Php7.4 or Php8 ' ); // @codeCoverageIgnore
719
+ if ($ version < 70400 || $ version >= 90000 ) {
720
+ throw new ReaderException ('Function valid only for Php7.4 or Php8 ' );
727
721
}
728
722
$ reader1 = new self ();
729
723
$ reader1 ->setInputEncoding ($ inputEncoding )
@@ -749,4 +743,15 @@ public static function affectedByPhp9(
749
743
750
744
return $ array1 !== $ array2 ;
751
745
}
746
+
747
+ /**
748
+ * The character that will be supplied to fgetcsv
749
+ * when escapeCharacter is null.
750
+ * It is anticipated that it will conditionally be set
751
+ * to null-string for Php9 and above.
752
+ */
753
+ private static function getDefaultEscapeCharacter (int $ version = PHP_VERSION_ID ): string
754
+ {
755
+ return $ version < 90000 ? '\\' : '' ;
756
+ }
752
757
}
0 commit comments