From d624e1b7d1f7cc44d37d7fd67456b420a5ccc177 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 12 Dec 2024 15:04:22 +1300 Subject: [PATCH] ENH Use updated league/csv api --- src/Forms/GridField/GridFieldExportButton.php | 11 ++++------- .../GridField/GridFieldExportButtonTest.php | 19 ++++++++++--------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/Forms/GridField/GridFieldExportButton.php b/src/Forms/GridField/GridFieldExportButton.php index 6b543b8c49e..3f77b649236 100644 --- a/src/Forms/GridField/GridFieldExportButton.php +++ b/src/Forms/GridField/GridFieldExportButton.php @@ -2,6 +2,7 @@ namespace SilverStripe\Forms\GridField; +use League\Csv\Bom; use League\Csv\Writer; use LogicException; use SilverStripe\Control\HTTPRequest; @@ -180,8 +181,8 @@ public function generateExportFileData($gridField) $csvWriter = Writer::createFromFileObject(new \SplTempFileObject()); $csvWriter->setDelimiter($this->getCsvSeparator()); $csvWriter->setEnclosure($this->getCsvEnclosure()); - $csvWriter->setNewline("\r\n"); //use windows line endings for compatibility with some csv libraries - $csvWriter->setOutputBOM(Writer::BOM_UTF8); + $csvWriter->setEndOfLine("\r\n"); //use windows line endings for compatibility with some csv libraries + $csvWriter->setOutputBOM(Bom::Utf8); if (!Config::inst()->get(get_class($this), 'xls_export_disabled')) { $csvWriter->addFormatter(function (array $row) { @@ -269,11 +270,7 @@ public function generateExportFileData($gridField) } } - if (method_exists($csvWriter, 'getContent')) { - return $csvWriter->getContent(); - } - - return (string)$csvWriter; + return $csvWriter->toString(); } /** diff --git a/tests/php/Forms/GridField/GridFieldExportButtonTest.php b/tests/php/Forms/GridField/GridFieldExportButtonTest.php index 9ec0d38ce48..255ba66edfe 100644 --- a/tests/php/Forms/GridField/GridFieldExportButtonTest.php +++ b/tests/php/Forms/GridField/GridFieldExportButtonTest.php @@ -2,6 +2,7 @@ namespace SilverStripe\Forms\Tests\GridField; +use League\Csv\Bom; use League\Csv\Reader; use LogicException; use ReflectionMethod; @@ -64,7 +65,7 @@ public function testCanView() $this->assertEquals( "$bom\"My Name\"\r\n", - (string) $csvReader + (string) $csvReader->toString() ); } @@ -78,7 +79,7 @@ public function testGenerateFileDataBasicFields() $this->assertEquals( $bom . '"My Name"' . "\r\n" . 'Test' . "\r\n" . 'Test2' . "\r\n", - (string) $csvReader + $csvReader->toString() ); } @@ -98,7 +99,7 @@ public function testXLSSanitisation() $this->assertEquals( "$bom\"My Name\"\r\n\"\t=SUM(1, 2)\"\r\nTest\r\nTest2\r\n", - (string) $csvReader + $csvReader->toString() ); } @@ -117,7 +118,7 @@ public function testGenerateFileDataAnonymousFunctionField() $this->assertEquals( $bom . 'Name,City' . "\r\n" . 'Test,"City city"' . "\r\n" . 'Test2,"Quoted ""City"" 2 city"' . "\r\n", - (string) $csvReader + $csvReader->toString() ); } @@ -134,7 +135,7 @@ public function testBuiltInFunctionNameCanBeUsedAsHeader() $this->assertEquals( $bom . 'Name,strtolower' . "\r\n" . 'Test,City' . "\r\n" . 'Test2,"Quoted ""City"" 2"' . "\r\n", - (string) $csvReader + $csvReader->toString() ); } @@ -152,7 +153,7 @@ public function testNoCsvHeaders() $this->assertEquals( $bom . 'Test,City' . "\r\n" . 'Test2,"Quoted ""City"" 2"' . "\r\n", - (string) $csvReader + $csvReader->toString() ); } @@ -179,7 +180,7 @@ public function testArrayListInput() $this->assertEquals( $bom . "ID\r\n" . "1\r\n" . "2\r\n" . "3\r\n" . "4\r\n" . "5\r\n" . "6\r\n" . "7\r\n" . "8\r\n" . "9\r\n" . "10\r\n" . "11\r\n" . "12\r\n" . "13\r\n" . "14\r\n" . "15\r\n" . "16\r\n", - (string) $csvReader + $csvReader->toString() ); } @@ -195,7 +196,7 @@ public function testZeroValue() $this->assertEquals( "$bom\"Rugby Team Number\"\r\n2\r\n0\r\n", - (string) $csvReader + $csvReader->toString() ); } @@ -224,7 +225,7 @@ protected function createReader($string) // Explicitly set the output BOM in league/csv 9 if (method_exists($reader, 'getContent')) { - $reader->setOutputBOM(Reader::BOM_UTF8); + $reader->setOutputBOM(Bom::Utf8); } return $reader;