5
5
6
6
7
7
use Exception ;
8
- use Generator ;
9
- use Illuminate \Database \Eloquent \Builder ;
10
- use Illuminate \Database \Eloquent \Collection ;
8
+ use Sujan \Exporter \Contracts \ExporterContract ;
11
9
12
10
class Export
13
11
{
14
12
/**
15
- * @var Builder|null
13
+ * @var array|object
16
14
*/
17
- private $ model ;
15
+ protected $ model ;
18
16
19
17
/**
20
18
* @var array
21
19
*/
22
- private $ heading = [] ;
20
+ protected $ columns ;
23
21
24
- /**
22
+ /*
25
23
* @var array
26
24
*/
27
- private $ columns ;
25
+ private $ heading = [] ;
28
26
29
27
/**
30
28
* @var string
31
29
*/
32
- private $ contentType = ' application/csv ' ;
30
+ protected $ filename ;
33
31
34
- /**
35
- * @var string
32
+ /*
33
+ * Array of content types
36
34
*/
37
- private $ filename ;
35
+ private $ contentTypes = [
36
+ '.csv ' => 'application/csv ' ,
37
+ '.xlsx ' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet '
38
+ ];
39
+
40
+ private $ contentType ;
38
41
39
42
/**
40
- * @var string
43
+ * @var false|resource
41
44
*/
42
- private $ delimiter = " ; " ;
45
+ protected $ file ;
43
46
44
47
/**
45
- * Export constructor.
46
- * @param object | array $model
47
- * @param array $columns
48
- * @param string $filename
48
+ * @var string
49
49
*/
50
- public function __construct (
51
- $ model ,
52
- array $ columns = [],
53
- string $ filename = 'export.csv '
54
- )
50
+ protected $ delimiter = "; " ;
51
+
52
+ protected function setConfiguration ($ model , array $ columns , string $ filename )
55
53
{
56
54
$ this ->setModel ($ model );
57
55
$ this ->setColumns ($ columns );
58
56
$ this ->setHeading ($ columns );
59
57
$ this ->setFilename ($ filename );
60
58
$ this ->setContentType ();
59
+ $ this ->openOutputStream ();
61
60
}
62
61
63
62
/**
64
- * Set content type
65
- */
66
- public function setContentType ()
67
- {
68
- header ("Content-Type: {$ this ->contentType }" );
69
- header ("Content-Disposition: attachment; filename= {$ this ->filename }; " );
70
- }
71
-
72
- /**
73
- * return generated CSV
74
- * @throws Exception
75
- */
76
- public function export ()
77
- {
78
- $ generator = $ this ->write ();
79
-
80
- while ($ generator ->valid ()) {
81
- $ generator ->next ();
82
- }
83
-
84
- die ();
85
- }
86
-
87
- /**
88
- * @return Generator
89
- * @throws Exception
90
- */
91
- public function write ()
92
- {
93
- // open the "output" stream
94
- $ file = fopen ('php://output ' , 'w ' );
95
-
96
- fputcsv ($ file , $ this ->heading , $ this ->delimiter );
97
-
98
- if (is_array ($ this ->model )) {
99
- foreach ($ this ->model as $ data ) {
100
- $ line = $ this ->getLine ($ data );
101
-
102
- yield fputcsv ($ file , $ line , $ this ->delimiter );
103
- }
104
- } else {
105
- $ className = !is_string ($ this ->model ) ? class_basename ($ this ->model ) : null ;
106
-
107
- switch ($ className ) {
108
- case 'Collection ' :
109
- foreach ($ this ->model as $ data ) {
110
- $ line = $ this ->getLine ($ data );
111
-
112
- yield fputcsv ($ file , $ line , $ this ->delimiter );
113
- }
114
- break ;
115
- case 'Builder ' :
116
- foreach ($ this ->model ->get () as $ data ) {
117
- $ line = $ this ->getLine ($ data );
118
-
119
- yield fputcsv ($ file , $ line , $ this ->delimiter );
120
- }
121
- break ;
122
- case 'PDOStatement ' :
123
- foreach ($ this ->model ->fetchAll () as $ data ) {
124
- $ line = $ this ->getLine ($ data );
125
-
126
- yield fputcsv ($ file , $ line , $ this ->delimiter );
127
- }
128
- break ;
129
- default :
130
- throw new Exception ('Type unknown ' );
131
- }
132
- }
133
-
134
- fclose ($ file );
135
- }
136
-
137
- /**
138
- * @param $data
139
- * @return array
140
- */
141
- public function getLine ($ data )
142
- {
143
- $ line = [];
144
-
145
- foreach ($ this ->columns as $ k => $ key ) {
146
- if (is_array ($ key )) {
147
- $ value = $ this ->getNestedData ($ data , $ key , $ k );
148
- array_push ($ line , $ value );
149
- } else {
150
- $ value = is_array ($ data ) ? $ data [$ key ] : $ data ->{$ key };
151
- array_push ($ line , $ value );
152
- }
153
- }
154
-
155
- return $ line ;
156
- }
157
-
158
- /**
159
- * @param $data
160
- * @param $keys
161
- * @param $k
162
- * @return string
63
+ * @param object | array $model
163
64
*/
164
- public function getNestedData ( $ data , $ keys , $ k )
65
+ protected function setModel ( $ model ): void
165
66
{
166
- foreach ($ keys as $ kk => $ key ) {
167
- if (is_array ($ data )) {
168
- $ data = isset ($ data [$ k ][$ key ]) ? $ data [$ k ][$ key ] : '' ;
169
- } else {
170
- $ data = isset ($ data ->{$ k }->{$ key }) ? $ data ->{$ k }->{$ key } : '' ;
171
- }
172
-
173
- if (is_array ($ data )) {
174
- $ this ->getNestedData ($ data , $ key , $ kk );
175
- }
176
- }
177
-
178
- return $ data ;
67
+ $ this ->model = $ model ;
179
68
}
180
69
181
70
/**
182
- * @param string $delimiter
71
+ * @param array $columns
183
72
*/
184
- protected function setDelimiter ( string $ delimiter ): void
73
+ public function setColumns ( array $ columns ): void
185
74
{
186
- $ this ->delimiter = $ delimiter ;
75
+ $ this ->columns = $ columns ;
187
76
}
188
77
189
78
/**
190
- * @param object | array $model
79
+ * @param array $heading
191
80
*/
192
- protected function setModel ( $ model ): void
81
+ protected function setHeading ( array $ heading ): void
193
82
{
194
- $ this ->model = $ model ;
83
+ array_walk_recursive ($ heading , function ($ item ) {
84
+ array_push ($ this ->heading , $ item );
85
+ });
195
86
}
196
87
197
88
/**
@@ -203,20 +94,26 @@ protected function setFilename(string $filename): void
203
94
}
204
95
205
96
/**
206
- * @param array $heading
97
+ * Set content type
207
98
*/
208
- protected function setHeading ( array $ heading ): void
99
+ public function setContentType ()
209
100
{
210
- array_walk_recursive ($ heading , function ($ item ) {
211
- array_push ($ this ->heading , $ item );
212
- });
101
+ preg_match ('/(?:.csv|.xlsx)/i ' , $ this ->filename , $ parts );
102
+
103
+ if (!$ parts [0 ]) {
104
+ $ this ->filename = $ this ->filename . '.csv ' ;
105
+ $ this ->contentType = $ this ->contentTypes ['.csv ' ];
106
+ } else {
107
+ $ this ->contentType = $ this ->contentTypes [strtolower ($ parts [0 ])];
108
+ }
109
+
110
+ header ("Content-Type: {$ this ->contentType }" );
111
+ header ("Content-Disposition: attachment; filename= {$ this ->filename }; " );
213
112
}
214
113
215
- /**
216
- * @param array $columns
217
- */
218
- public function setColumns (array $ columns ): void
114
+ private function openOutputStream ()
219
115
{
220
- $ this ->columns = $ columns ;
116
+ // open the "output" stream
117
+ $ this ->file = fopen ('php://output ' , 'w ' );
221
118
}
222
119
}
0 commit comments