mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-26 05:48:36 +00:00
0cd20f3099
* 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.
48 lines
1.3 KiB
PHP
48 lines
1.3 KiB
PHP
<?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"],
|
|
];
|
|
}
|
|
}
|