Skip to content

Commit

Permalink
Added ProvinceSeeders registry and interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Nov 21, 2024
1 parent cb2c49b commit 3a5c6b1
Show file tree
Hide file tree
Showing 17 changed files with 302 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Address Module Changelog

## Unreleased
##### 2024-XX-YY

- Added the konekt/xtend requirement
- Added the `ProvinceSeeders` registry
- Added the `ProvinceSeeder` interface and decorated all existing province seeders with it

## 3.3.2
##### 2024-10-22

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"doctrine/dbal": "^3.5.1|^4.0",
"konekt/concord": "^1.13",
"konekt/enum": "^4.1",
"konekt/enum-eloquent": "^1.9"
"konekt/enum-eloquent": "^1.9",
"konekt/xtend": "^1.2"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 16 additions & 0 deletions src/Contracts/ProvinceSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Konekt\Address\Contracts;

interface ProvinceSeeder
{
public static function getCountryCode(): string;

/**
* @return array|ProvinceType[]
*/
public static function getProvinceTypes(): array;

}
33 changes: 33 additions & 0 deletions src/Providers/ModuleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
use Konekt\Address\Models\ZoneMemberType;
use Konekt\Address\Models\ZoneProxy;
use Konekt\Address\Models\ZoneScope;
use Konekt\Address\Seeds\CountiesOfHungary;
use Konekt\Address\Seeds\CountiesOfRomania;
use Konekt\Address\Seeds\ProvincesAndRegionsOfBelgium;
use Konekt\Address\Seeds\ProvincesAndTerritoriesOfCanada;
use Konekt\Address\Seeds\ProvincesAndTerritoriesOfCanadaFrench;
use Konekt\Address\Seeds\ProvinceSeeders;
use Konekt\Address\Seeds\ProvincesOfIndonesia;
use Konekt\Address\Seeds\ProvincesOfNetherlands;
use Konekt\Address\Seeds\StatesAndTerritoriesOfIndia;
use Konekt\Address\Seeds\StatesOfGermany;
use Konekt\Address\Seeds\StatesOfUsa;
use Konekt\Concord\BaseModuleServiceProvider;

class ModuleServiceProvider extends BaseModuleServiceProvider
Expand All @@ -54,6 +65,28 @@ class ModuleServiceProvider extends BaseModuleServiceProvider
ZoneMemberType::class,
];

protected static array $provinceSeeders = [
CountiesOfHungary::class,
CountiesOfRomania::class,
ProvincesAndRegionsOfBelgium::class,
ProvincesAndTerritoriesOfCanada::class,
ProvincesAndTerritoriesOfCanadaFrench::class,
ProvincesOfIndonesia::class,
ProvincesOfNetherlands::class,
StatesAndTerritoriesOfIndia::class,
StatesOfGermany::class,
StatesOfUsa::class,
];

public function register(): void
{
parent::register();

foreach (self::$provinceSeeders as $provinceSeeder) {
ProvinceSeeders::extend($provinceSeeder);
}
}

