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 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 noTitleRow() |
36
|
|
|
{ |
37
|
|
|
$this->processHeader = false; |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function useDelimiter(string $delimiter) |
43
|
|
|
{ |
44
|
|
|
$this->reader->setFieldDelimiter($delimiter); |
|
|
|
|
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function useFieldEnclosure(string $fieldEnclosure) |
50
|
|
|
{ |
51
|
|
|
$this->reader->setFieldEnclosure($fieldEnclosure); |
|
|
|
|
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getReader(): ReaderInterface |
57
|
|
|
{ |
58
|
|
|
return $this->getReader(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getRows(): LazyCollection |
62
|
|
|
{ |
63
|
|
|
$this->reader->open($this->path); |
64
|
|
|
|
65
|
|
|
$sheet = $this->reader->getSheetIterator()->current(); |
66
|
|
|
|
67
|
|
|
$this->rowIterator = $sheet->getRowIterator(); |
68
|
|
|
|
69
|
|
|
$this->rowIterator->rewind(); |
70
|
|
|
|
71
|
|
|
/** @var \Box\Spout\Common\Entity\Row $firstRow */ |
72
|
|
|
$firstRow = $this->rowIterator->current(); |
73
|
|
|
|
74
|
|
|
if (is_null($firstRow)) { |
75
|
|
|
$this->noTitleRow(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($this->processHeader) { |
79
|
|
|
$this->headers = $firstRow->toArray(); |
|
|
|
|
80
|
|
|
$this->rowIterator->next(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return LazyCollection::make(function () { |
84
|
|
|
while ($this->rowIterator->valid()) { |
85
|
|
|
$row = $this->rowIterator->current(); |
86
|
|
|
|
87
|
|
|
yield $this->getValueFromRow($row); |
88
|
|
|
|
89
|
|
|
$this->rowIterator->next(); |
90
|
|
|
} |
91
|
|
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function getValueFromRow(Row $row): array |
95
|
|
|
{ |
96
|
|
|
if (!$this->processHeader) { |
97
|
|
|
return $row->toArray(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$values = array_slice($row->toArray(), 0, count($this->headers)); |
101
|
|
|
|
102
|
|
|
while (count($values) < count($this->headers)) { |
103
|
|
|
$values[] = ''; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return array_combine($this->headers, $values); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function __destruct() |
110
|
|
|
{ |
111
|
|
|
$this->reader->close(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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: