Completed
Push — master ( c6a537...94265d )
by ARCANEDEV
10s
created

AbstractImporter::parseAllRows()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

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