1414class CsvSeeder extends Seeder
1515{
1616
17- /**
18- * DB table name
19- *
20- * @var string
21- */
22- public $ table ;
23-
24- /**
25- * CSV filename
26- *
27- * @var string
28- */
29- public $ filename ;
30-
31- /**
32- * DB field that to be hashed, most likely a password field.
33- * If your password has a different name, please overload this
34- * variable from our seeder class.
35- *
36- * @var string
37- */
38-
39- public $ hashable = 'password ' ;
40-
41- /**
42- * An SQL INSERT query will execute every time this number of rows
43- * are read from the CSV. Without this, large INSERTS will silently
44- * fail.
45- *
46- * @var int
47- */
48- public $ insert_chunk_size = 50 ;
49-
50- /**
51- * CSV delimiter (defaults to ,)
52- *
53- * @var string
54- */
55- public $ csv_delimiter = ', ' ;
17+ /**
18+ * DB table name
19+ *
20+ * @var string
21+ */
22+ public $ table ;
23+
24+ /**
25+ * CSV filename
26+ *
27+ * @var string
28+ */
29+ public $ filename ;
30+
31+ /**
32+ * DB field that to be hashed, most likely a password field.
33+ * If your password has a different name, please overload this
34+ * variable from our seeder class.
35+ *
36+ * @var string
37+ */
38+
39+ public $ hashable = 'password ' ;
40+
41+ /**
42+ * An SQL INSERT query will execute every time this number of rows
43+ * are read from the CSV. Without this, large INSERTS will silently
44+ * fail.
45+ *
46+ * @var int
47+ */
48+ public $ insert_chunk_size = 50 ;
49+
50+ /**
51+ * CSV delimiter (defaults to ,)
52+ *
53+ * @var string
54+ */
55+ public $ csv_delimiter = ', ' ;
5656
5757 /**
5858 * Number of rows to skip at the start of the CSV
@@ -85,27 +85,27 @@ class CsvSeeder extends Seeder
8585 public $ mapping = [];
8686
8787
88- /**
89- * Run DB seed
90- */
91- public function run ()
92- {
88+ /**
89+ * Run DB seed
90+ */
91+ public function run ()
92+ {
9393 $ this ->seedFromCSV ($ this ->filename , $ this ->csv_delimiter );
94- }
95-
96- /**
97- * Strip UTF-8 BOM characters from the start of a string
98- *
99- * @param string $text
100- * @return string String with BOM stripped
101- */
102- public function stripUtf8Bom ( $ text )
103- {
104- $ bom = pack ('H* ' ,'EFBBBF ' );
105- $ text = preg_replace ("/^ $ bom/ " , '' , $ text );
94+ }
95+
96+ /**
97+ * Strip UTF-8 BOM characters from the start of a string
98+ *
99+ * @param string $text
100+ * @return string String with BOM stripped
101+ */
102+ public function stripUtf8Bom ($ text )
103+ {
104+ $ bom = pack ('H* ' , 'EFBBBF ' );
105+ $ text = preg_replace ("/^ $ bom/ " , '' , $ text );
106106
107107 return $ text ;
108- }
108+ }
109109
110110 /**
111111 * Opens a CSV file and returns it as a resource
@@ -115,10 +115,9 @@ public function stripUtf8Bom( $text )
115115 */
116116 public function openCSV ($ filename )
117117 {
118- if ( !file_exists ($ filename ) || !is_readable ($ filename ) )
119- {
118+ if (!file_exists ($ filename ) || !is_readable ($ filename )) {
120119 Log::error ("CSV insert failed: CSV " . $ filename . " does not exist or is not readable. " );
121- return FALSE ;
120+ return false ;
122121 }
123122
124123 // check if file is gzipped
@@ -132,63 +131,61 @@ public function openCSV($filename)
132131 return $ handle ;
133132 }
134133
135- /**
136- * Collect data from a given CSV file and return as array
137- *
138- * @param string $filename
139- * @param string $deliminator
140- * @return array|bool
141- */
142- public function seedFromCSV ($ filename , $ deliminator = ", " )
143- {
134+ /**
135+ * Collect data from a given CSV file and return as array
136+ *
137+ * @param string $filename
138+ * @param string $deliminator
139+ * @return array|bool
140+ */
141+ public function seedFromCSV ($ filename , $ deliminator = ", " )
142+ {
144143 $ handle = $ this ->openCSV ($ filename );
145144
146145 // CSV doesn't exist or couldn't be read from.
147- if ( $ handle === FALSE )
146+ if ($ handle === false ) {
148147 return [];
148+ }
149149
150- $ header = NULL ;
151- $ row_count = 0 ;
152- $ data = [];
150+ $ header = null ;
151+ $ row_count = 0 ;
152+ $ data = [];
153153 $ mapping = $ this ->mapping ?: [];
154154 $ offset = $ this ->offset_rows ;
155155
156- while ( ($ row = fgetcsv ($ handle , 0 , $ deliminator )) !== FALSE )
157- {
156+ while (($ row = fgetcsv ($ handle , 0 , $ deliminator )) !== false ) {
158157 // Offset the specified number of rows
159158
160- while ( $ offset > 0 )
161- {
159+ while ($ offset > 0 ) {
162160 $ offset --;
163161 continue 2 ;
164162 }
165163
166164 // No mapping specified - grab the first CSV row and use it
167- if ( !$ mapping )
168- {
165+ if (!$ mapping ) {
169166 $ mapping = $ row ;
170167 $ mapping [0 ] = $ this ->stripUtf8Bom ($ mapping [0 ]);
171168
172169 // skip csv columns that don't exist in the database
173- foreach ($ mapping as $ index => $ fieldname ){
174- if (!DB ::getSchemaBuilder ()->hasColumn ($ this ->table , $ fieldname )){
175- array_pull ($ mapping , $ index );
170+ foreach ($ mapping as $ index => $ fieldname ) {
171+ if (!DB ::getSchemaBuilder ()->hasColumn ($ this ->table , $ fieldname )) {
172+ if (isset ($ mapping [$ index ])) {
173+ unset($ mapping [$ index ]);
174+ }
176175 }
177176 }
178- }
179- else
180- {
177+ } else {
181178 $ row = $ this ->readRow ($ row , $ mapping );
182179
183180 // insert only non-empty rows from the csv file
184- if ( !$ row )
181+ if (!$ row) {
185182 continue ;
183+ }
186184
187185 $ data [$ row_count ] = $ row ;
188186
189187 // Chunk size reached, insert
190- if ( ++$ row_count == $ this ->insert_chunk_size )
191- {
188+ if (++$ row_count == $ this ->insert_chunk_size ) {
192189 $ this ->insert ($ data );
193190 $ row_count = 0 ;
194191 // clear the data array explicitly when it was inserted so
@@ -201,13 +198,14 @@ public function seedFromCSV($filename, $deliminator = ",")
201198
202199 // Insert any leftover rows
203200 //check if the data array explicitly if there are any values left to be inserted, if insert them
204- if ( count ($ data ) )
201+ if (count ($ data )) {
205202 $ this ->insert ($ data );
203+ }
206204
207205 fclose ($ handle );
208206
209- return $ data ;
210- }
207+ return $ data ;
208+ }
211209
212210 /**
213211 * Read a CSV row into a DB insertable array
@@ -216,15 +214,14 @@ public function seedFromCSV($filename, $deliminator = ",")
216214 * @param array $mapping Array of csvCol => dbCol
217215 * @return array
218216 */
219- public function readRow ( array $ row , array $ mapping )
217+ public function readRow (array $ row , array $ mapping )
220218 {
221219 $ row_values = [];
222220
223221 foreach ($ mapping as $ csvCol => $ dbCol ) {
224222 if (!isset ($ row [$ csvCol ]) || $ row [$ csvCol ] === '' ) {
225- $ row_values [$ dbCol ] = NULL ;
226- }
227- else {
223+ $ row_values [$ dbCol ] = null ;
224+ } else {
228225 $ row_values [$ dbCol ] = $ this ->should_trim ? trim ($ row [$ csvCol ]) : $ row [$ csvCol ];
229226 }
230227 }
@@ -242,16 +239,15 @@ public function readRow( array $row, array $mapping )
242239 * @param array $seedData
243240 * @return bool TRUE on success else FALSE
244241 */
245- public function insert ( array $ seedData )
246- {
247- try {
242+ public function insert (array $ seedData )
243+ {
244+ try {
248245 DB ::table ($ this ->table )->insert ($ seedData );
249- } catch (\Exception $ e ) {
246+ } catch (\Exception $ e ) {
250247 Log::error ("CSV insert failed: " . $ e ->getMessage () . " - CSV " . $ this ->filename );
251- return FALSE ;
252- }
253-
254- return TRUE ;
255- }
248+ return false ;
249+ }
256250
251+ return true ;
252+ }
257253}
0 commit comments