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
|
|
|
use Spatie\Macroable\Macroable; |
10
|
|
|
|
11
|
|
|
class SimpleExcelReader |
12
|
|
|
{ |
13
|
|
|
use Macroable; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
private $path; |
17
|
|
|
|
18
|
|
|
/** @var \Box\Spout\Reader\ReaderInterface */ |
19
|
|
|
private $reader; |
20
|
|
|
|
21
|
|
|
/** @var \Box\Spout\Reader\IteratorInterface */ |
22
|
|
|
private $rowIterator; |
23
|
|
|
|
24
|
|
|
private $processHeader = true; |
25
|
|
|
|
26
|
|
|
public static function create(string $file) |
27
|
|
|
{ |
28
|
|
|
return new static($file); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function __construct(string $path) |
32
|
|
|
{ |
33
|
|
|
$this->path = $path; |
34
|
|
|
|
35
|
|
|
$this->reader = ReaderEntityFactory::createReaderFromFile($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
|
|
|
if (! $this->processHeader) { |
102
|
|
|
return $row->toArray(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$values = array_slice($row->toArray(), 0, count($this->headers)); |
106
|
|
|
|
107
|
|
|
while (count($values) < count($this->headers)) { |
108
|
|
|
$values[] = ''; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return array_combine($this->headers, $values); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function close() |
115
|
|
|
{ |
116
|
|
|
$this->reader->close(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function __destruct() |
120
|
|
|
{ |
121
|
|
|
$this->close(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
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: