From 894d12b77493ca20f0417b8d726f61d0745e7221 Mon Sep 17 00:00:00 2001 From: Dan Alvidrez Date: Fri, 14 Apr 2023 23:28:05 -0700 Subject: [PATCH 1/2] Add LazyCsvCollection class - Add emptyToNull method --- README.md | 39 +++++++++++++++++++++++++------------- src/LazyCsvCollection.php | 26 +++++++++++++++++++++++++ src/SimpleCsv.php | 7 ++++--- src/SimpleCsvService.php | 5 ++--- tests/Unit/DefaultTest.php | 23 ++++++++++++++++++++-- 5 files changed, 79 insertions(+), 21 deletions(-) create mode 100644 src/LazyCsvCollection.php diff --git a/README.md b/README.md index cc1ec29..47ba16d 100644 --- a/README.md +++ b/README.md @@ -24,14 +24,24 @@ composer require bayareawebpro/laravel-simple-csv ## Usage: ```php -emptyToNull(); ``` ### Export to File ```php - '?', + 'enclosure' => '?', + 'escape' => '?', +]); ``` ## Or, Create a Config File + +`config/simple-csv.php` + ```php - "???", - 'enclosure' => "???", - 'escape' => "???", + 'delimiter' => '?', + 'enclosure' => '?', + 'escape' => '?', ]; ``` diff --git a/src/LazyCsvCollection.php b/src/LazyCsvCollection.php new file mode 100644 index 0000000..6b01ec6 --- /dev/null +++ b/src/LazyCsvCollection.php @@ -0,0 +1,26 @@ + $value){ + if(empty($value)){ + $item[$key] = null; + } + } + } + yield $item; + } + }); + } +} \ No newline at end of file diff --git a/src/SimpleCsv.php b/src/SimpleCsv.php index 1051fb3..a19465b 100644 --- a/src/SimpleCsv.php +++ b/src/SimpleCsv.php @@ -3,12 +3,13 @@ use Illuminate\Support\Collection; use Illuminate\Support\LazyCollection; use Illuminate\Support\Facades\Facade as LaravelFacade; +use Iterator; /** * The SimpleCsv Service Facade - * @method static \Symfony\Component\HttpFoundation\StreamedResponse download(Collection|LazyCollection|\Iterator|array $collection, string $filename) - * @method static void export(Collection|LazyCollection|\Iterator|array $collection, string $path) - * @method static LazyCollection import(string $path) + * @method static \Symfony\Component\HttpFoundation\StreamedResponse download(Collection|LazyCollection|Iterator|array $collection, string $filename) + * @method static void export(Collection|LazyCollection|Iterator|array $collection, string $path) + * @method static LazyCsvCollection import(string $path) */ class SimpleCsv extends LaravelFacade { diff --git a/src/SimpleCsvService.php b/src/SimpleCsvService.php index 819b669..7b58d4c 100644 --- a/src/SimpleCsvService.php +++ b/src/SimpleCsvService.php @@ -3,7 +3,6 @@ namespace BayAreaWebPro\SimpleCsv; use \Iterator; -use \SplFileObject; use \Exception; use Illuminate\Support\Collection; use Illuminate\Support\LazyCollection; @@ -32,11 +31,11 @@ public function __construct( $this->file = null; } - public function import(string $path): LazyCollection + public function import(string $path): LazyCsvCollection { $this->openFileObject($path); $this->headers = array_values($this->getLine()); - return LazyCollection::make(function () { + return LazyCsvCollection::make(function () { while ($this->file->valid() && $line = $this->getLine()) { if (!$this->isInValidLine($line)) { yield array_combine($this->headers, $line); diff --git a/tests/Unit/DefaultTest.php b/tests/Unit/DefaultTest.php index 07933ea..5014d6d 100644 --- a/tests/Unit/DefaultTest.php +++ b/tests/Unit/DefaultTest.php @@ -2,6 +2,7 @@ namespace BayAreaWebPro\SimpleCsv\Tests\Unit; +use BayAreaWebPro\SimpleCsv\LazyCsvCollection; use Symfony\Component\HttpFoundation\StreamedResponse; use Illuminate\Support\LazyCollection; @@ -34,6 +35,23 @@ private function getRandomStoragePath() return storage_path(Str::random(16) . '.csv'); } + public function test_imported_lazy_collection_methods() + { + $collection = new LazyCsvCollection(LazyGenerator::make(5, function () { + return ['null_field_exported_empty' => '']; + })); + foreach($collection as $row){ + $this->assertNotNull($row['null_field_exported_empty']); + } + + $collection = new LazyCsvCollection(LazyGenerator::make(5, function () { + return ['null_field_exported_empty' => '']; + })); + foreach($collection->emptyToNull() as $row){ + $this->assertNull($row['null_field_exported_empty']); + } + } + public function test_export_from_iterables() { $items = $this->getCollectionData(10)->toArray(); @@ -75,7 +93,8 @@ public function test_export_files_and_restore() } $decoded = SimpleCsv::import($path); - $this->assertTrue($decoded instanceof LazyCollection); + $this->assertInstanceOf(LazyCollection::class, $decoded); + foreach ($decoded as $decodedItem) { $this->assertStringContainsString($decodedItem['email'], $fileData); } @@ -89,7 +108,7 @@ public function test_can_download_streams() $response = SimpleCsv::download($collectionLazy, 'download.csv'); - $this->assertTrue($response instanceof StreamedResponse); + $this->assertInstanceOf(StreamedResponse::class, $response); //Capture Streamed Output... ob_start(); From 6b58876e8dfa3d0b067b8e5633b9bcf61ed2db61 Mon Sep 17 00:00:00 2001 From: Dan Alvidrez Date: Fri, 14 Apr 2023 23:30:53 -0700 Subject: [PATCH 2/2] Update LazyCsvCollection.php --- src/LazyCsvCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LazyCsvCollection.php b/src/LazyCsvCollection.php index 6b01ec6..6276162 100644 --- a/src/LazyCsvCollection.php +++ b/src/LazyCsvCollection.php @@ -3,7 +3,7 @@ use Illuminate\Support\Enumerable; use Illuminate\Support\LazyCollection; -class LazyCsvCollection extends LazyCollection +final class LazyCsvCollection extends LazyCollection { /** * Convert empty item array keys to null.