1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
The MIT License (MIT) |
4
|
|
|
|
5
|
|
|
Copyright (c) 2015 PortPHP |
6
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
in the Software without restriction, including without limitation the rights |
10
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
furnished to do so, subject to the following conditions: |
13
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in all |
15
|
|
|
copies or substantial portions of the Software. |
16
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
namespace Port\Spreadsheet; |
26
|
|
|
|
27
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
28
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
29
|
|
|
use Port\Writer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Writes to an Spreadsheet file |
33
|
|
|
* |
34
|
|
|
* @author David de Boer <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class SpreadsheetWriter implements Writer |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var Spreadsheet |
40
|
|
|
*/ |
41
|
|
|
protected $spreadsheet; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $filename; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $prependHeaderRow; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
protected $row = 1; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var null|string |
60
|
|
|
*/ |
61
|
|
|
protected $sheet; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected $type; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \SplFileObject $file File |
70
|
|
|
* @param string $sheet Sheet title (optional) |
71
|
|
|
* @param string $type Spreadsheet file type (defaults to Xlsx) |
72
|
|
|
* @param bool $prependHeaderRow |
73
|
|
|
*/ |
74
|
4 |
|
public function __construct(\SplFileObject $file, $sheet = null, $type = 'Xlsx', $prependHeaderRow = false) |
75
|
|
|
{ |
76
|
4 |
|
$this->filename = $file->getPathname(); |
77
|
4 |
|
$this->sheet = $sheet; |
78
|
4 |
|
$this->type = $type; |
79
|
4 |
|
$this->prependHeaderRow = $prependHeaderRow; |
80
|
4 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Wrap up the writer after all items have been written |
84
|
|
|
* |
85
|
|
|
* @return void Any returned value is ignored. |
86
|
|
|
*/ |
87
|
4 |
|
public function finish() |
88
|
|
|
{ |
89
|
4 |
|
$writer = IOFactory::createWriter($this->spreadsheet, $this->type); |
90
|
4 |
|
$writer->save($this->filename); |
91
|
4 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Prepare the writer before writing the items |
95
|
|
|
* |
96
|
|
|
* @return void Any returned value is ignored. |
97
|
|
|
*/ |
98
|
4 |
|
public function prepare() |
99
|
|
|
{ |
100
|
4 |
|
$reader = IOFactory::createReader($this->type); |
101
|
4 |
|
if ($reader->canRead($this->filename)) { |
102
|
1 |
|
$this->spreadsheet = $reader->load($this->filename); |
103
|
1 |
|
} else { |
104
|
4 |
|
$this->spreadsheet = new Spreadsheet(); |
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
if (null !== $this->sheet) { |
108
|
1 |
|
if (!$this->spreadsheet->sheetNameExists($this->sheet)) { |
109
|
1 |
|
$this->spreadsheet->createSheet()->setTitle($this->sheet); |
110
|
1 |
|
} |
111
|
1 |
|
$this->spreadsheet->setActiveSheetIndexByName($this->sheet); |
112
|
1 |
|
} |
113
|
4 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Write one data item |
117
|
|
|
* |
118
|
|
|
* @param array $item The data item with converted values |
119
|
|
|
* |
120
|
|
|
* @return void Any returned value is ignored. |
121
|
|
|
*/ |
122
|
4 |
|
public function writeItem(array $item) |
123
|
|
|
{ |
124
|
4 |
|
$count = count($item); |
125
|
|
|
|
126
|
4 |
|
if ($this->prependHeaderRow && 1 === $this->row) { |
127
|
1 |
|
$headers = array_keys($item); |
128
|
|
|
|
129
|
1 |
View Code Duplication |
for ($i = 0; $i < $count; $i++) { |
|
|
|
|
130
|
1 |
|
$this->spreadsheet->getActiveSheet()->setCellValueByColumnAndRow($i + 1, $this->row, $headers[$i]); |
131
|
1 |
|
} |
132
|
1 |
|
$this->row++; |
133
|
1 |
|
} |
134
|
|
|
|
135
|
4 |
|
$values = array_values($item); |
136
|
|
|
|
137
|
4 |
View Code Duplication |
for ($i = 0; $i < $count; $i++) { |
|
|
|
|
138
|
4 |
|
$this->spreadsheet->getActiveSheet()->setCellValueByColumnAndRow($i + 1, $this->row, $values[$i]); |
139
|
4 |
|
} |
140
|
|
|
|
141
|
4 |
|
$this->row++; |
142
|
4 |
|
} |
143
|
|
|
} |
144
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.