Skip to content

Commit e46beaf

Browse files
Merge pull request #16 from bayareawebpro/dev
Add LazyCsvCollection class
2 parents 5bc3b6d + 6b58876 commit e46beaf

File tree

5 files changed

+79
-21
lines changed

5 files changed

+79
-21
lines changed

README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,24 @@ composer require bayareawebpro/laravel-simple-csv
2424
## Usage:
2525

2626
```php
27-
<?php
2827
use BayAreaWebPro\SimpleCsv\SimpleCsv;
29-
$lazyCollection = SimpleCsv::import(storage_path('collection.csv'));
28+
29+
$lazyCsvCollection = SimpleCsv::import(storage_path('collection.csv'));
30+
```
31+
32+
### Empty Keys to Null
33+
34+
PHP's 'putcsv' doesn't support writing `null` to csv files. The LazyCsvCollection returned by the import method
35+
exposes a lazy 'emptyToNull' method that will convert empty array keys to null values for convenience.
36+
37+
```php
38+
use BayAreaWebPro\SimpleCsv\SimpleCsv;
39+
40+
SimpleCsv::import(storage_path('collection.csv'))->emptyToNull();
3041
```
3142

3243
### Export to File
3344
```php
34-
<?php
3545
use BayAreaWebPro\SimpleCsv\SimpleCsv;
3646

3747
// Collection
@@ -62,28 +72,31 @@ SimpleCsv::export(
6272
### Export Download Stream
6373

6474
```php
65-
<?php
6675
use BayAreaWebPro\SimpleCsv\SimpleCsv;
76+
6777
return SimpleCsv::download([...], 'download.csv');
6878
```
6979

7080
#### Override Options
7181
```php
72-
<?php
7382
use Illuminate\Support\Facades\Config;
74-
Config::set('simple-csv.delimiter', ...);
75-
Config::set('simple-csv.enclosure', ...);
76-
Config::set('simple-csv.escape', ...);
83+
84+
Config::set('simple-csv', [
85+
'delimiter' => '?',
86+
'enclosure' => '?',
87+
'escape' => '?',
88+
]);
7789
```
7890

7991
## Or, Create a Config File
92+
93+
`config/simple-csv.php`
94+
8095
```php
81-
<?php
82-
//config/simple-csv.php
8396
return [
84-
'delimiter' => "???",
85-
'enclosure' => "???",
86-
'escape' => "???",
97+
'delimiter' => '?',
98+
'enclosure' => '?',
99+
'escape' => '?',
87100
];
88101
```
89102

src/LazyCsvCollection.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php namespace BayAreaWebPro\SimpleCsv;
2+
3+
use Illuminate\Support\Enumerable;
4+
use Illuminate\Support\LazyCollection;
5+
6+
final class LazyCsvCollection extends LazyCollection
7+
{
8+
/**
9+
* Convert empty item array keys to null.
10+
*/
11+
public function emptyToNull()
12+
{
13+
return new static(function () {
14+
foreach ($this as $item){
15+
if(is_array($item) || $item instanceof Enumerable){
16+
foreach ($item as $key => $value){
17+
if(empty($value)){
18+
$item[$key] = null;
19+
}
20+
}
21+
}
22+
yield $item;
23+
}
24+
});
25+
}
26+
}

src/SimpleCsv.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
use Illuminate\Support\Collection;
44
use Illuminate\Support\LazyCollection;
55
use Illuminate\Support\Facades\Facade as LaravelFacade;
6+
use Iterator;
67

78
/**
89
* The SimpleCsv Service Facade
9-
* @method static \Symfony\Component\HttpFoundation\StreamedResponse download(Collection|LazyCollection|\Iterator|array $collection, string $filename)
10-
* @method static void export(Collection|LazyCollection|\Iterator|array $collection, string $path)
11-
* @method static LazyCollection import(string $path)
10+
* @method static \Symfony\Component\HttpFoundation\StreamedResponse download(Collection|LazyCollection|Iterator|array $collection, string $filename)
11+
* @method static void export(Collection|LazyCollection|Iterator|array $collection, string $path)
12+
* @method static LazyCsvCollection import(string $path)
1213
*/
1314
class SimpleCsv extends LaravelFacade
1415
{

src/SimpleCsvService.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace BayAreaWebPro\SimpleCsv;
44

55
use \Iterator;
6-
use \SplFileObject;
76
use \Exception;
87
use Illuminate\Support\Collection;
98
use Illuminate\Support\LazyCollection;
@@ -32,11 +31,11 @@ public function __construct(
3231
$this->file = null;
3332
}
3433

35-
public function import(string $path): LazyCollection
34+
public function import(string $path): LazyCsvCollection
3635
{
3736
$this->openFileObject($path);
3837
$this->headers = array_values($this->getLine());
39-
return LazyCollection::make(function () {
38+
return LazyCsvCollection::make(function () {
4039
while ($this->file->valid() && $line = $this->getLine()) {
4140
if (!$this->isInValidLine($line)) {
4241
yield array_combine($this->headers, $line);

tests/Unit/DefaultTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BayAreaWebPro\SimpleCsv\Tests\Unit;
44

5+
use BayAreaWebPro\SimpleCsv\LazyCsvCollection;
56
use Symfony\Component\HttpFoundation\StreamedResponse;
67

78
use Illuminate\Support\LazyCollection;
@@ -34,6 +35,23 @@ private function getRandomStoragePath()
3435
return storage_path(Str::random(16) . '.csv');
3536
}
3637

38+
public function test_imported_lazy_collection_methods()
39+
{
40+
$collection = new LazyCsvCollection(LazyGenerator::make(5, function () {
41+
return ['null_field_exported_empty' => ''];
42+
}));
43+
foreach($collection as $row){
44+
$this->assertNotNull($row['null_field_exported_empty']);
45+
}
46+
47+
$collection = new LazyCsvCollection(LazyGenerator::make(5, function () {
48+
return ['null_field_exported_empty' => ''];
49+
}));
50+
foreach($collection->emptyToNull() as $row){
51+
$this->assertNull($row['null_field_exported_empty']);
52+
}
53+
}
54+
3755
public function test_export_from_iterables()
3856
{
3957
$items = $this->getCollectionData(10)->toArray();
@@ -75,7 +93,8 @@ public function test_export_files_and_restore()
7593
}
7694

7795
$decoded = SimpleCsv::import($path);
78-
$this->assertTrue($decoded instanceof LazyCollection);
96+
$this->assertInstanceOf(LazyCollection::class, $decoded);
97+
7998
foreach ($decoded as $decodedItem) {
8099
$this->assertStringContainsString($decodedItem['email'], $fileData);
81100
}
@@ -89,7 +108,7 @@ public function test_can_download_streams()
89108

90109
$response = SimpleCsv::download($collectionLazy, 'download.csv');
91110

92-
$this->assertTrue($response instanceof StreamedResponse);
111+
$this->assertInstanceOf(StreamedResponse::class, $response);
93112

94113
//Capture Streamed Output...
95114
ob_start();

0 commit comments

Comments
 (0)