|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Spreadsheet\Reader; |
|
5
|
|
|
|
|
6
|
|
|
use Spreadsheet\{ |
|
7
|
|
|
SpreadsheetInterface, |
|
8
|
|
|
Spreadsheet, |
|
9
|
|
|
Exception\NestedDirectoryReadNotSupportedException, |
|
10
|
|
|
SheetInterface, |
|
11
|
|
|
Sheet, |
|
12
|
|
|
Cell, |
|
13
|
|
|
Position |
|
14
|
|
|
}; |
|
15
|
|
|
use Innmind\Filesystem\{ |
|
16
|
|
|
FileInterface, |
|
17
|
|
|
DirectoryInterface |
|
18
|
|
|
}; |
|
19
|
|
|
|
|
20
|
|
|
final class CsvReader implements ReaderInterface |
|
21
|
|
|
{ |
|
22
|
|
|
private $delimiter; |
|
23
|
|
|
private $useFirstLineAsColumnIdentifier; |
|
24
|
|
|
|
|
25
|
5 |
|
public function __construct( |
|
26
|
|
|
string $delimiter, |
|
27
|
|
|
bool $useFirstLineAsColumnIdentifier |
|
28
|
|
|
) { |
|
29
|
5 |
|
$this->delimiter = $delimiter; |
|
30
|
5 |
|
$this->useFirstLineAsColumnIdentifier = $useFirstLineAsColumnIdentifier; |
|
31
|
5 |
|
} |
|
32
|
|
|
|
|
33
|
4 |
|
public function read(FileInterface $file): SpreadsheetInterface |
|
34
|
|
|
{ |
|
35
|
4 |
|
$spreadsheet = new Spreadsheet( |
|
36
|
4 |
|
basename((string) $file->name(), '.csv') |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
4 |
|
if ($file instanceof DirectoryInterface) { |
|
40
|
2 |
|
foreach ($file as $subFile) { |
|
41
|
2 |
|
if ($subFile instanceof DirectoryInterface) { |
|
42
|
1 |
|
throw new NestedDirectoryReadNotSupportedException; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
$spreadsheet = $spreadsheet->add( |
|
46
|
1 |
|
$this->buildSheet($subFile) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
} else { |
|
50
|
2 |
|
$spreadsheet = $spreadsheet->add( |
|
51
|
2 |
|
$this->buildSheet($file) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
return $spreadsheet; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
private function buildSheet(FileInterface $file): SheetInterface |
|
59
|
|
|
{ |
|
60
|
3 |
|
$csv = tmpfile(); |
|
61
|
3 |
|
$stream = $file->content()->rewind(); |
|
62
|
|
|
|
|
63
|
3 |
|
while (!$stream->isEof()) { |
|
64
|
3 |
|
fwrite($csv, $stream->read(8192)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
rewind($csv); |
|
68
|
3 |
|
$sheet = new Sheet( |
|
69
|
3 |
|
basename((string) $file->name(), '.csv') |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
3 |
|
$position = 1; |
|
73
|
3 |
|
$firstLine = fgetcsv($csv, 0, $this->delimiter); |
|
74
|
|
|
|
|
75
|
3 |
|
if (!$this->useFirstLineAsColumnIdentifier) { |
|
76
|
1 |
|
rewind($csv); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
3 |
|
while (($line = fgetcsv($csv, 0, $this->delimiter)) !== false) { |
|
80
|
3 |
|
foreach ($line as $key => $value) { |
|
81
|
3 |
|
$sheet = $sheet->add( |
|
82
|
3 |
|
new Cell( |
|
83
|
3 |
|
new Position( |
|
84
|
3 |
|
$this->useFirstLineAsColumnIdentifier ? |
|
85
|
3 |
|
$firstLine[$key] : $key, |
|
86
|
|
|
$position |
|
87
|
|
|
), |
|
88
|
|
|
$value |
|
89
|
|
|
) |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
++$position; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
3 |
|
fclose($csv); |
|
97
|
|
|
|
|
98
|
3 |
|
return $sheet; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|