Skip to content

Commit 625e741

Browse files
Merge pull request #13 from bayareawebpro/dev
Update SimpleCsvService.php
2 parents eada019 + fbef21f commit 625e741

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

src/SimpleCsvService.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace BayAreaWebPro\SimpleCsv;
44

5-
use Iterator;
6-
use SplFileObject;
5+
use \Iterator;
6+
use \SplFileObject;
7+
use \Exception;
78
use Illuminate\Support\Collection;
89
use Illuminate\Support\LazyCollection;
910
use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -14,19 +15,8 @@ class SimpleCsvService
1415
const ENCLOSURE = '"';
1516
const ESCAPE = '\\';
1617

17-
/**
18-
* @var string
19-
*/
2018
protected $delimiter, $enclosure, $escape;
21-
22-
/**
23-
* @var array|null
24-
*/
2519
protected $headers;
26-
27-
/**
28-
* @var SplFileObject|null
29-
*/
3020
protected $file;
3121

3222
public function __construct(
@@ -52,9 +42,16 @@ public function import(string $path): LazyCollection
5242
yield array_combine($this->headers, $line);
5343
}
5444
}
45+
$this->resetState();
5546
});
5647
}
5748

49+
protected function resetState(): void
50+
{
51+
$this->headers = null;
52+
$this->file = null;
53+
}
54+
5855
protected function isInValidLine(array $line): bool
5956
{
6057
return count($line) === 1 && is_null($line[0]);
@@ -65,7 +62,7 @@ public function export($collection, string $path): self
6562
if (!file_exists($path)) touch($path);
6663
$this->openFileObject($path, 'w');
6764
$this->writeLines($collection);
68-
$this->closeFileObject();
65+
$this->resetState();
6966
return $this;
7067
}
7168

@@ -74,9 +71,9 @@ public function download($collection, string $filename, $headers = []): Streamed
7471
return response()->streamDownload(function () use ($collection) {
7572
$this->openFileObject('php://output', 'w');
7673
$this->writeLines($collection);
77-
$this->closeFileObject();
74+
$this->resetState();
7875
}, $filename, array_merge([
79-
'Content-Type' => 'text/csv',
76+
'Content-Type' => 'text/csv',
8077
], $headers));
8178
}
8279

@@ -97,12 +94,7 @@ protected function flattenRow($entry): array
9794

9895
protected function openFileObject(string $path, string $mode = 'r'): void
9996
{
100-
$this->file = new \SplFileObject($path, $mode);
101-
}
102-
103-
protected function closeFileObject(): void
104-
{
105-
$this->file = null;
97+
$this->file = new SplFileObject($path, $mode);
10698
}
10799

108100
protected function writeLines($collection): void
@@ -113,7 +105,7 @@ protected function writeLines($collection): void
113105
!$collection instanceof LazyCollection &&
114106
!is_array($collection)
115107
) {
116-
throw new \Exception("Non-Iterable Object cannot be iterated.");
108+
throw new Exception("Non-Iterable Object cannot be iterated.");
117109
}
118110
foreach ($collection as $entry) {
119111
if (!$this->headers) {

0 commit comments

Comments
 (0)