1
|
|
|
<?php namespace Arcanedev\LaravelExcel\Importers; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelExcel\Contracts\Importer as ImporterContract; |
4
|
|
|
use Arcanedev\LaravelExcel\Contracts\Parser as ParserContract; |
5
|
|
|
use Arcanedev\LaravelExcel\Traits\WithOptions; |
6
|
|
|
use Box\Spout\Reader\ReaderFactory; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AbstractExporter |
11
|
|
|
* |
12
|
|
|
* @package Arcanedev\LaravelExcel\Importer |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @todo: Refactor the parse methods |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractImporter implements ImporterContract |
18
|
|
|
{ |
19
|
|
|
/* ----------------------------------------------------------------- |
20
|
|
|
| Traits |
21
|
|
|
| ----------------------------------------------------------------- |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
use WithOptions; |
25
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
27
|
|
|
| Properties |
28
|
|
|
| ----------------------------------------------------------------- |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
protected $type; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
protected $path = ''; |
36
|
|
|
|
37
|
|
|
/** @var \Arcanedev\LaravelExcel\Contracts\Parser */ |
38
|
|
|
protected $parser; |
39
|
|
|
|
40
|
|
|
/** @var \Box\Spout\Reader\ReaderInterface */ |
41
|
|
|
protected $reader; |
42
|
|
|
|
43
|
|
|
/* ----------------------------------------------------------------- |
44
|
|
|
| Constructor |
45
|
|
|
| ----------------------------------------------------------------- |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* AbstractImporter constructor. |
50
|
|
|
* |
51
|
|
|
* @param array $options |
52
|
|
|
*/ |
53
|
57 |
|
public function __construct(array $options = []) |
54
|
|
|
{ |
55
|
57 |
|
$this->setParser(new DefaultParser); |
56
|
57 |
|
$this->setOptions($options); |
57
|
57 |
|
} |
58
|
|
|
|
59
|
|
|
/* ----------------------------------------------------------------- |
60
|
|
|
| Getters & Setters |
61
|
|
|
| ----------------------------------------------------------------- |
62
|
|
|
*/ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set the path. |
66
|
|
|
* |
67
|
|
|
* @param string $path |
68
|
|
|
* |
69
|
|
|
* @return self |
70
|
|
|
*/ |
71
|
18 |
|
public function setPath($path) |
72
|
|
|
{ |
73
|
18 |
|
$this->path = $path; |
74
|
|
|
|
75
|
18 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set the parser. |
80
|
|
|
* |
81
|
|
|
* @param \Arcanedev\LaravelExcel\Contracts\Parser $parser |
82
|
|
|
* |
83
|
|
|
* @return self |
84
|
|
|
*/ |
85
|
57 |
|
public function setParser(ParserContract $parser) |
86
|
|
|
{ |
87
|
57 |
|
$this->parser = $parser; |
88
|
|
|
|
89
|
57 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the file type. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
21 |
|
public function getType() |
98
|
|
|
{ |
99
|
21 |
|
return $this->type; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/* ----------------------------------------------------------------- |
103
|
|
|
| Main Methods |
104
|
|
|
| ----------------------------------------------------------------- |
105
|
|
|
*/ |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Load the file. |
109
|
|
|
* |
110
|
|
|
* @param string $path |
111
|
|
|
* |
112
|
|
|
* @return self |
113
|
|
|
*/ |
114
|
18 |
|
public function load($path) |
115
|
|
|
{ |
116
|
18 |
|
return $this->setPath($path); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the parsed data for a single sheet. |
121
|
|
|
* |
122
|
|
|
* @param int|string $sheet |
123
|
|
|
* |
124
|
|
|
* @return \Illuminate\Support\Collection |
125
|
|
|
*/ |
126
|
18 |
|
public function get($sheet = 1) |
127
|
|
|
{ |
128
|
18 |
|
$this->create(); |
129
|
18 |
|
$this->reader->open($this->path); |
130
|
18 |
|
$collection = $this->parseRows($sheet); |
131
|
18 |
|
$this->reader->close(); |
132
|
|
|
|
133
|
18 |
|
return $collection; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the parsed data for all sheets. |
138
|
|
|
* |
139
|
|
|
* @return \Illuminate\Support\Collection |
140
|
|
|
*/ |
141
|
9 |
|
public function all() |
142
|
|
|
{ |
143
|
9 |
|
return $this->get('*'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/* ----------------------------------------------------------------- |
147
|
|
|
| Other Methods |
148
|
|
|
| ----------------------------------------------------------------- |
149
|
|
|
*/ |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Create the reader instance. |
153
|
|
|
*/ |
154
|
18 |
|
protected function create() |
155
|
|
|
{ |
156
|
18 |
|
$this->reader = ReaderFactory::create($this->type); |
157
|
18 |
|
$this->loadOptions(); |
158
|
18 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Load the reader options. |
162
|
|
|
*/ |
163
|
|
|
abstract protected function loadOptions(); |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Parse the rows of selected sheet. |
167
|
|
|
* |
168
|
|
|
* @param int|string $sheetIndex |
169
|
|
|
* |
170
|
|
|
* @return \Illuminate\Support\Collection |
171
|
|
|
*/ |
172
|
18 |
|
protected function parseRows($sheetIndex) |
173
|
|
|
{ |
174
|
18 |
|
$sheets = $this->newCollection(); |
175
|
|
|
|
176
|
18 |
|
foreach ($this->reader->getSheetIterator() as $index => $sheet) { |
177
|
18 |
|
if ($sheetIndex === '*') { |
178
|
9 |
|
$rows = $this->newCollection(); |
179
|
|
|
|
180
|
9 |
|
foreach ($sheet->getRowIterator() as $row) { |
181
|
9 |
|
$rows->push($this->parser->transform($row)); |
182
|
|
|
} |
183
|
|
|
|
184
|
9 |
|
$sheets->put($index, $rows); |
185
|
|
|
} |
186
|
9 |
|
elseif ($sheetIndex === $index) { |
187
|
9 |
|
foreach ($sheet->getRowIterator() as $row) { |
188
|
9 |
|
$sheets->push($this->parser->transform($row)); |
189
|
|
|
} |
190
|
15 |
|
break; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
18 |
|
return $sheets; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Create a new collection object. |
199
|
|
|
* |
200
|
|
|
* @return \Illuminate\Support\Collection |
201
|
|
|
*/ |
202
|
18 |
|
protected function newCollection() |
203
|
|
|
{ |
204
|
18 |
|
return new Collection; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|