Skip to content

Commit 9a66ba2

Browse files
authored
way easier method to support field enclosure (#21)
* way easier method to support field enclosure * added release version
1 parent 044470a commit 9a66ba2

File tree

6 files changed

+8
-129
lines changed

6 files changed

+8
-129
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
/.idea/laravel-idea.xml
55
/.idea/phpunit.xml
66
/.idea/workspace.xml
7+
/.idea/laravel-idea-personal.xml
8+
/.idea/GitLink.xml

.idea/laravel-flatfiles.iml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php.xml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## v4.1.0
10+
### Changed
11+
- Way easier method to support field enclosure
12+
913
## v4.0.2
1014
### Added
1115
- Support for deleting export file after download

src/FlatfileExport.php

+2-27
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
use Illuminate\Support\Facades\Storage;
1010
use Illuminate\Support\Str;
1111
use League\Csv\CannotInsertRecord;
12+
use League\Csv\EncloseField;
1213
use League\Csv\Writer;
1314
use League\Flysystem\Adapter\Local;
14-
use RealMediaTechnicStaudacher\LaravelFlatfiles\StreamFilters\RemoveSequence;
1515
use RuntimeException;
1616
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1717
use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -220,7 +220,7 @@ private function determineDefaultWriter(): FlatfileExport
220220
$this->writer->setEnclosure($this->configuration->get('csv', 'enclosure'));
221221

222222
if ($this->configuration->get('csv', 'force_enclosure')) {
223-
$this->addForceEnclosure();
223+
EncloseField::addTo($this->writer, "\t\x1f");
224224
}
225225

226226
$this->writer->setOutputBOM($this->configuration->get('csv', 'bom') ? Writer::BOM_UTF8 : '');
@@ -306,31 +306,6 @@ private function toArrayWithoutSnakeCasedKeys($model): array
306306
return $dataAsArray;
307307
}
308308

309-
/**
310-
* adding an StreamFilter to force the enclosure of each cell.
311-
*/
312-
private function addForceEnclosure(): void
313-
{
314-
$sequence = "\t\x1f";
315-
$addSequence = static function (array $row) use ($sequence) {
316-
$res = [];
317-
foreach ($row as $value) {
318-
if (is_array($value)) {
319-
throw new RuntimeException('Cell value must not be an array: '.json_encode($row));
320-
}
321-
322-
$res[] = $sequence.$value;
323-
}
324-
325-
return $res;
326-
};
327-
328-
$this->writer->addFormatter($addSequence);
329-
RemoveSequence::registerStreamFilter();
330-
/** @noinspection PhpUnhandledExceptionInspection */
331-
$this->writer->addStreamFilter(RemoveSequence::createFilterName($this->writer, $sequence));
332-
}
333-
334309
/**
335310
* @param string|null $filename
336311
* @param array $headers

src/StreamFilters/RemoveSequence.php

-100
This file was deleted.

0 commit comments

Comments
 (0)