function load_zipcodes() { DB::schema('mock-data')->dropIfExists('zipcodes'); DB::schema('mock-data')->create('zipcodes', function ($table) { # Define some fields. $table->increments('id'); $table->string('zip', 10); $table->string('type', 20); $table->string('city', 50); $table->text('acceptable_cities'); $table->text('unacceptable_cities'); $table->string('state_code', 2); $table->string('state', 50); $table->string('county', 50); $table->string('timezone', 50); $table->string('area_codes', 50); $table->float('latitude'); $table->float('longitude'); $table->string('world_region', 50); $table->string('country', 50); $table->smallInteger('decomissioned'); $table->bigInteger('estimated_population'); $table->string('notes', 50); # Add some indexs. $table->index('state_code'); $table->index('state'); $table->index('zip'); $table->index('county'); }); // The zip code database only contains state codes - no state names. The state abbreviations file supplements this data. $abbreviations = file(get_filename('state_abbreviations.txt')); $state_lookup = []; foreach ($abbreviations as $x) { $row = unpack("A32name/A2code", trim($x)); $state_lookup[trim($row['code'])] = ucfirst(strtolower(trim($row['name']))); } $fp = fopen(get_filename('zip_code_database.csv'), 'r'); $counter = 0; // Total number records processed. $loaded = 0; // Number records actually loaded. while (!feof($fp)) { $counter++; list($zip, $type, $primary_city, $acceptable_cities, $unacceptable_cities, $state_code, $county, $timezone, $area_codes, $lat, $long, $world_region, $country, $decomissioned, $estimated_population, $notes) = fgetcsv($fp); // Skip heading row and everything but standard zip codes for the 50 states and DC if ($counter > 1 && $type == 'STANDARD' && !in_array($state_code, ['GU', 'PR', 'VI'])) { $loaded++; if (array_key_exists($state_code, $state_lookup)) { $state = $state_lookup[$state_code]; } else { $state = $state_code; } $zipcodes = new Zipcode(); $zipcodes->zip = $zip; $zipcodes->type = $type; $zipcodes->city = ucwords($primary_city); $zipcodes->acceptable_cities = $acceptable_cities; $zipcodes->unacceptable_cities = $unacceptable_cities; $zipcodes->state_code = $state_code; $zipcodes->state = ucwords($state); $zipcodes->county = str_replace(' County', '', $county); $zipcodes->timezone = $timezone; $zipcodes->area_codes = $area_codes; $zipcodes->latitude = $lat; $zipcodes->longitude = $long; $zipcodes->world_region = $world_region; $zipcodes->country = $country; $zipcodes->decomissioned = $decomissioned; $zipcodes->estimated_population = $estimated_population; $zipcodes->notes = $notes; $zipcodes->save(); } } return $loaded; }
/** * Return a phone number * * @param null $state_code * @param null $zip * @param bool $include_toll_free * @return string */ public function getPhone($state_code = null, $zip = null, $include_toll_free = false) { if (!empty($zip)) { $areacodes = Zipcode::where('zip', $zip)->orderByRaw(Database::random())->first()->area_codes; } else { // Get a random state if state not provided $state_code = !empty($state_code) ? $state_code : $this->getState()->code; // Get area codes appropriate for this state $areacodes = Zipcode::where('state_code', $state_code)->orderByRaw(Database::random())->first()->area_codes; } // Get list of valid area codes for the state/zip code $code_list = explode(',', $areacodes); // @codeCoverageIgnoreStart // Add some toll free numbers into the mix. if ($include_toll_free === true) { $code_list[] = 800; $code_list[] = 888; $code_list[] = 877; $code_list[] = 866; $code_list[] = 855; } // @codeCoverageIgnoreEnd // Get a random area code from valid area codes $areacode = $this->fromArray($code_list); $prefix = rand(100, 999); $number = rand(1, 9999); return $areacode . '-' . $prefix . '-' . str_pad($number, 4, '0', STR_PAD_LEFT); }