src/Console/CommandTrait.php
protected function parseGeonamesText($name, $refresh = false)
    {
        $fieldsArray = array(
            'allCountries' => function ($row) {
                return array(
                    'geoname_id' => $row[0],
                    'name' => $row[1],
                    'ascii_name' => $row[2] ? $row[2] : null,
                    'alternate_names' => $row[3] ? $row[3] : null,
                    'latitude' => $row[4],
                    'longitude' => $row[5],
                    'feature_class' => $row[6] ? $row[6] : null,
                    'feature_code' => $row[7] ? $row[7] : null,
                    'country_code' => $row[8],
                    'cc2' => $row[9] ? $row[9] : null,
                    'admin1_code' => $row[10] ? $row[10] : null,
                    'admin2_code' => $row[11] ? $row[11] : null,
                    'admin3_code' => $row[12] ? $row[12] : null,
                    'admin4_code' => $row[13] ? $row[13] : null,
                    'population' => $row[14] ? $row[14] : null,
                    'elevation' => $row[15] ? $row[15] : null,
                    'dem' => $row[16] ? $row[16] : null,
                    'timezone_id' => $row[17] ? $row[17] : null,
                    'modified_at' => trim($row[18]),
                );
            },
            'iso-languagecodes' => function ($row) {
                return array(
                    'iso_639_3' => $row[0],
                    'iso_639_2' => $row[1] ? $row[1] : null,
                    'iso_639_1' => $row[2] ? $row[2] : null,
                    'language_name' => trim($row[3]),
                );
            },
            'hierarchy' => function ($row) {
                return array(
                    'parent_id' => $row[0],
                    'child_id' => $row[1],
                    'type' => trim($row[2]),
                );
            },
            'alternateNames' => function ($row) {
                return array(
                    'alternate_name_id' => $row[0],
                    'geoname_id' => $row[1],
                    'iso_language' => $row[2] ? $row[2] : null,
                    'alternate_name' => $row[3] ? $row[3] : null,
                    'isPreferredName' => $row[4] ? 1 : 0,
                    'isShortName' => $row[5] ? 1 : 0,
                    'isColloquial' => $row[6] ? 1 : 0,
                    'isHistoric' => $row[7] ? 1 : 0,
                );
            },
            'timeZones' => function ($row) {
                // Skip unusual comment line in this file
                if ($row[0] === 'CountryCode') return null;
                return array(
                    'country_code' => $row[0],
                    'timezone_id' => $row[1],
                    'gmt_offset' => $row[2],
                    'dst_offset' => $row[3],
                    'raw_offset' => $row[4]
                );
            },
            'countryInfo' => function ($row) {
                return array(
                    'iso' => $row[0],
                    'iso3' => $row[1],
                    'iso_numeric' => $row[2],
                    'fips' => $row[3] ? $row[3] : null,
                    'country' => $row[4] ? $row[4] : null,
                    'capital' => $row[5] ? $row[5] : null,
                    'area' => $row[6] ? $row[6] : null,
                    'population' => $row[7] ? $row[7] : null,
                    'continent_code' => $row[8],
                    'continent_id' => $this->continentCodes[$row[8]],
                    'tld' => $row[9] ? $row[9] : null,
                    'currency_code' => $row[10] ? $row[10] : null,
                    'currency_name' => $row[11] ? $row[11] : null,
                    'phone' => $row[12] ? $row[12] : null,
                    'postal_code_format' => $row[13] ? $row[13] : null,
                    'postal_code_regex' => $row[14] ? $row[14] : null,
                    'languages' => $row[15] ? $row[15] : null,
                    'geoname_id' => $row[16] ? $row[16] : null,
                    'neighbors' => $row[17] ? $row[17] : null,
                    'equivalent_fips_code' => $row[18] ? trim($row[18]) : null,
                );
            },
            'featureCodes' => function ($row) {
                return array(
                    'code' => $row[0],
                    'name' => $row[1],
                    'description' => trim($row[2]),
                );
            },
            'admin1CodesASCII' => function ($row) {
                return array(
                    'code' => $row[0],
                    'name' => $row[1],
                    'name_ascii' => $row[2],
                    'geoname_id' => $row[3],
                );
            },
            'admin2Codes' => function ($row) {
                return array(
                    'code' => $row[0],
                    'name' => $row[1],
                    'name_ascii' => $row[2],
                    'geoname_id' => $row[3],
                );
            },
        );

        // This will greatly improve the performance of inserts
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        $tableName = $this->files[$name]['table'];
        // If table is empty or we are refreshing, truncate it unless it was recently truncated!
        if (!in_array($tableName,$this->truncatedTables) &&
            (DB::table($tableName)->count() === 0 || $refresh)
        ) {
            $this->truncatedTables[] = $tableName;
            $this->line('<info>Database:</info> Truncating table ' . $tableName);
            DB::table($tableName)->truncate();
        }
        $buffer = array();
        // If it is a custom country code, use allCountries
        if (in_array($name, config('geonames.countries')))
            $fields = $fieldsArray['allCountries'];
        else
            $fields = $fieldsArray[$name];
        $this->parseFile($name, function ($row) use (&$buffer, $fields, $tableName) {
            $insert = $fields($row);
            if (isset($insert) && is_array($insert)) {
                $buffer[] = $insert;
            }
            if (count($buffer) === $this->bufferSize) {
                $this->updateOrInsertMultiple($tableName, $buffer);
                $buffer = array();
            }
        });

        // Insert leftovers in buffer
        if (count($buffer) > 0)
            $this->updateOrInsertMultiple($tableName, $buffer);

        DB::statement('SET FOREIGN_KEY_CHECKS=1;');
    }
