Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ composer require bayareawebpro/laravel-simple-csv
## Usage:

```php
<?php
use BayAreaWebPro\SimpleCsv\SimpleCsv;
$lazyCollection = SimpleCsv::import(storage_path('collection.csv'));

$lazyCsvCollection = SimpleCsv::import(storage_path('collection.csv'));
```

### Empty Keys to Null

PHP's 'putcsv' doesn't support writing `null` to csv files. The LazyCsvCollection returned by the import method
exposes a lazy 'emptyToNull' method that will convert empty array keys to null values for convenience.

```php
use BayAreaWebPro\SimpleCsv\SimpleCsv;

SimpleCsv::import(storage_path('collection.csv'))->emptyToNull();
```

### Export to File
```php
<?php
use BayAreaWebPro\SimpleCsv\SimpleCsv;

// Collection
Expand Down Expand Up @@ -62,28 +72,31 @@ SimpleCsv::export(
### Export Download Stream

```php
<?php
use BayAreaWebPro\SimpleCsv\SimpleCsv;

return SimpleCsv::download([...], 'download.csv');
```

#### Override Options
```php
<?php
use Illuminate\Support\Facades\Config;
Config::set('simple-csv.delimiter', ...);
Config::set('simple-csv.enclosure', ...);
Config::set('simple-csv.escape', ...);

Config::set('simple-csv', [
'delimiter' => '?',
'enclosure' => '?',
'escape' => '?',
]);
```

## Or, Create a Config File

`config/simple-csv.php`

```php
<?php
//config/simple-csv.php
return [
'delimiter' => "???",
'enclosure' => "???",
'escape' => "???",
'delimiter' => '?',
'enclosure' => '?',
'escape' => '?',
];
```

Expand Down
26 changes: 26 additions & 0 deletions src/LazyCsvCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php namespace BayAreaWebPro\SimpleCsv;

use Illuminate\Support\Enumerable;
use Illuminate\Support\LazyCollection;

final class LazyCsvCollection extends LazyCollection
{
/**
* Convert empty item array keys to null.
*/
public function emptyToNull()
{
return new static(function () {
foreach ($this as $item){
if(is_array($item) || $item instanceof Enumerable){
foreach ($item as $key => $value){
if(empty($value)){
$item[$key] = null;
}
}
}
yield $item;
}
});
}
}
7 changes: 4 additions & 3 deletions src/SimpleCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
5 changes: 2 additions & 3 deletions src/SimpleCsvService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace BayAreaWebPro\SimpleCsv;

use \Iterator;
use \SplFileObject;
use \Exception;
use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 21 additions & 2 deletions tests/Unit/DefaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BayAreaWebPro\SimpleCsv\Tests\Unit;

use BayAreaWebPro\SimpleCsv\LazyCsvCollection;
use Symfony\Component\HttpFoundation\StreamedResponse;

use Illuminate\Support\LazyCollection;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
Expand Down