Handle Case Where Both Format and Value Contain Quotation Mark

This commit is contained in:
oleibman
2024-11-26 15:20:32 -08:00
parent 3eee74950f
commit 4b568b47b4
2 changed files with 35 additions and 6 deletions
@@ -14,6 +14,7 @@ class Formatter extends BaseFormatter
* Matches any @ symbol that isn't enclosed in quotes.
*/
private const SYMBOL_AT = '/@(?=(?:[^"]*"[^"]*")*[^"]*\Z)/miu';
private const QUOTE_REPLACEMENT = "\u{fffe}"; // invalid Unicode character
/**
* Matches any ; symbol that isn't enclosed in quotes, for a "section" split.
@@ -130,9 +131,17 @@ class Formatter extends BaseFormatter
return str_replace('@', $value, $format);
}
//escape any dollar signs on the string, so they are not replaced with an empty value
$value = str_replace('$', '\\$', (string) $value);
$value = str_replace(
['$', '"'],
['\\$', self::QUOTE_REPLACEMENT],
(string) $value
);
return str_replace('"', '', preg_replace(self::SYMBOL_AT, $value, $format) ?? $value);
return str_replace(
['"', self::QUOTE_REPLACEMENT],
['', '"'],
preg_replace(self::SYMBOL_AT, $value, $format) ?? $value
);
}
// If we have a text value, return it "as is"
+24 -4
View File
@@ -1682,24 +1682,44 @@ return [
'#,##0.00;;"---"',
],
'issue 4124' => ['1 HUF', 1, '#,##0_-[$HUF]'],
'issue 4242-0' => [
'issue 4242 General with dollar sign' => [
'General $200 - 200', // expected result
'General $200 - 200', // cell contents
NumberFormat::FORMAT_GENERAL, // cell style
],
'issue 4242-1' => [
'issue 4242 Text with dollar sign' => [
'Text $200 - 200',
'Text $200 - 200',
NumberFormat::FORMAT_TEXT,
],
'issue 4242-2' => [
'issue 4242 Text with quotes, format without' => [
'"Hello" she said and "Hello" I replied',
'"Hello" she said and "Hello" I replied',
NumberFormat::FORMAT_TEXT,
],
'issue 4242-3' => [
'issue 4242 Format with quotes, text without' => [
'Text: $200 - 200',
'$200 - 200',
'"Text: "' . NumberFormat::FORMAT_TEXT,
],
'issue 4242 single quote mark' => [
'"',
'"',
'@',
],
'issue 4242 dollar sign' => [
'$100',
'$100',
'@',
],
'issue 4242 repeat unquoted at signs' => [
'xy xy xy',
'xy',
'@ @ @',
],
'issue 4242 quotes in format and text' => [
'Text: "Hooray" for me',
'"Hooray" for me',
'"Text: "' . NumberFormat::FORMAT_TEXT,
],
];