Skip to content

Commit

Permalink
barebones package done
Browse files Browse the repository at this point in the history
  • Loading branch information
jericdei committed Sep 11, 2024
1 parent 41cfc29 commit ecbfeb8
Show file tree
Hide file tree
Showing 18 changed files with 529 additions and 43 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
],
"require": {
"php": "^8.2",
"illuminate/contracts": "^10.0||^11.0",
"spatie/laravel-package-tools": "^1.16",
"illuminate/contracts": "^10.0||^11.0"
"spatie/simple-excel": "^3.6",
"symfony/dom-crawler": "^7.1"
},
"require-dev": {
"laravel/pint": "^1.14",
Expand Down Expand Up @@ -81,4 +83,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
34 changes: 34 additions & 0 deletions database/migrations/create_barangays_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('barangays', function (Blueprint $table) {
$table->string('code')->primary();
$table->string('old_code')->nullable();
$table->string('region_code');
$table->string('province_code');
$table->string('municipality_code');
$table->string('sub_municipality_code');
$table->string('barangay_code');
$table->string('name');
$table->string('old_name')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('barangays');
}
};
32 changes: 32 additions & 0 deletions database/migrations/create_cities_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('cities', function (Blueprint $table) {
$table->string('code')->primary();
$table->string('old_code')->nullable();
$table->string('region_code');
$table->string('province_code');
$table->string('city_code');
$table->string('name');
$table->string('old_name')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('cities');
}
};
32 changes: 32 additions & 0 deletions database/migrations/create_municipalities_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('municipalities', function (Blueprint $table) {
$table->string('code')->primary();
$table->string('old_code')->nullable();
$table->string('region_code');
$table->string('province_code');
$table->string('municipality_code');
$table->string('name');
$table->string('old_name')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('municipalities');
}
};
31 changes: 31 additions & 0 deletions database/migrations/create_provinces_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('provinces', function (Blueprint $table) {
$table->string('code')->primary();
$table->string('old_code')->nullable();
$table->string('region_code');
$table->string('province_code');
$table->string('name');
$table->string('old_name')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('provinces');
}
};
19 changes: 0 additions & 19 deletions database/migrations/create_psgc_database_table.php.stub

This file was deleted.

30 changes: 30 additions & 0 deletions database/migrations/create_regions_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('regions', function (Blueprint $table) {
$table->string('code')->primary();
$table->string('old_code')->nullable();
$table->string('region_code');
$table->string('name');
$table->string('old_name')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('regions');
}
};
33 changes: 33 additions & 0 deletions database/migrations/create_sub_municipalities_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('sub_municipalities', function (Blueprint $table) {
$table->string('code')->primary();
$table->string('old_code')->nullable();
$table->string('region_code');
$table->string('province_code');
$table->string('municipality_code');
$table->string('sub_municipality_code');
$table->string('name');
$table->string('old_name')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('sub_municipalities');
}
};
121 changes: 121 additions & 0 deletions src/Commands/ConvertToDatabaseCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Jericdei\PsgcDatabase\Commands;

use Jericdei\PsgcDatabase\Models\Barangay;
use Jericdei\PsgcDatabase\Models\City;
use Jericdei\PsgcDatabase\Models\Municipality;
use Jericdei\PsgcDatabase\Models\Province;
use Jericdei\PsgcDatabase\Models\Region;
use Jericdei\PsgcDatabase\Models\SubMunicipality;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Spatie\SimpleExcel\SimpleExcelReader;

use function Laravel\Prompts\confirm;

class ConvertToDatabaseCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'psgc:convert';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Convert PSGC excel data to database.';

/**
* Execute the console command.
*/
public function handle()
{
if (Region::count() !== 0) {
$confirm = confirm('PSGC database is not empty. Do you want to remove all data?');

if (!$confirm) {
$this->error('Action cancelled.');

return;
}

DB::table('regions')->truncate();
DB::table('provinces')->truncate();
DB::table('cities')->truncate();
DB::table('municipalities')->truncate();
DB::table('sub_municipalities')->truncate();
DB::table('barangays')->truncate();

$this->info('All data has been removed.');
}

$file = storage_path("app/public/psgc/latest.xlsx");

if (!File::exists($file)) {
$this->info('Downloading latest PSGC file...');
$this->call('psgc:dl-latest');
}

$this->info('Reading PSGC Excel file...');

$worksheet = Cache::rememberForever(
"psgc-latest",
fn() => SimpleExcelReader::create($file)->fromSheetName('PSGC')->getRows()->toArray()
);

array_shift($worksheet);

$this->info('Writing to database...');

// Guide
// 0 -> code
// 1 -> name
// 2 -> old_code
// 3 -> type
// 4 -> old_name
foreach ($worksheet as $row) {
$code = $row['10-digit PSGC'];

if ($code === null) {
continue;
}

$type = strtolower($row['Geographic Level']);

$data = [
'code' => $code,
'old_code' => $row['Correspondence Code'],
'region_code' => mb_substr($code, 0, 2),
'province_code' => mb_substr($code, 2, 3),
'municipality_code' => mb_substr($code, 2, 5),
'city_code' => mb_substr($code, 2, 5),
'sub_municipality_code' => mb_substr($code, 5, 2),
'barangay_code' => mb_substr($code, 2, 8),
'name' => trim($row['Name']),
'old_name' => $row['Old names'] ? trim($row['Old names']) : null,
];

DB::transaction(fn() => match ($type) {
'reg' => Region::create($data),
'prov' => Province::create($data),
'mun' => Municipality::create($data),
'city' => City::create($data),
'submun' => SubMunicipality::create($data),
'bgy' => Barangay::create($data),
default => null,
});

$this->info("Saved to database: $type - {$data['name']}");
}

$this->newLine(2);
$this->info('Done~!');
}
}
Loading

0 comments on commit ecbfeb8

Please sign in to comment.