|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\SimpleExcel; |
|
4
|
|
|
|
|
5
|
|
|
use Box\Spout\Common\Entity\Row; |
|
6
|
|
|
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory; |
|
7
|
|
|
use Box\Spout\Reader\IteratorInterface; |
|
8
|
|
|
use Box\Spout\Reader\ReaderInterface; |
|
9
|
|
|
use Illuminate\Support\LazyCollection; |
|
10
|
|
|
|
|
11
|
|
|
class SimpleExcelReader |
|
12
|
|
|
{ |
|
13
|
|
|
private string $path; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
private ReaderInterface $reader; |
|
16
|
|
|
|
|
17
|
|
|
private IteratorInterface $rowIterator; |
|
18
|
|
|
|
|
19
|
|
|
private bool $processHeader = true; |
|
20
|
|
|
|
|
21
|
|
|
public static function create(string $file) |
|
22
|
|
|
{ |
|
23
|
|
|
return new static($file); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(string $path) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->path = $path; |
|
29
|
|
|
|
|
30
|
|
|
$this->reader = ReaderEntityFactory::createReaderFromFile($this->path); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getPath(): string |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->path; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function noHeaderRow() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->processHeader = false; |
|
41
|
|
|
|
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function useDelimiter(string $delimiter) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->reader->setFieldDelimiter($delimiter); |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function useFieldEnclosure(string $fieldEnclosure) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->reader->setFieldEnclosure($fieldEnclosure); |
|
55
|
|
|
|
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getReader(): ReaderInterface |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->reader; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getRows(): LazyCollection |
|
65
|
|
|
{ |
|
66
|
|
|
$this->reader->open($this->path); |
|
67
|
|
|
|
|
68
|
|
|
$this->reader->getSheetIterator()->rewind(); |
|
69
|
|
|
|
|
70
|
|
|
$sheet = $this->reader->getSheetIterator()->current(); |
|
71
|
|
|
|
|
72
|
|
|
$this->rowIterator = $sheet->getRowIterator(); |
|
73
|
|
|
|
|
74
|
|
|
$this->rowIterator->rewind(); |
|
75
|
|
|
|
|
76
|
|
|
/** @var \Box\Spout\Common\Entity\Row $firstRow */ |
|
77
|
|
|
$firstRow = $this->rowIterator->current(); |
|
78
|
|
|
|
|
79
|
|
|
if (is_null($firstRow)) { |
|
80
|
|
|
$this->noHeaderRow(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if ($this->processHeader) { |
|
84
|
|
|
$this->headers = $firstRow->toArray(); |
|
85
|
|
|
$this->rowIterator->next(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return LazyCollection::make(function () { |
|
89
|
|
|
while ($this->rowIterator->valid()) { |
|
90
|
|
|
$row = $this->rowIterator->current(); |
|
91
|
|
|
|
|
92
|
|
|
yield $this->getValueFromRow($row); |
|
93
|
|
|
|
|
94
|
|
|
$this->rowIterator->next(); |
|
95
|
|
|
} |
|
96
|
|
|
}); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function getValueFromRow(Row $row): array |
|
100
|
|
|
{ |
|
101
|
|
|
$values = $row->toArray(); |
|
102
|
|
|
ksort($values); |
|
103
|
|
|
|
|
104
|
|
|
if (! $this->processHeader) { |
|
105
|
|
|
return $values; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$values = array_slice($values, 0, count($this->headers)); |
|
109
|
|
|
|
|
110
|
|
|
while (count($values) < count($this->headers)) { |
|
111
|
|
|
$values[] = ''; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return array_combine($this->headers, $values); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function close() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->reader->close(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function __destruct() |
|
123
|
|
|
{ |
|
124
|
|
|
$this->close(); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|