44namespace Sujan \Exporter ;
55
66
7+ use Exception ;
78use Generator ;
89use Illuminate \Database \Eloquent \Builder ;
10+ use Illuminate \Database \Eloquent \Collection ;
911
1012class Export
1113{
@@ -27,7 +29,7 @@ class Export
2729 /**
2830 * @var string
2931 */
30- private $ contentType ;
32+ private $ contentType = ' application/csv ' ;
3133
3234 /**
3335 * @var string
@@ -37,43 +39,39 @@ class Export
3739 /**
3840 * @var string
3941 */
40- private $ delimiter ;
42+ private $ delimiter = " ; " ;
4143
4244 /**
4345 * Export constructor.
44- * @param Builder|null $model
46+ * @param object | array $model
4547 * @param array $columns
46- * @param string $contentType
4748 * @param string $filename
48- * @param string $delimiter
4949 */
5050 public function __construct (
51- Builder $ model = null ,
51+ $ model ,
5252 array $ columns = [],
53- string $ contentType = 'application/csv ' ,
54- string $ filename = 'export.csv ' ,
55- string $ delimiter = "; "
53+ string $ filename = 'export.csv '
5654 )
5755 {
5856 $ this ->setModel ($ model );
5957 $ this ->setColumns ($ columns );
6058 $ this ->setHeading ($ columns );
6159 $ this ->setFilename ($ filename );
62- $ this ->setContentType ($ contentType );
63- $ this ->setDelimiter ($ delimiter );
60+ $ this ->setContentType ();
6461 }
6562
6663 /**
67- * @param $contentType
64+ * Set content type
6865 */
69- public function setContentType ($ contentType )
66+ public function setContentType ()
7067 {
71- header ("Content-Type: {$ contentType }" );
68+ header ("Content-Type: {$ this -> contentType }" );
7269 header ("Content-Disposition: attachment; filename= {$ this ->filename }; " );
7370 }
7471
7572 /**
7673 * return generated CSV
74+ * @throws Exception
7775 */
7876 public function export ()
7977 {
@@ -82,10 +80,13 @@ public function export()
8280 while ($ generator ->valid ()) {
8381 $ generator ->next ();
8482 }
83+
84+ die ();
8585 }
8686
8787 /**
8888 * @return Generator
89+ * @throws Exception
8990 */
9091 public function write ()
9192 {
@@ -94,10 +95,40 @@ public function write()
9495
9596 fputcsv ($ file , $ this ->heading , $ this ->delimiter );
9697
97- foreach ($ this ->model ->get () as $ data ) {
98- $ line = $ this ->getLine ($ data );
98+ if (is_array ($ this ->model )) {
99+ foreach ($ this ->model as $ data ) {
100+ $ line = $ this ->getLine ($ data );
99101
100- yield fputcsv ($ file , $ line , $ this ->delimiter );
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+ }
101132 }
102133
103134 fclose ($ file );
@@ -116,7 +147,8 @@ public function getLine($data)
116147 $ value = $ this ->getNestedData ($ data , $ key , $ k );
117148 array_push ($ line , $ value );
118149 } else {
119- array_push ($ line , $ data ->{$ key });
150+ $ value = is_array ($ data ) ? $ data [$ key ] : $ data ->{$ key };
151+ array_push ($ line , $ value );
120152 }
121153 }
122154
@@ -132,7 +164,11 @@ public function getLine($data)
132164 public function getNestedData ($ data , $ keys , $ k )
133165 {
134166 foreach ($ keys as $ kk => $ key ) {
135- $ data = isset ($ data ->{$ k }->{$ key }) ? $ data ->{$ k }->{$ 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+ }
136172
137173 if (is_array ($ data )) {
138174 $ this ->getNestedData ($ data , $ key , $ kk );
@@ -151,9 +187,9 @@ protected function setDelimiter(string $delimiter): void
151187 }
152188
153189 /**
154- * @param Builder $model
190+ * @param object | array $model
155191 */
156- protected function setModel (Builder $ model ): void
192+ protected function setModel ($ model ): void
157193 {
158194 $ this ->model = $ model ;
159195 }
0 commit comments