Some Long-Term Prep

Enable us to determine if user has explicitly changed escape character vs. using default.
This commit is contained in:
oleibman
2024-09-07 07:35:04 -07:00
parent 352eaae3ee
commit 2a8fb60fbb
+21 -4
View File
@@ -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 @@ class Csv extends BaseReader
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 @@ class Csv extends BaseReader
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 @@ class Csv extends BaseReader
public function getEscapeCharacter(): string
{
return $this->escapeCharacter;
return $this->escapeCharacter ?? self::$defaultEscapeCharacter;
}
/**
@@ -664,8 +680,9 @@ class Csv extends BaseReader
?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);
}