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\ReaderInterface; |
8
|
|
|
use Illuminate\Support\LazyCollection; |
9
|
|
|
|
10
|
|
|
class SimpleExcelReader |
11
|
|
|
{ |
12
|
|
|
/** @var \Box\Spout\Reader\ReaderInterface */ |
13
|
|
|
private $reader; |
14
|
|
|
|
15
|
|
|
/** @var \Box\Spout\Reader\IteratorInterface */ |
16
|
|
|
private $rowIterator; |
17
|
|
|
|
18
|
|
|
private $processHeader = true; |
19
|
|
|
|
20
|
|
|
public static function create(string $file) |
21
|
|
|
{ |
22
|
|
|
return new static($file); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function __construct(string $path) |
26
|
|
|
{ |
27
|
|
|
$this->reader = ReaderEntityFactory::createReaderFromFile($path); |
28
|
|
|
|
29
|
|
|
$this->reader->open($path); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function noTitleRow() |
33
|
|
|
{ |
34
|
|
|
$this->processHeader = false; |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getReader(): ReaderInterface |
40
|
|
|
{ |
41
|
|
|
return $this->getReader(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getRows(): LazyCollection |
45
|
|
|
{ |
46
|
|
|
$sheet = $this->reader->getSheetIterator()->current(); |
47
|
|
|
|
48
|
|
|
$this->rowIterator = $sheet->getRowIterator(); |
49
|
|
|
|
50
|
|
|
$this->rowIterator->rewind(); |
51
|
|
|
|
52
|
|
|
/** @var \Box\Spout\Common\Entity\Row $firstRow */ |
53
|
|
|
$firstRow = $this->rowIterator->current(); |
54
|
|
|
|
55
|
|
|
if (is_null($firstRow)) { |
56
|
|
|
$this->noTitleRow(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($this->processHeader) { |
60
|
|
|
$this->headers = $firstRow->toArray(); |
|
|
|
|
61
|
|
|
$this->rowIterator->next(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return LazyCollection::make(function () { |
65
|
|
|
while ($this->rowIterator->valid()) { |
66
|
|
|
$row = $this->rowIterator->current(); |
67
|
|
|
|
68
|
|
|
yield $this->getValueFromRow($row); |
69
|
|
|
|
70
|
|
|
$this->rowIterator->next(); |
71
|
|
|
} |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getValueFromRow(Row $row): array |
76
|
|
|
{ |
77
|
|
|
if (!$this->processHeader) { |
78
|
|
|
return $row->toArray(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$values = array_slice($row->toArray(), 0, count($this->headers)); |
82
|
|
|
|
83
|
|
|
while (count($values) < count($this->headers)) { |
84
|
|
|
$values[] = ''; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return array_combine($this->headers, $values); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function __destruct() |
91
|
|
|
{ |
92
|
|
|
$this->reader->close(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: