Skip to content

Commit

Permalink
Add insert activations
Browse files Browse the repository at this point in the history
  • Loading branch information
altwaireb committed Oct 2, 2024
1 parent 487df3b commit 5e9c58f
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 53 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ You can specify the activation of countries through the country code ISO2 or ISO
before processing the seed data in the config file. `config/world.php`
```php
return [
'insert_activations_only' => false,
'countries' => [
'activation' => [
'default' => true,
Expand Down Expand Up @@ -118,6 +119,30 @@ return [
],
];
```
If you need to insert the countries is activation , this insert only two Countries `( Albania , Argentina )` with States and Cities.
```php
return [
'insert_activations_only' => true,
'countries' => [
'activation' => [
'default' => true,
'only' => [
'iso2' => ['AL','AR'],
'iso3' => [],
],
'except' => [
'iso2' => [],
'iso3' => [],
],
],
'chunk_length' => 50,
],

...
];
```


This means that only these two countries and the states and cities affiliated with them will be activated.
+ Note: If activation only `iso2` an `iso3` are empty, the column is_active take the `default` value in config file.
+ Note: If Country is active, all States and Cities are active.
Expand Down
1 change: 1 addition & 0 deletions config/world.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// config for Altwaireb/World
return [
'insert_activations_only' => false,
'countries' => [
'activation' => [
'default' => true,
Expand Down
223 changes: 170 additions & 53 deletions database/seeders/BaseWorldSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ public function __construct(
protected World $serves
) {}

/**
* @throws \Altwaireb\World\Exceptions\IsoCodesIsEmptyException
*/
public function run(): void
{
try {
$this->serves->ensureIsInsertActivationsHasIsoCodes();
$this->serves->ensureIsoCodesIsValid();
$this->createCountries();
$this->createStates();
Expand All @@ -29,107 +33,220 @@ public function run(): void

protected function createCountries(): void
{
$countries = $this->serves->getCountries();
$countries = $this->getAllCountries();
$chunkLength = $this->serves->getCountriesChunkLength();

$this->command->info('Starting Seed Country Data ...');
$this->command->info('Starting Seed Countries Data ...');
$this->command->getOutput()->progressStart(count($countries));

foreach (array_chunk($countries, $chunkLength) as $chunk) {
foreach ($chunk as $country) {
Country::create([
'id' => $country->id,
'name' => $country->name,
'iso2' => $country->iso2,
'iso3' => $country->iso3,
'numeric_code' => $country->numeric_code,
'phonecode' => $country->phonecode,
'capital' => $country->capital,
'currency' => $country->currency,
'currency_name' => $country->currency_name,
'currency_symbol' => $country->currency_symbol,
'tld' => $country->tld,
'native' => $country->native,
'region' => $country->region,
'subregion' => $country->subregion,
'timezones' => $country->timezones,
'translations' => $country->translations,
'latitude' => $country->latitude,
'longitude' => $country->longitude,
'emoji' => $country->emoji,
'emojiU' => $country->emojiU,
'flag' => $country->flag,
'is_active' => $this->serves->isCountryActiveByIso2OrIso3(
if ($this->serves->IsInsertActivationsOnly() === true) {

if ($this->serves->isCountryActiveByIso2OrIso3(
iso2: $country->iso2,
iso3: $country->iso3
),
]);
) === true) {
$this->createCountry($country);
}
} else {
$this->createCountry($country);
}
}
$this->command->getOutput()->progressAdvance();
}

$this->command->getOutput()->progressFinish();
$this->command->info('Country Data Seeded has successful');
$this->command->info('Countries Data Seeded has successful');
$this->command->line('');
}

protected function createStates(): void
{
$states = $this->serves->getStates();
$states = $this->getAllStates();
$chunkLength = $this->serves->getStatesChunkLength();

$this->command->info('Starting Seed State Data ...');
$this->command->info('Starting Seed States Data ...');
$this->command->getOutput()->progressStart(count($states));

foreach (array_chunk($states, $chunkLength) as $chunk) {
foreach ($chunk as $state) {
State::create([
'id' => $state->id,
'name' => $state->name,
'country_id' => $state->country_id,
'latitude' => $state->latitude,
'longitude' => $state->longitude,
'is_active' => $this->serves->isStateActiveByCountryId(
if ($this->serves->IsInsertActivationsOnly() === true) {
if ($this->serves->isStateActiveByCountryId(
countryId: $state->country_id
),
]);
)) {
$this->createState($state);
}
} else {
$this->createState($state);
}

$this->command->getOutput()->progressAdvance();
}
}

$this->command->getOutput()->progressFinish();
$this->command->info('State Data Seeded has successful');
$this->command->info('States Data Seeded has successful');
$this->command->line('');

}

protected function createCities(): void
{
$cities = $this->serves->getCities();
$cities = $this->getAllCities();
$chunkLength = $this->serves->getCitiesChunkLength();

$this->command->info('Starting Seed City Data ...');
$this->command->info('Starting Seed Cities Data ...');
$this->command->getOutput()->progressStart(count($cities));

foreach (array_chunk($cities, $chunkLength) as $chunk) {
foreach ($chunk as $city) {
City::create([
'id' => $city->id,
'name' => $city->name,
'country_id' => $city->country_id,
'state_id' => $city->state_id,
'latitude' => $city->latitude,
'longitude' => $city->longitude,
'is_active' => $this->serves->isCityActiveByCountryId(
if ($this->serves->IsInsertActivationsOnly() === true) {
if ($this->serves->isCityActiveByCountryId(
countryId: $city->country_id
),
]);
) === true) {
$this->createCity($city);

}
} else {
$this->createCity($city);
}

$this->command->getOutput()->progressAdvance();
}
}

$this->command->getOutput()->progressFinish();
$this->command->info('City Data Seeded has successful');
$this->command->info('Cities Data Seeded has successful');

}

private function getAllCountries(): array
{
$allCountries = collect($this->serves->getCountries());
$idsCountriesActive = $this->serves->getIdsCountriesActive();

if ($this->serves->IsInsertActivationsOnly() === true) {
if (! empty($idsCountriesActive)) {
if (count($idsCountriesActive) === 1) {
$countries = $allCountries->where('id', $idsCountriesActive[0])->toArray();
} elseif (count($idsCountriesActive) > 1) {
$countries = $allCountries->whereIn('id', $idsCountriesActive)->toArray();
} else {
$countries = [];
}

return $countries;
}
}

return $allCountries->toArray();
}

private function getAllStates(): array
{
$allStates = collect($this->serves->getStates());

$idsCountriesActive = $this->serves->getIdsCountriesActive();

if ($this->serves->IsInsertActivationsOnly() === true) {
if (! empty($idsCountriesActive)) {
if (count($idsCountriesActive) === 1) {
$states = $allStates->where('country_id', $idsCountriesActive[0])->toArray();
} elseif (count($idsCountriesActive) > 1) {
$states = $allStates->whereIn('country_id', $idsCountriesActive)->toArray();

} else {
$states = [];
}

return $states;
}
}

return $allStates->toArray();
}

private function getAllCities(): array
{
$allCities = collect($this->serves->getCities());

$idsCountriesActive = $this->serves->getIdsCountriesActive();

if ($this->serves->IsInsertActivationsOnly() === true) {
if (! empty($idsCountriesActive)) {
if (count($idsCountriesActive) === 1) {
$cities = $allCities->where('country_id', $idsCountriesActive[0])->toArray();
} elseif (count($idsCountriesActive) > 1) {
$cities = $allCities->whereIn('country_id', $idsCountriesActive)->toArray();
} else {
$cities = [];
}

return $cities;
}
}

return $allCities->toArray();
}

protected function createCountry($country): void
{
Country::create([
'id' => $country->id,
'name' => $country->name,
'iso2' => $country->iso2,
'iso3' => $country->iso3,
'numeric_code' => $country->numeric_code,
'phonecode' => $country->phonecode,
'capital' => $country->capital,
'currency' => $country->currency,
'currency_name' => $country->currency_name,
'currency_symbol' => $country->currency_symbol,
'tld' => $country->tld,
'native' => $country->native,
'region' => $country->region,
'subregion' => $country->subregion,
'timezones' => $country->timezones,
'translations' => $country->translations,
'latitude' => $country->latitude,
'longitude' => $country->longitude,
'emoji' => $country->emoji,
'emojiU' => $country->emojiU,
'flag' => $country->flag,
'is_active' => $this->serves->isCountryActiveByIso2OrIso3(
iso2: $country->iso2,
iso3: $country->iso3
),
]);
}

protected function createState($state): void
{
State::create([
'id' => $state->id,
'name' => $state->name,
'country_id' => $state->country_id,
'latitude' => $state->latitude,
'longitude' => $state->longitude,
'is_active' => $this->serves->isStateActiveByCountryId(
countryId: $state->country_id
),
]);
}

protected function createCity(mixed $city): void
{
City::create([
'id' => $city->id,
'name' => $city->name,
'country_id' => $city->country_id,
'state_id' => $city->state_id,
'latitude' => $city->latitude,
'longitude' => $city->longitude,
'is_active' => $this->serves->isCityActiveByCountryId(
countryId: $city->country_id
),
]);
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/IsoCodesIsEmptyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Altwaireb\World\Exceptions;

use Exception;

class IsoCodesIsEmptyException extends Exception
{
public static function isEmpty(): IsoCodesIsEmptyException
{
return new self(message: "Iso Code Is Empty , please add ISO codes in config file 'config/world.php");
}
}
Loading

0 comments on commit 5e9c58f

Please sign in to comment.