protected function parseGeonamesText($name, $refresh = false)
    {
        $fieldsArray = array(
            'allCountries' => function ($row) {
                return array(
                    'geoname_id' => $row[0],
                    'name' => $row[1],
                    'ascii_name' => $row[2] ? $row[2] : null,
                    'alternate_names' => $row[3] ? $row[3] : null,
                    'latitude' => $row[4],
                    'longitude' => $row[5],
                    'feature_class' => $row[6] ? $row[6] : null,
                    'feature_code' => $row[7] ? $row[7] : null,
                    'country_code' => $row[8],
                    'cc2' => $row[9] ? $row[9] : null,
                    'admin1_code' => $row[10] ? $row[10] : null,
                    'admin2_code' => $row[11] ? $row[11] : null,
                    'admin3_code' => $row[12] ? $row[12] : null,
                    'admin4_code' => $row[13] ? $row[13] : null,
                    'population' => $row[14] ? $row[14] : null,
                    'elevation' => $row[15] ? $row[15] : null,
                    'dem' => $row[16] ? $row[16] : null,
                    'timezone_id' => $row[17] ? $row[17] : null,
                    'modified_at' => trim($row[18]),
                );
            },
            'iso-languagecodes' => function ($row) {
                return array(
                    'iso_639_3' => $row[0],
                    'iso_639_2' => $row[1] ? $row[1] : null,
                    'iso_639_1' => $row[2] ? $row[2] : null,
                    'language_name' => trim($row[3]),
                );
            },
            'hierarchy' => function ($row) {
                return array(
                    'parent_id' => $row[0],
                    'child_id' => $row[1],
                    'type' => trim($row[2]),
                );
            },
            'alternateNames' => function ($row) {
                return array(
                    'alternate_name_id' => $row[0],
                    'geoname_id' => $row[1],
                    'iso_language' => $row[2] ? $row[2] : null,
                    'alternate_name' => $row[3] ? $row[3] : null,
                    'isPreferredName' => $row[4] ? 1 : 0,
                    'isShortName' => $row[5] ? 1 : 0,
                    'isColloquial' => $row[6] ? 1 : 0,
                    'isHistoric' => $row[7] ? 1 : 0,
                );
            },
            'timeZones' => function ($row) {
                // Skip unusual comment line in this file
                if ($row[0] === 'CountryCode') return null;
                return array(
                    'country_code' => $row[0],
                    'timezone_id' => $row[1],
                    'gmt_offset' => $row[2],
                    'dst_offset' => $row[3],
                    'raw_offset' => $row[4]
                );
            },
            'countryInfo' => function ($row) {
                return array(
                    'iso' => $row[0],
                    'iso3' => $row[1],
                    'iso_numeric' => $row[2],
                    'fips' => $row[3] ? $row[3] : null,
                    'country' => $row[4] ? $row[4] : null,
                    'capital' => $row[5] ? $row[5] : null,
                    'area' => $row[6] ? $row[6] : null,
                    'population' => $row[7] ? $row[7] : null,
                    'continent_code' => $row[8],
                    'continent_id' => $this->continentCodes[$row[8]],
                    'tld' => $row[9] ? $row[9] : null,
                    'currency_code' => $row[10] ? $row[10] : null,
                    'currency_name' => $row[11] ? $row[11] : null,
                    'phone' => $row[12] ? $row[12] : null,
                    'postal_code_format' => $row[13] ? $row[13] : null,
                    'postal_code_regex' => $row[14] ? $row[14] : null,
                    'languages' => $row[15] ? $row[15] : null,
                    'geoname_id' => $row[16] ? $row[16] : null,
                    'neighbors' => $row[17] ? $row[17] : null,
                    'equivalent_fips_code' => $row[18] ? trim($row[18]) : null,
                );
            },
            'featureCodes' => function ($row) {
                return array(
                    'code' => $row[0],
                    'name' => $row[1],
                    'description' => trim($row[2]),
                );
            },
            'admin1CodesASCII' => function ($row) {
                return array(
                    'code' => $row[0],
                    'name' => $row[1],
                    'name_ascii' => $row[2],
                    'geoname_id' => $row[3],
                );
            },
            'admin2Codes' => function ($row) {
                return array(
                    'code' => $row[0],
                    'name' => $row[1],
                    'name_ascii' => $row[2],
                    'geoname_id' => $row[3],
                );
            },
        );

        // This will greatly improve the performance of inserts
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        $tableName = $this->files[$name]['table'];
        // If table is empty or we are refreshing, truncate it unless it was recently truncated!
        if (!in_array($tableName,$this->truncatedTables) &&
            (DB::table($tableName)->count() === 0 || $refresh)
        ) {
            $this->truncatedTables[] = $tableName;
            $this->line('<info>Database:</info> Truncating table ' . $tableName);
            DB::table($tableName)->truncate();
        }
        $buffer = array();
        // If it is a custom country code, use allCountries
        if (in_array($name, config('geonames.countries')))
            $fields = $fieldsArray['allCountries'];
        else
            $fields = $fieldsArray[$name];
        $this->parseFile($name, function ($row) use (&$buffer, $fields, $tableName) {
            $insert = $fields($row);
            if (isset($insert) && is_array($insert)) {
                $buffer[] = $insert;
            }
            if (count($buffer) === $this->bufferSize) {
                $this->updateOrInsertMultiple($tableName, $buffer);
                $buffer = array();
            }
        });

        // Insert leftovers in buffer
        if (count($buffer) > 0)
            $this->updateOrInsertMultiple($tableName, $buffer);

        DB::statement('SET FOREIGN_KEY_CHECKS=1;');
    }
Parses the array created from different geonames file lines
and converts into key=>value type array

@param string $name The config name of the file
@param boolean $refresh Set true for truncating table before inserting rows