Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
composer.lock
vendor
.idea/
14 changes: 14 additions & 0 deletions lang/en/australia-states.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use AllDressed\Constants\AustraliaState;

return [
AustraliaState::AUSTRALIAN_CAPITAL_TERRITORY->value => 'Australian Capital Territory',
AustraliaState::NEW_SOUTH_WALES->value => 'New South Wales',
AustraliaState::NORTHERN_TERRITORY->value => 'Northern Territory',
AustraliaState::QUEENSLAND->value => 'Queensland',
AustraliaState::SOUTH_AUSTRALIA->value => 'South Australia',
AustraliaState::TASMANIA->value => 'Tasmania',
AustraliaState::VICTORIA->value => 'Victoria',
AustraliaState::WESTERN_AUSTRALIA->value => 'Western Australia',
];
19 changes: 19 additions & 0 deletions lang/en/canada-states.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use AllDressed\Constants\CanadaState;

return [
CanadaState::ALBERTA->value => 'Alberta',
CanadaState::BRITISH_COLUMBIA->value => 'British Columbia',
CanadaState::MANITOBA->value => 'Manitoba',
CanadaState::NEW_BRUNSWICK->value => 'New Brunswick',
CanadaState::NEWFOUNDLAND_AND_LABRADOR->value => 'Newfoundland and Labrador',
CanadaState::NOVA_SCOTIA->value => 'Nova Scotia',
CanadaState::NORTHWEST_TERRITORIES->value => 'Northwest Territories',
CanadaState::NUNAVUT->value => 'Nunavut',
CanadaState::ONTARIO->value => 'Ontario',
CanadaState::PRINCE_EDWARD_ISLAND->value => 'Prince Edward Island',
CanadaState::QUEBEC->value => 'Quebec',
CanadaState::SASKATCHEWAN->value => 'Saskatchewan',
CanadaState::YUKON->value => 'Yukon',
];
21 changes: 0 additions & 21 deletions lang/en/states.php

This file was deleted.

37 changes: 37 additions & 0 deletions src/AustraliaState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace AllDressed\Constants;

use AllDressed\Constants\Concerns\AvailableAsCollection;
use AllDressed\Constants\Concerns\AvailableAsOptions;
use AllDressed\Constants\Concerns\CanBeRandomized;

enum AustraliaState: string
{
use AvailableAsCollection, AvailableAsOptions, CanBeRandomized;

case AUSTRALIAN_CAPITAL_TERRITORY = 'ACT';
case NEW_SOUTH_WALES = 'NSW';
case NORTHERN_TERRITORY = 'NT';
case QUEENSLAND = 'QLD';
case SOUTH_AUSTRALIA = 'SA';
case TASMANIA = 'TAS';
case VICTORIA = 'VIC';
case WESTERN_AUSTRALIA = 'WA';

/**
* Retrieve the country of the state.
*/
public function getCountry(): string
{
return 'AU';
}

/**
* Retrieve the abbreviation of the state.
*/
public function getStateValue(): string
{
return $this->value;
}
}
47 changes: 47 additions & 0 deletions src/CanadaState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace AllDressed\Constants;

use AllDressed\Constants\Concerns\AvailableAsCollection;
use AllDressed\Constants\Concerns\AvailableAsOptions;
use AllDressed\Constants\Concerns\CanBeRandomized;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Lang;

enum CanadaState: string
{
use AvailableAsCollection, AvailableAsOptions, CanBeRandomized;

case ALBERTA = 'AL';
case BRITISH_COLUMBIA = 'BC';
case MANITOBA = 'MB';
case NEW_BRUNSWICK = 'NB';
case NEWFOUNDLAND_AND_LABRADOR = 'NL';
case NOVA_SCOTIA = 'NS';
case NORTHWEST_TERRITORIES = 'NT';
case NUNAVUT = 'NU';
case ONTARIO = 'ON';
case PRINCE_EDWARD_ISLAND = 'PE';
case QUEBEC = 'QC';
case SASKATCHEWAN = 'SK';
case YUKON = 'YK';


/**
* Retrieve the country of the state.
*/
public function getCountry(): string
{
return 'CA';
}

/**
* Retrieve the abbreviation of the state.
*/
public function getStateValue(): string
{
return $this->value;
}
}
50 changes: 7 additions & 43 deletions src/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,17 @@
use AllDressed\Constants\Concerns\CanBeRandomized;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Lang;

enum State: string
{
use AvailableAsCollection, AvailableAsOptions, CanBeRandomized;

case AUSTRALIAN_CAPITAL_TERRITORY = 'ACT';
case NEW_SOUTH_WALES = 'NSW';
case NORTHERN_TERRITORY = 'NT';
case QUEBEC = 'QC';
case QUEENSLAND = 'QLD';
case SOUTH_AUSTRALIA = 'SA';
case TASMANIA = 'TAS';
case VICTORIA = 'VIC';
case WESTERN_AUSTRALIA = 'WA';

/**
* Retrieve the country of the state.
*/
public function getCountry(): Country
{
$key = "alldressed-{$this->value}-country";

if (! App::has($key)) {
App::singleton($key, function () {
$country = collect(Lang::get('constants::states'))->search(
function ($states) {
return collect($states)->has($this->value);
}
);

return Country::from($country);
});
}

return App::get($key);
}

/**
* Preprend the country for the translation key.
* Get all states from all enums.
*/
public function getTranslationKey(): string
public static function all(): Collection
{
return "states.{$this->getCountry()->value}.{$this->value}";
return collect([...AustraliaState::cases(), ...CanadaState::cases()]);
}

/**
Expand All @@ -60,12 +26,10 @@ public function getTranslationKey(): string
public static function toCountrySelectorOptions(): Collection
{
return static::all()
->map(static fn ($state) => [
'country' => $state->getCountry()->value,
->groupBy(static fn ($states) => $states->getCountry())
->map(static fn ($states) => $states->map(static fn ($state) => [
'label' => $state->getLabel(),
'value' => $state->value,
])
->groupBy('country')
->map(static fn ($option) => Arr::except($option, 'country'));
]));
}
}
}