From 55797126887ce385e9b38c91e964a0de3d81bf2c Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sun, 27 Feb 2022 23:06:22 +0100 Subject: [PATCH] Make CSV Reader boolean casting (e.g. string ``"TRUE"` to boolean `TRUE`) locale-aware. --- docs/topics/accessing-cells.md | 10 +++++----- src/PhpSpreadsheet/Reader/Csv.php | 5 +++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/docs/topics/accessing-cells.md b/docs/topics/accessing-cells.md index 346e5858..9d3fb1cb 100644 --- a/docs/topics/accessing-cells.md +++ b/docs/topics/accessing-cells.md @@ -37,9 +37,7 @@ $spreadsheet->getActiveSheet() ### Creating a new Cell If you make a call to `getCell()`, and the cell doesn't already exist, then -PhpSpreadsheet will (by default) create the cell for you. If you don't want -to create a new cell, then you can pass a second argument of false, and then -`getCell()` will return a null if the cell doesn't exist. +PhpSpreadsheet will create that cell for you. ### BEWARE: Cells assigned to variables as a Detached Reference @@ -532,7 +530,7 @@ types of entered data using a cell's `setValue()` method (the Optionally, the default behaviour of PhpSpreadsheet can be modified, allowing easier data entry. For example, a `\PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder` class is available. -It automatically converts percentages, number in scientific format, and +It automatically converts percentages, numbers in scientific format, and dates entered as strings to the correct format, also setting the cell's style information. The following example demonstrates how to set the value binder in PhpSpreadsheet: @@ -577,7 +575,9 @@ $stringValueBinder->setNumericConversion(false) \PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( $stringValueBinder ); ``` -**Creating your own value binder is relatively straightforward.** When more specialised +### Creating your own value binder + +Creating your own value binder is relatively straightforward. When more specialised value binding is required, you can implement the `\PhpOffice\PhpSpreadsheet\Cell\IValueBinder` interface or extend the existing `\PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder` or diff --git a/src/PhpSpreadsheet/Reader/Csv.php b/src/PhpSpreadsheet/Reader/Csv.php index 0146fca0..f367e0b5 100644 --- a/src/PhpSpreadsheet/Reader/Csv.php +++ b/src/PhpSpreadsheet/Reader/Csv.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheet\Reader; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Cell\Cell; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Reader\Csv\Delimiter; @@ -353,9 +354,9 @@ class Csv extends BaseReader private function convertBoolean(&$rowDatum, bool $preserveBooleanString): void { if (is_string($rowDatum) && !$preserveBooleanString) { - if (strcasecmp('true', $rowDatum) === 0) { + if (strcasecmp(Calculation::getTRUE(), $rowDatum) === 0 || strcasecmp('true', $rowDatum) === 0) { $rowDatum = true; - } elseif (strcasecmp('false', $rowDatum) === 0) { + } elseif (strcasecmp(Calculation::getFALSE(), $rowDatum) === 0 || strcasecmp('false', $rowDatum) === 0) { $rowDatum = false; } } elseif ($rowDatum === null) {