From 0c3bc425e1241484944ea3e16b7fb23212166aef Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 12 Dec 2024 14:47:40 +1300 Subject: [PATCH] ENH Use updated league/csv api --- src/Forms/GridField/GridFieldExportButton.php | 11 ++++------- .../php/Forms/GridField/GridFieldExportButtonTest.php | 5 +++-- 2 files changed, 7 insertions(+), 9 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..c3abd9fada9 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() ); } @@ -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;