From 734b412aa6556548f39bcb6523761050627924fa Mon Sep 17 00:00:00 2001 From: holywise Date: Mon, 5 Nov 2018 00:32:53 +0900 Subject: [PATCH 1/2] fix bom appears in all rows if _bom is enabled. #101 --- src/View/CsvView.php | 11 +++++- tests/TestCase/View/CsvViewTest.php | 52 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/View/CsvView.php b/src/View/CsvView.php index 225008f..ee74760 100644 --- a/src/View/CsvView.php +++ b/src/View/CsvView.php @@ -106,6 +106,13 @@ class CsvView extends View */ protected $bomMap; + /** + * BOM first appearance + * + * @var boolean + */ + protected $isFirstBom; + /** * List of special view vars. * @@ -153,6 +160,7 @@ public function __construct( parent::__construct($request, $response, $eventManager, $viewOptions); $this->response = $this->response->withType('csv'); + $this->isFirstBom = true; } /** @@ -456,8 +464,9 @@ protected function _generateRow($row = null) } //bom must be added after encoding - if ($this->viewVars['_bom']) { + if ($this->viewVars['_bom'] && $this->isFirstBom) { $csv = $this->getBom($this->viewVars['_csvEncoding']) . $csv; + $this->isFirstBom = false; } return $csv; diff --git a/tests/TestCase/View/CsvViewTest.php b/tests/TestCase/View/CsvViewTest.php index 62ecca2..3764cca 100644 --- a/tests/TestCase/View/CsvViewTest.php +++ b/tests/TestCase/View/CsvViewTest.php @@ -62,6 +62,58 @@ public function testBom() $this->assertSame($expected, $output); } + /** + * Test BOM appears only in the first row. + * + * @return void + */ + public function testBomMultipleContentRows() + { + if (!extension_loaded('mbstring')) { + $this->markTestSkipped( + 'The mbstring extension is not available.' + ); + } + + $data = [ + ['test'], + ['test2'], + ['test3'], + ]; + $this->view->set(['data' => $data, '_serialize' => 'data', '_bom' => true, '_csvEncoding' => 'UTF-8']); + $output = $this->view->render(false); + + $bom = chr(0xEF) . chr(0xBB) . chr(0xBF); + $expected = $bom . 'test' . PHP_EOL . 'test2' . PHP_EOL . 'test3' . PHP_EOL; + $this->assertSame($expected, $output); + } + + /** + * Test BOM appears only in the first row even it has a header. + * + * @return void + */ + public function testBomMultipleContentRowsWithHeader() + { + if (!extension_loaded('mbstring')) { + $this->markTestSkipped( + 'The mbstring extension is not available.' + ); + } + + $header = ['column1']; + $data = [ + ['test'], + ['test2'], + ]; + $this->view->set(['data' => $data, '_header' => $header, '_serialize' => 'data', '_bom' => true, '_csvEncoding' => 'UTF-8']); + $output = $this->view->render(false); + + $bom = chr(0xEF) . chr(0xBB) . chr(0xBF); + $expected = $bom . 'column1' . PHP_EOL . 'test' . PHP_EOL . 'test2' . PHP_EOL; + $this->assertSame($expected, $output); + } + /** * Test render with an array in _serialize * From b4cff013d11d83dcf4c86ad39bcb1ba694611def Mon Sep 17 00:00:00 2001 From: holywise Date: Mon, 5 Nov 2018 03:12:38 +0900 Subject: [PATCH 2/2] fix phpdoc comment. --- src/View/CsvView.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/View/CsvView.php b/src/View/CsvView.php index ee74760..032b559 100644 --- a/src/View/CsvView.php +++ b/src/View/CsvView.php @@ -109,7 +109,7 @@ class CsvView extends View /** * BOM first appearance * - * @var boolean + * @var bool */ protected $isFirstBom;