|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: alive |
|
5
|
|
|
* Date: 4/17/18 |
|
6
|
|
|
* Time: 2:58 AM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Alive2212\ExcelHelper; |
|
10
|
|
|
|
|
11
|
|
|
use Maatwebsite\Excel\Classes\LaravelExcelWorksheet; |
|
12
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
|
13
|
|
|
use Maatwebsite\Excel\Writers\LaravelExcelWriter; |
|
14
|
|
|
|
|
15
|
|
|
class ExcelHelper |
|
16
|
|
|
{ |
|
17
|
|
|
protected $options = [ |
|
18
|
|
|
'store_format' => 'xls', |
|
19
|
|
|
'download_format' => 'xls', |
|
20
|
|
|
'title' => 'title', |
|
21
|
|
|
'creator' => 'user', |
|
22
|
|
|
'company' => 'Alive Co', |
|
23
|
|
|
'description' => 'this is excel file from table', |
|
24
|
|
|
'sheet' => 'sheet1', |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
protected $excel = ''; |
|
28
|
|
|
|
|
29
|
|
|
protected $table = ''; |
|
30
|
|
|
|
|
31
|
|
|
protected $arrayTable = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* ExcelHelper constructor. |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function __construct() |
|
37
|
|
|
{ |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
static function create(){ |
|
|
|
|
|
|
41
|
|
|
// dd("Taylor Swift felt to Babak Nodoust Love"); |
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function getOptions() |
|
48
|
|
|
{ |
|
49
|
1 |
|
return $this->options; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param array $options |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function setOptions($options) |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->options = array_merge($this->options, $options); |
|
59
|
1 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* download excel |
|
64
|
|
|
*/ |
|
65
|
|
|
public function download() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->excel->store($this->options['store_format'])->download($this->options['download_format']); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param $arrayTable |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function exportTable($arrayTable) |
|
74
|
|
|
{ |
|
75
|
1 |
|
$this->arrayTable = $arrayTable; |
|
76
|
1 |
|
$this->table(); |
|
77
|
1 |
|
$this->options; |
|
78
|
1 |
|
$this->createExcelFile(); |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
public function createExcelFile() |
|
82
|
|
|
{ |
|
83
|
1 |
|
$options = $this->options; |
|
84
|
1 |
|
$table = $this->table; |
|
85
|
1 |
|
$this->excel = Excel::create('payments', function (LaravelExcelWriter $excel) use ($table, $options) { |
|
86
|
|
|
$excel->setTitle($options['title']); |
|
87
|
|
|
$excel->setCreator($options['creator'])->setCompany($options['company']); |
|
88
|
|
|
$excel->setDescription($options['description']); |
|
89
|
|
|
$closer = function (LaravelExcelWorksheet $sheet) use ($table){ |
|
90
|
|
|
$sheet->fromArray($table, null, 'A1', false, false); |
|
|
|
|
|
|
91
|
|
|
}; |
|
92
|
|
|
$excel->sheet("sheet", $closer); |
|
93
|
1 |
|
}); |
|
94
|
1 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param $arrayTables |
|
99
|
|
|
* @return array |
|
100
|
|
|
* @internal param string $key |
|
101
|
|
|
* @internal param $arrayTable |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function getTitleOfArrayTable($arrayTables) |
|
104
|
|
|
{ |
|
105
|
1 |
|
$result = []; |
|
106
|
1 |
|
foreach ($arrayTables as $arrayTable) { |
|
107
|
1 |
|
list($titles, $key) = $this->titleCrawler($arrayTable); |
|
108
|
1 |
|
foreach ($titles as $title) { |
|
109
|
1 |
|
if (!is_int(array_search($title, $result))) { |
|
110
|
1 |
|
array_push($result, $title); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
1 |
|
return $result; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $arrayTable |
|
119
|
|
|
* @param string $parentKey |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public function titleCrawler($arrayTable, $parentKey = '') |
|
123
|
|
|
{ |
|
124
|
1 |
|
$result = []; |
|
125
|
1 |
|
$titlesKey = ''; |
|
126
|
1 |
|
foreach ($arrayTable as $key => $value) { |
|
127
|
1 |
|
if (is_array($value)) { |
|
128
|
|
|
list($titles, $titlesKey) = $this->titleCrawler($value, $parentKey == '' ? $key : $parentKey . '.' . $key); |
|
129
|
|
|
foreach ($titles as $titleKey => $titleValue) { |
|
130
|
|
|
array_push($result, $titlesKey == '' ? $titleValue : $titlesKey . '.' . $titleValue); |
|
131
|
|
|
} |
|
132
|
|
|
} else { |
|
133
|
1 |
|
array_push($result, $parentKey == '' ? $key : $parentKey . '.' . $key); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
1 |
|
return array($result, $titlesKey); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param array $titles |
|
141
|
|
|
* @param bool $setHeader |
|
142
|
|
|
* @return $this |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function table($titles = [], $setHeader = true) |
|
145
|
|
|
{ |
|
146
|
1 |
|
$arrayTable = $this->arrayTable; |
|
147
|
1 |
|
$result = []; |
|
148
|
1 |
|
if (count($titles) == 0) { |
|
149
|
1 |
|
$titles = $this->getTitleOfArrayTable($arrayTable); |
|
150
|
|
|
} |
|
151
|
1 |
|
if ($setHeader) { |
|
152
|
1 |
|
array_push($result, $titles); |
|
153
|
|
|
} |
|
154
|
1 |
|
foreach ($arrayTable as $item) { |
|
155
|
1 |
|
$arrayTableRecord = []; |
|
156
|
1 |
|
foreach ($titles as $title) { |
|
157
|
1 |
|
if (array_key_exists(explode('.', $title)[0], $item)) { |
|
158
|
1 |
|
array_push($arrayTableRecord, $this->getInnerValue($item, $title)); |
|
159
|
|
|
} else { |
|
160
|
1 |
|
array_push($arrayTableRecord, null); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
1 |
|
array_push($result, $arrayTableRecord); |
|
164
|
|
|
} |
|
165
|
1 |
|
$this->table = $result; |
|
166
|
1 |
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param $array |
|
171
|
|
|
* @param $key |
|
172
|
|
|
* @return mixed |
|
173
|
|
|
*/ |
|
174
|
1 |
|
public function getInnerValue($array, $key) |
|
175
|
|
|
{ |
|
176
|
1 |
|
$keyTree = explode('.', $key); |
|
177
|
1 |
|
if (count($keyTree) > 1) { |
|
178
|
|
|
$key = $keyTree[0]; |
|
179
|
|
|
unset($keyTree[0]); |
|
180
|
|
|
return $this->getInnerValue($array[$key], implode('.', $keyTree)); |
|
181
|
|
|
} else { |
|
182
|
1 |
|
return $array[$keyTree[0]]; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param $arrayTable |
|
188
|
|
|
* @return mixed |
|
189
|
|
|
*/ |
|
190
|
|
|
public function arrayToTable($arrayTable) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->getTitleOfArrayTable($arrayTable); |
|
193
|
|
|
$this->table($arrayTable); |
|
194
|
|
|
return $arrayTable; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param $name |
|
199
|
|
|
* @param $arguments |
|
200
|
|
|
* @return mixed |
|
201
|
|
|
*/ |
|
202
|
|
|
public static function __callStatic($name, $arguments) |
|
203
|
|
|
{ |
|
204
|
|
|
return (new static)->$name(...$arguments); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return array |
|
209
|
|
|
*/ |
|
210
|
1 |
|
public function getArrayTable(): array |
|
211
|
|
|
{ |
|
212
|
1 |
|
return $this->arrayTable; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param array $arrayTable |
|
217
|
|
|
*/ |
|
218
|
1 |
|
public function setArrayTable(array $arrayTable) |
|
219
|
|
|
{ |
|
220
|
1 |
|
$this->arrayTable = $arrayTable; |
|
221
|
1 |
|
} |
|
222
|
|
|
|
|
223
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.