From 5e9c58f7f71189af7528a219195262f7f6c58f15 Mon Sep 17 00:00:00 2001 From: Abdulmajeed Altwaireb Date: Thu, 3 Oct 2024 01:25:59 +0300 Subject: [PATCH] Add insert activations --- README.md | 25 +++ config/world.php | 1 + database/seeders/BaseWorldSeeder.php | 223 +++++++++++++++----- src/Exceptions/IsoCodesIsEmptyException.php | 13 ++ src/World.php | 35 +++ 5 files changed, 244 insertions(+), 53 deletions(-) create mode 100644 src/Exceptions/IsoCodesIsEmptyException.php diff --git a/README.md b/README.md index 2bfed6a..1e5e5d0 100644 --- a/README.md +++ b/README.md @@ -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, @@ -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. diff --git a/config/world.php b/config/world.php index d30bf2a..bd50aa1 100644 --- a/config/world.php +++ b/config/world.php @@ -2,6 +2,7 @@ // config for Altwaireb/World return [ + 'insert_activations_only' => false, 'countries' => [ 'activation' => [ 'default' => true, diff --git a/database/seeders/BaseWorldSeeder.php b/database/seeders/BaseWorldSeeder.php index 5b9b809..5ee91f9 100644 --- a/database/seeders/BaseWorldSeeder.php +++ b/database/seeders/BaseWorldSeeder.php @@ -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(); @@ -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 + ), + ]); } } diff --git a/src/Exceptions/IsoCodesIsEmptyException.php b/src/Exceptions/IsoCodesIsEmptyException.php new file mode 100644 index 0000000..f5c2f4b --- /dev/null +++ b/src/Exceptions/IsoCodesIsEmptyException.php @@ -0,0 +1,13 @@ +