Completed
Pull Request — master (#9)
by ARCANEDEV
04:43
created

AbstractImporter::setOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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