forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Html Reader/Writer Better Handling of Booleans
When Html Writer outputs a cell with a boolean value, the result will either be 1 or null-string; neither of these is optimal for anyone looking at the resulting html. Html Reader already has the ability to recognize data types using the html `data-type` attribute, but Html Writer doesn't use it. This PR adds the ability to generate that attribute for booleans. It will generate a string value appropriate for the locale when it encounters a boolean. Html Reader, when it encounters `data-type="b"`, will interpret the result as true if the value is 1 or a string value recognized as true in any locale; it will interpret the result as false if the value is 0, null-string, null, or a string value recognized as false in any locale; if none of the above, it will leave the value as an unchanged string. So, Reader will wind up with the correct result even if its locale is different than what Writer used. Because this is a breaking change, it is opt-in. You need to call `Writer::setBetterBoolean(true)` in order for it take effect. The current default value for that property is false. When it is time to introduce breaking changes (see PR PHPOffice#4240), the default will be changed to true.
- Loading branch information
Showing
4 changed files
with
266 additions
and
2 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
140 changes: 140 additions & 0 deletions
140
tests/PhpSpreadsheetTests/Writer/Html/BetterBooleanTest.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,140 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html; | ||
|
||
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; | ||
use PhpOffice\PhpSpreadsheet\Reader\Html as HtmlReader; | ||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter; | ||
use PhpOffice\PhpSpreadsheetTests\Functional; | ||
|
||
class BetterBooleanTest extends Functional\AbstractFunctional | ||
{ | ||
private string $locale; | ||
|
||
protected function setUp(): void | ||
{ | ||
$calculation = Calculation::getInstance(); | ||
$this->locale = $calculation->getLocale(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$calculation = Calculation::getInstance(); | ||
$calculation->setLocale($this->locale); | ||
} | ||
|
||
public function testDefault(): void | ||
{ | ||
$spreadsheet = new Spreadsheet(); | ||
$writer = new HtmlWriter($spreadsheet); | ||
// Default will change with next PhpSpreadsheet release | ||
self::assertFalse($writer->getBetterBoolean()); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
|
||
public function setBetter(HtmlWriter $writer): void | ||
{ | ||
$writer->setBetterBoolean(true); | ||
} | ||
|
||
public function setNotBetter(HtmlWriter $writer): void | ||
{ | ||
$writer->setBetterBoolean(false); | ||
} | ||
|
||
public function testBetterBoolean(): void | ||
{ | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
$sheet->getCell('A1')->setValue(10); | ||
$sheet->getCell('B1')->setValue('Hello'); | ||
$sheet->getCell('C1')->setValue(true); | ||
$sheet->getCell('D1')->setValue('=IF(1>2, TRUE, FALSE)'); | ||
|
||
/** @var callable */ | ||
$callableWriter = [$this, 'setBetter']; | ||
$reloaded = $this->writeAndReload($spreadsheet, 'Html', null, $callableWriter); | ||
$spreadsheet->disconnectWorksheets(); | ||
|
||
$rsheet = $reloaded->getActiveSheet(); | ||
self::assertSame(10, $rsheet->getCell('A1')->getValue()); | ||
self::assertSame('Hello', $rsheet->getCell('B1')->getValue()); | ||
self::assertTrue($rsheet->getCell('C1')->getValue()); | ||
self::assertFalse($rsheet->getCell('D1')->getValue()); | ||
$reloaded->disconnectWorksheets(); | ||
} | ||
|
||
public function testNotBetterBoolean(): void | ||
{ | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
$sheet->getCell('A1')->setValue(10); | ||
$sheet->getCell('B1')->setValue('Hello'); | ||
$sheet->getCell('C1')->setValue(true); | ||
$sheet->getCell('D1')->setValue('=IF(1>2, TRUE, FALSE)'); | ||
|
||
/** @var callable */ | ||
$callableWriter = [$this, 'setNotBetter']; | ||
$reloaded = $this->writeAndReload($spreadsheet, 'Html', null, $callableWriter); | ||
$spreadsheet->disconnectWorksheets(); | ||
|
||
$rsheet = $reloaded->getActiveSheet(); | ||
self::assertSame(10, $rsheet->getCell('A1')->getValue()); | ||
self::assertSame('Hello', $rsheet->getCell('B1')->getValue()); | ||
self::assertSame(1, $rsheet->getCell('C1')->getValue()); | ||
self::assertNull($rsheet->getCell('D1')->getValue()); | ||
$reloaded->disconnectWorksheets(); | ||
} | ||
|
||
public function testLocale(): void | ||
{ | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
$sheet->getCell('A1')->setValue(10); | ||
$sheet->getCell('B1')->setValue('Hello'); | ||
$sheet->getCell('C1')->setValue(true); | ||
$sheet->getCell('D1')->setValue('=IF(1>2, TRUE, FALSE)'); | ||
$calc = Calculation::getInstance(); | ||
$calc->setLocale('fr'); | ||
$writer = new HtmlWriter($spreadsheet); | ||
$writer->setBetterBoolean(true); | ||
$html = $writer->generateHtmlAll(); | ||
self::assertStringContainsString('VRAI', $html); | ||
self::assertStringNotContainsString('TRUE', $html); | ||
|
||
/** @var callable */ | ||
$callableWriter = [$this, 'setBetter']; | ||
$reloaded = $this->writeAndReload($spreadsheet, 'Html', null, $callableWriter); | ||
$spreadsheet->disconnectWorksheets(); | ||
|
||
$rsheet = $reloaded->getActiveSheet(); | ||
self::assertSame(10, $rsheet->getCell('A1')->getValue()); | ||
self::assertSame('Hello', $rsheet->getCell('B1')->getValue()); | ||
self::assertTrue($rsheet->getCell('C1')->getValue()); | ||
self::assertFalse($rsheet->getCell('D1')->getValue()); | ||
$reloaded->disconnectWorksheets(); | ||
} | ||
|
||
public function testForeignNoLocale(): void | ||
{ | ||
$fragment = '<table><tbody><tr>' | ||
. '<td>10</td>' | ||
. '<td>Hello</td>' | ||
. '<td data-type="b">ИСТИНА</td>' // Bulgarian TRUE | ||
. '<td data-type="b">EPÄTOSI</td>' // Finnish FALSE | ||
. '<td data-type="b">whatever</td>' | ||
. '<td data-type="b">tRuE</td>' | ||
. '</tr></tbody></table>'; | ||
$reader = new HtmlReader(); | ||
$spreadsheet = $reader->loadFromString($fragment); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
self::assertTrue($sheet->getCell('C1')->getValue()); | ||
self::assertFalse($sheet->getCell('D1')->getValue()); | ||
self::assertSame('whatever', $sheet->getCell('E1')->getValue()); | ||
self::assertTrue($sheet->getCell('F1')->getValue()); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
} |