-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Csv Handling of Booleans (and an 8.1 Deprecation) (#2232)
* Csv Handling of Booleans (and an 8.1 Deprecation) PhpSpreadsheet writes boolean values to a Csv as null-string/1, and treats input values of 'true' and 'false' as if they were strings. On the other hand, Excel writes boolean values to a Csv as TRUE/FALSE, and case-insensitively treats a matching string as boolean on read. This PR changes PhpSpreadsheet to match Excel. A side-effect of this change is that it fixes behavior incorrectly reported as a bug in PR #2048. That issue was closed, correctly, as user error. The user had altered Csv Writer, including adding ```declare(strict_types=1);```; that declaration was the cause of the error. The "offending" statements, calls to strpbrk and str_replace, will now work correctly whether or not strict_types is in use. And, just as I was getting ready to push this, the dailies for PHP 8.1 introduced a change deprecating auto_detect_line_endings. Csv Reader uses that setting; it allows it to process a Csv with Mac line endings, which happens to be something that Excel can do. As they say in https://wiki.php.net/rfc/deprecations_php_8_1, where the proposal passed without a single dissenting vote, "These newlines were used by “Classic” Mac OS, a system which has been discontinued in 2001, nearly two decades ago. Interoperability with such systems is no longer relevant." I tend to agree, but I don't know that we're ready to pull the plug yet. I don't see an easy way to emulate that functionality. For now, I have silenced the deprecation notices with at signs. I have also added a test case which will fail when support for that setting is pulled; this will give time to consider alternatives. * Scrutinizer: Handling ini_set This could be interesting. It doesn't like not handling an error condition for ini_set. Let's see if this satisfies it.
- Loading branch information
Showing
5 changed files
with
195 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
tests/PhpSpreadsheetTests/Reader/Csv/CsvLineEndingTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Reader\Csv; | ||
|
||
use PhpOffice\PhpSpreadsheet\Reader\Csv; | ||
use PhpOffice\PhpSpreadsheet\Shared\File; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CsvLineEndingTest extends TestCase | ||
{ | ||
/** @var string */ | ||
private $tempFile = ''; | ||
|
||
protected function tearDown(): void | ||
{ | ||
if ($this->tempFile !== '') { | ||
unlink($this->tempFile); | ||
$this->tempFile = ''; | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider providerEndings | ||
*/ | ||
public function testEndings(string $ending): void | ||
{ | ||
$this->tempFile = $filename = File::temporaryFilename(); | ||
$data = ['123', '456', '789']; | ||
file_put_contents($filename, implode($ending, $data)); | ||
$reader = new Csv(); | ||
$spreadsheet = $reader->load($filename); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
self::assertEquals($data[0], $sheet->getCell('A1')->getValue()); | ||
self::assertEquals($data[1], $sheet->getCell('A2')->getValue()); | ||
self::assertEquals($data[2], $sheet->getCell('A3')->getValue()); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
|
||
public function providerEndings(): array | ||
{ | ||
return [ | ||
'Unix endings' => ["\n"], | ||
'Mac endings' => ["\r"], | ||
'Windows endings' => ["\r\n"], | ||
]; | ||
} | ||
} |
Oops, something went wrong.