Skip to content

Commit

Permalink
Merge pull request #6 from jeddtony/master
Browse files Browse the repository at this point in the history
Add validation for Nigerian states
  • Loading branch information
ssx authored Apr 16, 2024
2 parents 46ac937 + 2effc85 commit 9b1a1bd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
45 changes: 43 additions & 2 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function __construct(string $country = null)
$v = Validator::make([
"country" => $country,
], [
"country" => "nullable|string|in:US,CA,MX,BR",

"country" => "nullable|string|in:US,CA,MX,BR,NG",
]);

if ($v->fails()) {
Expand Down Expand Up @@ -177,6 +178,45 @@ protected function isAbbr($value, string $country = null): bool
["abbr" => 'SE', "name" => 'Sergipe'],
["abbr" => 'TO', "name" => 'Tocantins'],
],
"NG" => [
["abbr" => "AB", "name" => "Abia"],
["abbr" => "AD", "name" => "Adamawa"],
["abbr" => "AK", "name" => "Akwa Ibom"],
["abbr" => "AN", "name" => "Anambra"],
["abbr" => "BA", "name" => "Bauchi"],
["abbr" => "BY", "name" => "Bayelsa"],
["abbr" => "BE", "name" => "Benue"],
["abbr" => "BO", "name" => "Borno"],
["abbr" => "CR", "name" => "Cross River"],
["abbr" => "DE", "name" => "Delta"],
["abbr" => "EB", "name" => "Ebonyi"],
["abbr" => "ED", "name" => "Edo"],
["abbr" => "EK", "name" => "Ekiti"],
["abbr" => "EN", "name" => "Enugu"],
["abbr" => "FC", "name" => "Federal Capital Territory"],
["abbr" => "GO", "name" => "Gombe"],
["abbr" => "IM", "name" => "Imo"],
["abbr" => "JI", "name" => "Jigawa"],
["abbr" => "KD", "name" => "Kaduna"],
["abbr" => "KN", "name" => "Kano"],
["abbr" => "KT", "name" => "Katsina"],
["abbr" => "KE", "name" => "Kebbi"],
["abbr" => "KO", "name" => "Kogi"],
["abbr" => "LA", "name" => "Lagos"],
["abbr" => "NA", "name" => "Nasarawa"],
["abbr" => "NI", "name" => "Niger"],
["abbr" => "OG", "name" => "Ogun"],
["abbr" => "ON", "name" => "Ondo"],
["abbr" => "OS", "name" => "Osun"],
["abbr" => "OY", "name" => "Oyo"],
["abbr" => "PL", "name" => "Plateau"],
["abbr" => "RI", "name" => "Rivers"],
["abbr" => "SO", "name" => "Sokoto"],
["abbr" => "TA", "name" => "Taraba"],
["abbr" => "YO", "name" => "Yobe"],
["abbr" => "ZA", "name" => "Zamfara"],

]
];

protected function getSubject(string $country = null): string
Expand All @@ -185,7 +225,8 @@ protected function getSubject(string $country = null): string
{
case "US":
case "MX":
case "BR":
case "BR":
case "NG":
return "State";
case "CA":
return "Province";
Expand Down
18 changes: 17 additions & 1 deletion tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ class ValidatorTest extends TestCase
'mexico' => ['AGU', 'BCN', 'BCS', 'CAM', 'CHP', 'CHH', 'CMX', 'COA', 'COL', 'DUR', 'GUA',
'GRO', 'HID', 'JAL', 'MIC', 'MOR', 'MEX', 'NAY', 'NLE', 'OAX', 'PUE', 'QUE', 'ROO',
'SLP', 'SIN', 'SON', 'TAB', 'TAM', 'TLA', 'VER', 'YUC', 'ZAC'],
];
'nigeria' => ['AB', 'AD', 'AK', 'AN', 'BA', 'BY', 'BE', 'BO', 'CR', 'DE', 'EB', 'ED', 'EK',
'EN', 'FC', 'GO', 'IM', 'JI', 'KD', 'KN', 'KT', 'KE', 'KO', 'LA', 'NA', 'NI', 'OG', 'ON',
'OS', 'OY', 'PL', 'RI', 'SO', 'TA', 'YO', 'ZA'],
];

protected $names = [
'usa' => ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'District Of Columbia', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming', 'American Samoa', 'Federated States Of Micronesia', 'Guam', 'Marshall Islands', 'Northern Mariana Islands', 'Pala', 'Puerto Rico', 'Virgin Islands'],
'canada' => [ 'Alberta', 'British Columbia', 'Manitoba', 'New Brunswick', 'Newfoundland And Labrador', 'Nova Scotia', 'Northwest Territories', 'Nunavut', 'Ontario', 'Prince Edward Island', 'Quebec', 'Saskatchewan', 'Yukon'],
'mexico' => [ 'Aguascalientes', 'Baja California', 'Baja California Sur', 'Campeche', 'Chiapas', 'Chihuahua', 'Coahuila', 'Colima', 'Mexico City', 'Durango', 'Guanajuato', 'Guerrero', 'Hidalgo', 'Jalisco', 'México', 'Michoacán', 'Morelos', 'Nayarit', 'Nuevo León', 'Oaxaca', 'Puebla', 'Querétaro', 'Quintana Roo', 'San Luis Potosí', 'Sinaloa', 'Sonora', 'Tabasco', 'Tamaulipas', 'Tlaxcala', 'Veracruz', 'Yucatán', 'Zacatecas'],
'nigeria' => ['Abia', 'Adamawa', 'Akwa Ibom', 'Anambra', 'Bauchi', 'Bayelsa', 'Benue', 'Borno', 'Cross River', 'Delta', 'Ebonyi', 'Edo', 'Ekiti', 'Enugu', 'Federal Capital Territory', 'Gombe', 'Imo', 'Jigawa', 'Kaduna', 'Kano', 'Katsina', 'Kebbi', 'Kogi', 'Lagos', 'Nasarawa', 'Niger', 'Ogun', 'Ondo', 'Osun', 'Oyo', 'Plateau', 'Rivers', 'Sokoto', 'Taraba', 'Yobe', 'Zamfara']
];

protected function validate($value, Rule $rule)
Expand Down Expand Up @@ -86,6 +90,18 @@ public function testValidatorMexico()
$this->assertEquals(false, $this->validate('utah', new Full("MX")));
}

public function testValidatorNigeria()
{
$this->assertEquals(true, $this->validateAbbrs('nigeria', new Abbr("NG")));
$this->assertEquals(true, $this->validate('la', new Abbr("NG")));
$this->assertEquals(true, $this->validate('Lagos', new Full("NG")));
$this->assertEquals(true, $this->validate('lagos', new Full("NG")));

$this->assertEquals(false, $this->validateAbbrs('usa', new Abbr("NG")));
$this->assertEquals(false, $this->validate('ut', new Abbr("NG")));
$this->assertEquals(false, $this->validate('Utah', new Full("NG")));
$this->assertEquals(false, $this->validate('utah', new Full("NG")));
}
protected function validateAbbrs($country, $rule)
{
$x = true;
Expand Down

0 comments on commit 9b1a1bd

Please sign in to comment.