Skip to content

Commit 2a8fb60

Browse files
committed
Some Long-Term Prep
Enable us to determine if user has explicitly changed escape character vs. using default.
1 parent 352eaae commit 2a8fb60

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/PhpSpreadsheet/Reader/Csv.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,19 @@ class Csv extends BaseReader
6262

6363
/**
6464
* The character that can escape the enclosure.
65+
* This will probably become unsupported in Php 9.
66+
* Not yet ready to mark deprecated in order to give users
67+
* a migration path.
6568
*/
66-
private string $escapeCharacter = '\\';
69+
private ?string $escapeCharacter = null;
70+
71+
/**
72+
* The character that will be supplied to fgetcsv
73+
* when escapeCharacter is null.
74+
* It is anticipated that it will conditionally be set
75+
* to null-string for Php9 and above.
76+
*/
77+
private static string $defaultEscapeCharacter = '\\';
6778

6879
/**
6980
* Callback for setting defaults in construction.
@@ -185,7 +196,7 @@ protected function inferSeparator(): void
185196
return;
186197
}
187198

188-
$inferenceEngine = new Delimiter($this->fileHandle, $this->escapeCharacter, $this->enclosure);
199+
$inferenceEngine = new Delimiter($this->fileHandle, $this->escapeCharacter ?? self::$defaultEscapeCharacter, $this->enclosure);
189200

190201
// If number of lines is 0, nothing to infer : fall back to the default
191202
if ($inferenceEngine->linesCounted() === 0) {
@@ -527,6 +538,11 @@ public function getContiguous(): bool
527538
return $this->contiguous;
528539
}
529540

541+
/**
542+
* Php9 intends to drop support for this parameter in fgetcsv.
543+
* Not yet ready to mark deprecated in order to give users
544+
* a migration path.
545+
*/
530546
public function setEscapeCharacter(string $escapeCharacter): self
531547
{
532548
$this->escapeCharacter = $escapeCharacter;
@@ -536,7 +552,7 @@ public function setEscapeCharacter(string $escapeCharacter): self
536552

537553
public function getEscapeCharacter(): string
538554
{
539-
return $this->escapeCharacter;
555+
return $this->escapeCharacter ?? self::$defaultEscapeCharacter;
540556
}
541557

542558
/**
@@ -664,8 +680,9 @@ private static function getCsv(
664680
?int $length = null,
665681
string $separator = ',',
666682
string $enclosure = '"',
667-
string $escape = '\\'
683+
?string $escape = null
668684
): array|false {
685+
$escape = $escape ?? self::$defaultEscapeCharacter;
669686
if (PHP_VERSION_ID >= 80400 && $escape !== '') {
670687
return @fgetcsv($stream, $length, $separator, $enclosure, $escape);
671688
}

0 commit comments

Comments
 (0)