public function boot()
{
parent::boot();
Expand Down
9 changes: 8 additions & 1 deletion src/resources/database/seeds/CountiesOfHungary.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
namespace Konekt\Address\Seeds;

use Illuminate\Database\Seeder;
use Konekt\Address\Contracts\ProvinceSeeder;
use Konekt\Address\Models\ProvinceType;

class CountiesOfHungary extends Seeder
class CountiesOfHungary extends Seeder implements ProvinceSeeder
{
use IsProvinceSeeder;

protected static string $forCountry = 'HU';
protected static array $provinceTypes = [ProvinceType::COUNTY];

public function run()
{
\DB::table('provinces')->insert([
Expand Down
9 changes: 8 additions & 1 deletion src/resources/database/seeds/CountiesOfRomania.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
namespace Konekt\Address\Seeds;

use Illuminate\Database\Seeder;
use Konekt\Address\Contracts\ProvinceSeeder;
use Konekt\Address\Models\ProvinceType;

class CountiesOfRomania extends Seeder
class CountiesOfRomania extends Seeder implements ProvinceSeeder
{
use IsProvinceSeeder;

protected static string $forCountry = 'RO';
protected static array $provinceTypes = [ProvinceType::COUNTY];

/**
* Inserts the counties of Romania into the provinces table
*
Expand Down
22 changes: 22 additions & 0 deletions src/resources/database/seeds/IsProvinceSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Konekt\Address\Seeds;

use Konekt\Address\Contracts\ProvinceType;
use Konekt\Address\Models\ProvinceTypeProxy;

trait IsProvinceSeeder
{
public static function getCountryCode(): string
{
return self::$forCountry;
}

public static function getProvinceTypes(): array
{
return array_map(fn(string|ProvinceType $type) => is_string($type) ? ProvinceTypeProxy::create($type): $type, self::$provinceTypes);
}

}
47 changes: 47 additions & 0 deletions src/resources/database/seeds/ProvinceSeeders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Konekt\Address\Seeds;

use InvalidArgumentException;
use Illuminate\Support\Str;
use Konekt\Address\Contracts\ProvinceSeeder;
use Konekt\Extend\Concerns\HasRegistry;
use Konekt\Extend\Concerns\RequiresClassOrInterface;
use Konekt\Extend\Contracts\Registry;

class ProvinceSeeders implements Registry
{
use HasRegistry;
use RequiresClassOrInterface;

protected static string $requiredInterface = ProvinceSeeder::class;

public static function make(string $id, array $parameters = []): object
{
if (null === $class = self::getClassOf($id)) {
throw new InvalidArgumentException(
"No type is registered with the id `$id`."
);
}

return app()->make($class, $parameters);
}

/** @return ProvinceSeeder[] */
public static function availableSeedersOfCountry(string $country): array
{
return array_filter(self::$registry, fn($class) => $class::getCountryCode() === $country);
}

public static function extend(string $seederClass): void
{
self::add(self::generateId($seederClass), $seederClass);
}

private static function generateId(string $class): string
{
return Str::snake(class_basename($class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
namespace Konekt\Address\Seeds;

use Illuminate\Database\Seeder;
use Konekt\Address\Contracts\ProvinceSeeder;
use Konekt\Address\Models\ProvinceType;

class ProvincesAndRegionsOfBelgium extends Seeder
class ProvincesAndRegionsOfBelgium extends Seeder implements ProvinceSeeder
{
use IsProvinceSeeder;

protected static string $forCountry = 'BE';
protected static array $provinceTypes = [ProvinceType::REGION, ProvinceType::PROVINCE];

public function run()
{
\DB::table('provinces')->insert([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Konekt\Address\Contracts\ProvinceSeeder;
use Konekt\Address\Models\ProvinceType;

class ProvincesAndTerritoriesOfCanada extends Seeder
class ProvincesAndTerritoriesOfCanada extends Seeder implements ProvinceSeeder
{
use IsProvinceSeeder;

protected static string $forCountry = 'CA';
protected static array $provinceTypes = [ProvinceType::PROVINCE, ProvinceType::TERRITORY];

public function run()
{
DB::table('provinces')->insert([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Konekt\Address\Contracts\ProvinceSeeder;
use Konekt\Address\Models\ProvinceType;

class ProvincesAndTerritoriesOfCanadaFrench extends Seeder
class ProvincesAndTerritoriesOfCanadaFrench extends Seeder implements ProvinceSeeder
{
use IsProvinceSeeder;

protected static string $forCountry = 'CA';
protected static array $provinceTypes = [ProvinceType::PROVINCE, ProvinceType::TERRITORY];

public function run()
{
DB::table('provinces')->insert([
Expand Down
9 changes: 8 additions & 1 deletion src/resources/database/seeds/ProvincesOfIndonesia.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
namespace Konekt\Address\Seeds;

use Illuminate\Database\Seeder;
use Konekt\Address\Contracts\ProvinceSeeder;
use Konekt\Address\Models\ProvinceType;

class ProvincesOfIndonesia extends Seeder
class ProvincesOfIndonesia extends Seeder implements ProvinceSeeder
{
use IsProvinceSeeder;

protected static string $forCountry = 'ID';
protected static array $provinceTypes = [ProvinceType::UNIT, ProvinceType::PROVINCE, ProvinceType::REGION];

public function run()
{
$this->createJavaWithProvinces();
Expand Down
10 changes: 9 additions & 1 deletion src/resources/database/seeds/ProvincesOfNetherlands.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@
namespace Konekt\Address\Seeds;

use Illuminate\Database\Seeder;
use Konekt\Address\Contracts\ProvinceSeeder;
use Konekt\Address\Models\ProvinceType;

class ProvincesOfNetherlands extends Seeder
class ProvincesOfNetherlands extends Seeder implements ProvinceSeeder
{
use IsProvinceSeeder;

protected static string $forCountry = 'NL';
protected static array $provinceTypes = [ProvinceType::PROVINCE];


public function run()
{
\DB::table('provinces')->insert([
Expand Down
8 changes: 7 additions & 1 deletion src/resources/database/seeds/StatesAndTerritoriesOfIndia.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
namespace Konekt\Address\Seeds;

use Illuminate\Database\Seeder;
use Konekt\Address\Contracts\ProvinceSeeder;
use Konekt\Address\Models\ProvinceType;

class StatesAndTerritoriesOfIndia extends Seeder
class StatesAndTerritoriesOfIndia extends Seeder implements ProvinceSeeder
{
use IsProvinceSeeder;

protected static string $forCountry = 'IN';
protected static array $provinceTypes = [ProvinceType::STATE, ProvinceType::TERRITORY];

public function run()
{
\DB::table('provinces')->insert([
Expand Down
9 changes: 8 additions & 1 deletion src/resources/database/seeds/StatesOfGermany.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
namespace Konekt\Address\Seeds;

use Illuminate\Database\Seeder;
use Konekt\Address\Contracts\ProvinceSeeder;
use Konekt\Address\Models\ProvinceType;

class StatesOfGermany extends Seeder
class StatesOfGermany extends Seeder implements ProvinceSeeder
{
use IsProvinceSeeder;

protected static string $forCountry = 'DE';
protected static array $provinceTypes = [ProvinceType::STATE];

public function run()
{
\DB::table('provinces')->insert([
Expand Down
8 changes: 7 additions & 1 deletion src/resources/database/seeds/StatesOfUsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
namespace Konekt\Address\Seeds;

use Illuminate\Database\Seeder;
use Konekt\Address\Contracts\ProvinceSeeder;
use Konekt\Address\Models\ProvinceType;

class StatesOfUsa extends Seeder
class StatesOfUsa extends Seeder implements ProvinceSeeder
{
use IsProvinceSeeder;

protected static string $forCountry = 'US';
protected static array $provinceTypes = [ProvinceType::STATE, ProvinceType::FEDERAL_DISTRICT, ProvinceType::MILITARY, ProvinceType::TERRITORY];

/**
* Inserts the states of USA into the provinces table
*
Expand Down
Loading

0 comments on commit 3a5c6b1

Please sign in to comment.