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