Skip to content

Commit 2308017

Browse files
authored
Merge pull request #2 from mikemand/patch-1
Add Mexico states
2 parents 2ee10c6 + 439d9c6 commit 2308017

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# State
22

3-
Validate US States and Canada Provinces
3+
Validate US States, Canada Provinces, and Mexico States
44

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

41-
# Abbreviation - USA vs Canada
43+
# Abbreviation - USA vs Canada vs Mexico
4244
$request->validate(['test' => 'UT'], ['test' => new Abbr]); // Pass!
4345
$request->validate(['test' => 'UT'], ['test' => new Abbr('US')]); // Pass!
4446
$request->validate(['test' => 'BC'], ['test' => new Abbr('CA')); // Pass!
47+
$request->validate(['test' => 'SON'], ['test' => new Abbr('MX')); // Pass!
4548

46-
# Full - USA vs Canada
49+
# Full - USA vs Canada vs Mexico
4750
$request->validate(['test' => 'Utah'], ['test' => new Full('US')]); // Pass!
4851
$request->validate(['test' => 'Alberta'], ['test' => new Full('CA')]); // Pass!
52+
$request->validate(['test' => 'Sonora'], ['text' => new Full('MX')]); // Pass!
4953
```

src/Base.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function __construct(string $country = null)
1616
$v = Validator::make([
1717
"country" => $country,
1818
], [
19-
"country" => "nullable|string|in:US,CA",
19+
"country" => "nullable|string|in:US,CA,MX",
2020
]);
2121

2222
if ($v->fails()) {
@@ -113,6 +113,40 @@ protected function isAbbr($value, string $country = null): bool
113113
["abbr" => 'QC', "name" => 'Quebec'],
114114
["abbr" => 'SK', "name" => 'Saskatchewan'],
115115
["abbr" => 'YT', "name" => 'Yukon'],
116+
],
117+
"MX" => [
118+
["abbr" => "AGU", "name" => "Aguascalientes"],
119+
["abbr" => "BCN", "name" => "Baja California"],
120+
["abbr" => "BCS", "name" => "Baja California Sur"],
121+
["abbr" => "CAM", "name" => "Campeche"],
122+
["abbr" => "CHP", "name" => "Chiapas"],
123+
["abbr" => "CHH", "name" => "Chihuahua"],
124+
["abbr" => "CMX", "name" => "Ciudad de México"],
125+
["abbr" => "COA", "name" => "Coahuila de Zaragoza"],
126+
["abbr" => "COL", "name" => "Colima"],
127+
["abbr" => "DUR", "name" => "Durango"],
128+
["abbr" => "GUA", "name" => "Guanajuato"],
129+
["abbr" => "GRO", "name" => "Guerrero"],
130+
["abbr" => "HID", "name" => "Hidalgo"],
131+
["abbr" => "JAL", "name" => "Jalisco"],
132+
["abbr" => "MIC", "name" => "Michoacán de Ocampo"],
133+
["abbr" => "MOR", "name" => "Morelos"],
134+
["abbr" => "MEX", "name" => "México"],
135+
["abbr" => "NAY", "name" => "Nayarit"],
136+
["abbr" => "NLE", "name" => "Nuevo León"],
137+
["abbr" => "OAX", "name" => "Oaxaca"],
138+
["abbr" => "PUE", "name" => "Puebla"],
139+
["abbr" => "QUE", "name" => "Querétaro"],
140+
["abbr" => "ROO", "name" => "Quintana Roo"],
141+
["abbr" => "SLP", "name" => "San Luis Potosí"],
142+
["abbr" => "SIN", "name" => "Sinaloa"],
143+
["abbr" => "SON", "name" => "Sonora"],
144+
["abbr" => "TAB", "name" => "Tabasco"],
145+
["abbr" => "TAM", "name" => "Tamaulipas"],
146+
["abbr" => "TLA", "name" => "Tlaxcala"],
147+
["abbr" => "VER", "name" => "Veracruz de Ignacio de la Llave"],
148+
["abbr" => "YUC", "name" => "Yucatán"],
149+
["abbr" => "ZAC", "name" => "Zacatecas"],
116150
],
117151
];
118152

@@ -121,6 +155,7 @@ protected function getSubject(string $country = null): string
121155
switch($country)
122156
{
123157
case "US":
158+
case "MX":
124159
return "State";
125160
case "CA":
126161
return "Province";

tests/ValidatorTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ class ValidatorTest extends TestCase
1717
'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY',
1818
'AS', 'FM', 'GU', 'MH', 'MP', 'PW', 'PR', 'VI'],
1919
'canada' => ['AB', 'BC', 'MB', 'NB', 'NL', 'NS', 'NT', 'NU', 'ON', 'PE', 'QC', 'SK', 'YT'],
20+
'mexico' => ['AGU', 'BCN', 'BCS', 'CAM', 'CHP', 'CHH', 'CMX', 'COA', 'COL', 'DUR', 'GUA',
21+
'GRO', 'HID', 'JAL', 'MIC', 'MOR', 'MEX', 'NAY', 'NLE', 'OAX', 'PUE', 'QUE', 'ROO',
22+
'SLP', 'SIN', 'SON', 'TAB', 'TAM', 'TLA', 'VER', 'YUC', 'ZAC'],
2023
];
2124

2225
protected $names = [
2326
'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'],
24-
'canada' => [ 'Alberta', 'British Columbia', 'Manitoba', 'New Brunswick', 'Newfoundland And Labrador', 'Nova Scotia', 'Northwest Territories', 'Nunavut', 'Ontario', 'Prince Edward Island', 'Quebec', 'Saskatchewan', 'Yukon',],
27+
'canada' => [ 'Alberta', 'British Columbia', 'Manitoba', 'New Brunswick', 'Newfoundland And Labrador', 'Nova Scotia', 'Northwest Territories', 'Nunavut', 'Ontario', 'Prince Edward Island', 'Quebec', 'Saskatchewan', 'Yukon'],
28+
'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'],
2529
];
2630

2731
protected function validate($value, Rule $rule)
@@ -68,7 +72,20 @@ public function testValidatorCanada()
6872
$this->assertEquals(false, $this->validate('Utah', new Full("CA")));
6973
$this->assertEquals(false, $this->validate('utah', new Full("CA")));
7074
}
71-
75+
76+
public function testValidatorMexico()
77+
{
78+
$this->assertEquals(true, $this->validateAbbrs('mexico', new Abbr("MX")));
79+
$this->assertEquals(true, $this->validate('son', new Abbr("MX")));
80+
$this->assertEquals(true, $this->validate('Sonora', new Full("MX")));
81+
$this->assertEquals(true, $this->validate('sonora', new Full("MX")));
82+
83+
$this->assertEquals(false, $this->validateAbbrs('usa', new Abbr("MX")));
84+
$this->assertEquals(false, $this->validate('ut', new Abbr("MX")));
85+
$this->assertEquals(false, $this->validate('Utah', new Full("MX")));
86+
$this->assertEquals(false, $this->validate('utah', new Full("MX")));
87+
}
88+
7289
protected function validateAbbrs($country, $rule)
7390
{
7491
$x = true;

0 commit comments

Comments
 (0)