Skip to content

Commit

Permalink
Merge pull request #2 from mikemand/patch-1
Browse files Browse the repository at this point in the history
Add Mexico states
  • Loading branch information
tylercd100 authored Mar 29, 2019
2 parents 2ee10c6 + 439d9c6 commit 2308017
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# State

Validate US States and Canada Provinces
Validate US States, Canada Provinces, and Mexico States

<p >
<a href="https://travis-ci.org/laravel-validation-rules/us-state">
Expand Down Expand Up @@ -35,15 +35,19 @@ use LVR\State\Full;
# Abbreviation vs Full
$request->validate(['test' => 'UT'], ['test' => new Abbr]); // Pass!
$request->validate(['test' => 'BC'], ['test' => new Abbr); // Pass!
$request->validate(['test' => 'SON'], ['test' => new Abbr); // Pass!
$request->validate(['test' => 'Utah'], ['test' => new Full]); // Pass!
$request->validate(['test' => 'Alberta'], ['test' => new Full]); // Pass!
$request->validate(['test' => 'Sonora'], ['test' => new Full]); // Pass!

# Abbreviation - USA vs Canada
# Abbreviation - USA vs Canada vs Mexico
$request->validate(['test' => 'UT'], ['test' => new Abbr]); // Pass!
$request->validate(['test' => 'UT'], ['test' => new Abbr('US')]); // Pass!
$request->validate(['test' => 'BC'], ['test' => new Abbr('CA')); // Pass!
$request->validate(['test' => 'SON'], ['test' => new Abbr('MX')); // Pass!

# Full - USA vs Canada
# Full - USA vs Canada vs Mexico
$request->validate(['test' => 'Utah'], ['test' => new Full('US')]); // Pass!
$request->validate(['test' => 'Alberta'], ['test' => new Full('CA')]); // Pass!
$request->validate(['test' => 'Sonora'], ['text' => new Full('MX')]); // Pass!
```
37 changes: 36 additions & 1 deletion src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function __construct(string $country = null)
$v = Validator::make([
"country" => $country,
], [
"country" => "nullable|string|in:US,CA",
"country" => "nullable|string|in:US,CA,MX",
]);

if ($v->fails()) {
Expand Down Expand Up @@ -113,6 +113,40 @@ protected function isAbbr($value, string $country = null): bool
["abbr" => 'QC', "name" => 'Quebec'],
["abbr" => 'SK', "name" => 'Saskatchewan'],
["abbr" => 'YT', "name" => 'Yukon'],
],
"MX" => [
["abbr" => "AGU", "name" => "Aguascalientes"],
["abbr" => "BCN", "name" => "Baja California"],
["abbr" => "BCS", "name" => "Baja California Sur"],
["abbr" => "CAM", "name" => "Campeche"],
["abbr" => "CHP", "name" => "Chiapas"],
["abbr" => "CHH", "name" => "Chihuahua"],
["abbr" => "CMX", "name" => "Ciudad de México"],
["abbr" => "COA", "name" => "Coahuila de Zaragoza"],
["abbr" => "COL", "name" => "Colima"],
["abbr" => "DUR", "name" => "Durango"],
["abbr" => "GUA", "name" => "Guanajuato"],
["abbr" => "GRO", "name" => "Guerrero"],
["abbr" => "HID", "name" => "Hidalgo"],
["abbr" => "JAL", "name" => "Jalisco"],
["abbr" => "MIC", "name" => "Michoacán de Ocampo"],
["abbr" => "MOR", "name" => "Morelos"],
["abbr" => "MEX", "name" => "México"],
["abbr" => "NAY", "name" => "Nayarit"],
["abbr" => "NLE", "name" => "Nuevo León"],
["abbr" => "OAX", "name" => "Oaxaca"],
["abbr" => "PUE", "name" => "Puebla"],
["abbr" => "QUE", "name" => "Querétaro"],
["abbr" => "ROO", "name" => "Quintana Roo"],
["abbr" => "SLP", "name" => "San Luis Potosí"],
["abbr" => "SIN", "name" => "Sinaloa"],
["abbr" => "SON", "name" => "Sonora"],
["abbr" => "TAB", "name" => "Tabasco"],
["abbr" => "TAM", "name" => "Tamaulipas"],
["abbr" => "TLA", "name" => "Tlaxcala"],
["abbr" => "VER", "name" => "Veracruz de Ignacio de la Llave"],
["abbr" => "YUC", "name" => "Yucatán"],
["abbr" => "ZAC", "name" => "Zacatecas"],
],
];

Expand All @@ -121,6 +155,7 @@ protected function getSubject(string $country = null): string
switch($country)
{
case "US":
case "MX":
return "State";
case "CA":
return "Province";
Expand Down
21 changes: 19 additions & 2 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ class ValidatorTest extends TestCase
'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY',
'AS', 'FM', 'GU', 'MH', 'MP', 'PW', 'PR', 'VI'],
'canada' => ['AB', 'BC', 'MB', 'NB', 'NL', 'NS', 'NT', 'NU', 'ON', 'PE', 'QC', 'SK', 'YT'],
'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'],
];

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',],
'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'],
];

protected function validate($value, Rule $rule)
Expand Down Expand Up @@ -68,7 +72,20 @@ public function testValidatorCanada()
$this->assertEquals(false, $this->validate('Utah', new Full("CA")));
$this->assertEquals(false, $this->validate('utah', new Full("CA")));
}


public function testValidatorMexico()
{
$this->assertEquals(true, $this->validateAbbrs('mexico', new Abbr("MX")));
$this->assertEquals(true, $this->validate('son', new Abbr("MX")));
$this->assertEquals(true, $this->validate('Sonora', new Full("MX")));
$this->assertEquals(true, $this->validate('sonora', new Full("MX")));

$this->assertEquals(false, $this->validateAbbrs('usa', new Abbr("MX")));
$this->assertEquals(false, $this->validate('ut', new Abbr("MX")));
$this->assertEquals(false, $this->validate('Utah', new Full("MX")));
$this->assertEquals(false, $this->validate('utah', new Full("MX")));
}

protected function validateAbbrs($country, $rule)
{
$x = true;
Expand Down

0 comments on commit 2308017

Please sign in to comment.