From 439d9c6414e9959865eb0ddeec023072c7b3ee67 Mon Sep 17 00:00:00 2001 From: Micheal Mand Date: Sat, 23 Mar 2019 11:47:21 -0600 Subject: [PATCH] Add Mexico states Since you're already doing Canada, why not Mexico too? Data from: https://www.iso.org/obp/ui/#iso:code:3166:MX Signed-off-by: Micheal Mand --- README.md | 10 +++++++--- src/Base.php | 37 ++++++++++++++++++++++++++++++++++++- tests/ValidatorTest.php | 21 +++++++++++++++++++-- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fdab29e..ea39379 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # State -Validate US States and Canada Provinces +Validate US States, Canada Provinces, and Mexico States

@@ -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! ``` diff --git a/src/Base.php b/src/Base.php index 91d1dc0..5e15d5a 100644 --- a/src/Base.php +++ b/src/Base.php @@ -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()) { @@ -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"], ], ]; @@ -121,6 +155,7 @@ protected function getSubject(string $country = null): string switch($country) { case "US": + case "MX": return "State"; case "CA": return "Province"; diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 5fb2f1c..4f2e867 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -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) @@ -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;