From 15fb9205db8ff78e9e665b47c02f087639d8fa48 Mon Sep 17 00:00:00 2001 From: ilbee Date: Thu, 11 Jan 2024 14:45:10 +0100 Subject: [PATCH 1/2] Update composer.json Make this bundle compatible with symfony 7 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 569091c..5e84aee 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ }, "require": { "php": ">=7.4 <9", - "symfony/http-foundation": "^4 || ^5 || ^6" + "symfony/http-foundation": "^4 || ^5 || ^6 || ^7" }, "require-dev": { "phpunit/phpunit": "^9.6", From cfaf67a01c15fe10372c1e8ec185502f60f79852 Mon Sep 17 00:00:00 2001 From: Julien PRIGENT Date: Mon, 5 Feb 2024 12:35:22 +0100 Subject: [PATCH 2/2] Fix phpunit --- src/CSVResponse.php | 17 ++++++++++------- tests/CSVResponseTest.php | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/CSVResponse.php b/src/CSVResponse.php index d5bd701..aa5f37d 100644 --- a/src/CSVResponse.php +++ b/src/CSVResponse.php @@ -6,7 +6,7 @@ class CSVResponse extends Response { - private string $fileName = 'CSVExport'; + private string $fileName = 'CSVExport.csv'; private ?string $separator = null; public const COMMA = ','; @@ -18,7 +18,7 @@ public function __construct(array $data, ?string $fileName = null, ?string $sepa $this->separator = $separator; if ($fileName) { - $this->fileName = $fileName; + $this->setFileName($fileName); } $this->setContent($this->initContent($data)); @@ -26,7 +26,12 @@ public function __construct(array $data, ?string $fileName = null, ?string $sepa $this->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $this->fileName)); } - private function initContent($data): string + private function setFileName(string $fileName): void + { + $this->fileName = $fileName; + } + + private function initContent(array $data): string { $fp = fopen('php://temp', 'w'); foreach ($this->prepareData($data) as $fields) { @@ -55,10 +60,8 @@ private function prepareData(array $data): array $line = []; foreach ($row as $key => $value) { - if (is_object($value)) { - if (get_class($value) == 'DateTime') { - $value = $value->format('Y-m-d H:i:s'); - } + if (is_object($value) && get_class($value) == 'DateTime') { + $value = $value->format('Y-m-d H:i:s'); } $line[] = $value; } diff --git a/tests/CSVResponseTest.php b/tests/CSVResponseTest.php index d796bef..473541f 100644 --- a/tests/CSVResponseTest.php +++ b/tests/CSVResponseTest.php @@ -34,7 +34,7 @@ public function testResponse(): void $response->getContent() ); // Use a comma to separate values. - $response = new CSVResponse($this->getData(), 'my-file-name', CSVResponse::COMMA); + $response = new CSVResponse($this->getData(), 'my-file-name.csv', CSVResponse::COMMA); $this->assertSame( "firstName,lastName\nMarcel,TOTO\nMaurice,TATA\n", $response->getContent()