Completed
Push — master ( 56bd52...cf94f2 )
by ARCANEDEV
8s
created

AbstractExporter::setSerializer()   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\Exporters;
2
3
use Illuminate\Support\Collection;
4
use Box\Spout\Writer\WriterFactory;
5
use Arcanedev\LaravelExcel\Contracts\Serializer as SerializerContract;
6
use Arcanedev\LaravelExcel\Contracts\Exporter as ExporterContract;
7
8
/**
9
 * Class     AbstractExporter
10
 *
11
 * @package  Arcanedev\LaravelExcel\Exporter
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
abstract class AbstractExporter implements ExporterContract
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
21
    /** @var array */
22
    protected $data = [];
23
24
    /** @var string */
25
    protected $type;
26
27
    /** @var \Arcanedev\LaravelExcel\Contracts\Serializer */
28
    protected $serializer;
29
30
    /** @var \Box\Spout\Writer\WriterInterface */
31
    protected $writer;
32
33
    /** @var array */
34
    protected $options = [];
35
36
    /* -----------------------------------------------------------------
37
     |  Constructor
38
     | -----------------------------------------------------------------
39
     */
40
41
    /**
42
     * AbstractExporter constructor.
43
     *
44
     * @param  array  $options
45
     */
46 27
    public function __construct(array $options = [])
47
    {
48 27
        $this->setSerializer(new DefaultSerializer);
49 27
        $this->setOptions($options);
50 27
    }
51
52
    /* -----------------------------------------------------------------
53
     |  Getters & Setters
54
     | -----------------------------------------------------------------
55
     */
56
57
    /**
58
     * Set the serializer.
59
     *
60
     * @param  \Arcanedev\LaravelExcel\Contracts\Serializer  $serializer
61
     *
62
     * @return self
63
     */
64 27
    public function setSerializer(SerializerContract $serializer)
65
    {
66 27
        $this->serializer = $serializer;
67
68 27
        return $this;
69
    }
70
71
    /**
72
     * Get the file type.
73
     *
74
     * @return string
75
     */
76 18
    public function getType()
77
    {
78 18
        return $this->type;
79
    }
80
81
    /**
82
     * Set the writer options.
83
     *
84
     * @param  array  $options
85
     *
86
     * @return self
87
     */
88 27
    public function setOptions(array $options)
89
    {
90 27
        $this->options = $options;
91
92 27
        return $this;
93
    }
94
95
    /* -----------------------------------------------------------------
96
     |  Main Methods
97
     | -----------------------------------------------------------------
98
     */
99
100
    /**
101
     * Load the data.
102
     *
103
     * @param  \Illuminate\Support\Collection  $data
104
     *
105
     * @return self
106
     */
107 9
    public function load(Collection $data)
108
    {
109 9
        $this->data = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data of type object<Illuminate\Support\Collection> is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
110
111 9
        return $this;
112
    }
113
114
    /**
115
     * Save the data to a file.
116
     *
117
     * @param  string  $filename
118
     */
119 9
    public function save($filename)
120
    {
121 9
        $this->create();
122 9
        $this->writer->openToFile($filename);
123 9
        $this->makeRows();
124 9
        $this->close();
125 9
    }
126
127
    /* -----------------------------------------------------------------
128
     |  Other Methods
129
     | -----------------------------------------------------------------
130
     */
131
132
    /**
133
     * Create the writer.
134
     */
135 9
    protected function create()
136
    {
137 9
        $this->writer = WriterFactory::create($this->type);
138 9
        $this->loadOptions();
139 9
    }
140
141
    /**
142
     * Load the writer options.
143
     */
144
    abstract protected function loadOptions();
145
146
    /**
147
     * Close the writer.
148
     */
149 9
    protected function close()
150
    {
151 9
        $this->writer->close();
152 9
    }
153
154
    /**
155
     * Make rows.
156
     */
157 9
    protected function makeRows()
158
    {
159 9
        if ( ! empty($headerRow = $this->serializer->getHeader()))
160
            $this->writer->addRow($headerRow);
161
162 9
        foreach ($this->data as $record) {
163 6
            $this->writer->addRow(
164 6
                $this->serializer->getData($record)
165
            );
166
        }
167 9
    }
168
}
169