55
66
77use Exception ;
8- use Generator ;
9- use Illuminate \Database \Eloquent \Builder ;
10- use Illuminate \Database \Eloquent \Collection ;
8+ use Sujan \Exporter \Contracts \ExporterContract ;
119
1210class Export
1311{
1412 /**
15- * @var Builder|null
13+ * @var array|object
1614 */
17- private $ model ;
15+ protected $ model ;
1816
1917 /**
2018 * @var array
2119 */
22- private $ heading = [] ;
20+ protected $ columns ;
2321
24- /**
22+ /*
2523 * @var array
2624 */
27- private $ columns ;
25+ private $ heading = [] ;
2826
2927 /**
3028 * @var string
3129 */
32- private $ contentType = ' application/csv ' ;
30+ protected $ filename ;
3331
34- /**
35- * @var string
32+ /*
33+ * Array of content types
3634 */
37- private $ filename ;
35+ private $ contentTypes = [
36+ '.csv ' => 'application/csv ' ,
37+ '.xlsx ' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet '
38+ ];
39+
40+ private $ contentType ;
3841
3942 /**
40- * @var string
43+ * @var false|resource
4144 */
42- private $ delimiter = " ; " ;
45+ protected $ file ;
4346
4447 /**
45- * Export constructor.
46- * @param object | array $model
47- * @param array $columns
48- * @param string $filename
48+ * @var string
4949 */
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 )
5553 {
5654 $ this ->setModel ($ model );
5755 $ this ->setColumns ($ columns );
5856 $ this ->setHeading ($ columns );
5957 $ this ->setFilename ($ filename );
6058 $ this ->setContentType ();
59+ $ this ->openOutputStream ();
6160 }
6261
6362 /**
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
16364 */
164- public function getNestedData ( $ data , $ keys , $ k )
65+ protected function setModel ( $ model ): void
16566 {
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 ;
17968 }
18069
18170 /**
182- * @param string $delimiter
71+ * @param array $columns
18372 */
184- protected function setDelimiter ( string $ delimiter ): void
73+ public function setColumns ( array $ columns ): void
18574 {
186- $ this ->delimiter = $ delimiter ;
75+ $ this ->columns = $ columns ;
18776 }
18877
18978 /**
190- * @param object | array $model
79+ * @param array $heading
19180 */
192- protected function setModel ( $ model ): void
81+ protected function setHeading ( array $ heading ): void
19382 {
194- $ this ->model = $ model ;
83+ array_walk_recursive ($ heading , function ($ item ) {
84+ array_push ($ this ->heading , $ item );
85+ });
19586 }
19687
19788 /**
@@ -203,20 +94,26 @@ protected function setFilename(string $filename): void
20394 }
20495
20596 /**
206- * @param array $heading
97+ * Set content type
20798 */
208- protected function setHeading ( array $ heading ): void
99+ public function setContentType ()
209100 {
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 }; " );
213112 }
214113
215- /**
216- * @param array $columns
217- */
218- public function setColumns (array $ columns ): void
114+ private function openOutputStream ()
219115 {
220- $ this ->columns = $ columns ;
116+ // open the "output" stream
117+ $ this ->file = fopen ('php://output ' , 'w ' );
221118 }
222119}
0 commit comments