From 2a8fb60fbbd6f6dafc62a1336c169408cd3bbd49 Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Sat, 7 Sep 2024 07:35:04 -0700 Subject: [PATCH] Some Long-Term Prep Enable us to determine if user has explicitly changed escape character vs. using default. --- src/PhpSpreadsheet/Reader/Csv.php | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/PhpSpreadsheet/Reader/Csv.php b/src/PhpSpreadsheet/Reader/Csv.php index 5f188ad94c..762ec31910 100644 --- a/src/PhpSpreadsheet/Reader/Csv.php +++ b/src/PhpSpreadsheet/Reader/Csv.php @@ -62,8 +62,19 @@ class Csv extends BaseReader /** * The character that can escape the enclosure. + * This will probably become unsupported in Php 9. + * Not yet ready to mark deprecated in order to give users + * a migration path. */ - private string $escapeCharacter = '\\'; + private ?string $escapeCharacter = null; + + /** + * The character that will be supplied to fgetcsv + * when escapeCharacter is null. + * It is anticipated that it will conditionally be set + * to null-string for Php9 and above. + */ + private static string $defaultEscapeCharacter = '\\'; /** * Callback for setting defaults in construction. @@ -185,7 +196,7 @@ protected function inferSeparator(): void return; } - $inferenceEngine = new Delimiter($this->fileHandle, $this->escapeCharacter, $this->enclosure); + $inferenceEngine = new Delimiter($this->fileHandle, $this->escapeCharacter ?? self::$defaultEscapeCharacter, $this->enclosure); // If number of lines is 0, nothing to infer : fall back to the default if ($inferenceEngine->linesCounted() === 0) { @@ -527,6 +538,11 @@ public function getContiguous(): bool return $this->contiguous; } + /** + * Php9 intends to drop support for this parameter in fgetcsv. + * Not yet ready to mark deprecated in order to give users + * a migration path. + */ public function setEscapeCharacter(string $escapeCharacter): self { $this->escapeCharacter = $escapeCharacter; @@ -536,7 +552,7 @@ public function setEscapeCharacter(string $escapeCharacter): self public function getEscapeCharacter(): string { - return $this->escapeCharacter; + return $this->escapeCharacter ?? self::$defaultEscapeCharacter; } /** @@ -664,8 +680,9 @@ private static function getCsv( ?int $length = null, string $separator = ',', string $enclosure = '"', - string $escape = '\\' + ?string $escape = null ): array|false { + $escape = $escape ?? self::$defaultEscapeCharacter; if (PHP_VERSION_ID >= 80400 && $escape !== '') { return @fgetcsv($stream, $length, $separator, $enclosure, $escape); }