1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LVR\State; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Validation\Rule; |
6
|
|
|
use Illuminate\Support\Facades\Validator; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
abstract class Base implements Rule |
10
|
|
|
{ |
11
|
|
|
protected $country; |
12
|
|
|
protected $subject; |
13
|
|
|
|
14
|
15 |
|
function __construct(string $country = null) |
|
|
|
|
15
|
|
|
{ |
16
|
15 |
|
$v = Validator::make([ |
17
|
15 |
|
"country" => $country, |
18
|
|
|
], [ |
19
|
15 |
|
"country" => "nullable|string|in:US,CA,MX", |
20
|
|
|
]); |
21
|
|
|
|
22
|
15 |
|
if ($v->fails()) { |
23
|
3 |
|
throw new \Exception($v->errors()->first(), 1); |
24
|
|
|
} |
25
|
|
|
|
26
|
12 |
|
$this->country = $country; |
27
|
12 |
|
$this->subject = $this->getSubject($this->country); |
28
|
12 |
|
} |
29
|
|
|
|
30
|
12 |
|
protected function isFull($value, string $country = null): bool |
31
|
|
|
{ |
32
|
12 |
|
return in_array(Str::title($value), $this->getStateNames($country)); |
33
|
|
|
} |
34
|
|
|
|
35
|
12 |
|
protected function isAbbr($value, string $country = null): bool |
36
|
|
|
{ |
37
|
12 |
|
return in_array(Str::upper($value), $this->getStateAbbreviations($country)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected $states = [ |
41
|
|
|
"US" => [ |
42
|
|
|
["abbr" => 'AL', "name" => 'Alabama'], |
43
|
|
|
["abbr" => 'AK', "name" => 'Alaska'], |
44
|
|
|
["abbr" => 'AZ', "name" => 'Arizona'], |
45
|
|
|
["abbr" => 'AR', "name" => 'Arkansas'], |
46
|
|
|
["abbr" => 'CA', "name" => 'California'], |
47
|
|
|
["abbr" => 'CO', "name" => 'Colorado'], |
48
|
|
|
["abbr" => 'CT', "name" => 'Connecticut'], |
49
|
|
|
["abbr" => 'DC', "name" => 'District Of Columbia'], |
50
|
|
|
["abbr" => 'DE', "name" => 'Delaware'], |
51
|
|
|
["abbr" => 'FL', "name" => 'Florida'], |
52
|
|
|
["abbr" => 'GA', "name" => 'Georgia'], |
53
|
|
|
["abbr" => 'HI', "name" => 'Hawaii'], |
54
|
|
|
["abbr" => 'ID', "name" => 'Idaho'], |
55
|
|
|
["abbr" => 'IL', "name" => 'Illinois'], |
56
|
|
|
["abbr" => 'IN', "name" => 'Indiana'], |
57
|
|
|
["abbr" => 'IA', "name" => 'Iowa'], |
58
|
|
|
["abbr" => 'KS', "name" => 'Kansas'], |
59
|
|
|
["abbr" => 'KY', "name" => 'Kentucky'], |
60
|
|
|
["abbr" => 'LA', "name" => 'Louisiana'], |
61
|
|
|
["abbr" => 'ME', "name" => 'Maine'], |
62
|
|
|
["abbr" => 'MD', "name" => 'Maryland'], |
63
|
|
|
["abbr" => 'MA', "name" => 'Massachusetts'], |
64
|
|
|
["abbr" => 'MI', "name" => 'Michigan'], |
65
|
|
|
["abbr" => 'MN', "name" => 'Minnesota'], |
66
|
|
|
["abbr" => 'MS', "name" => 'Mississippi'], |
67
|
|
|
["abbr" => 'MO', "name" => 'Missouri'], |
68
|
|
|
["abbr" => 'MT', "name" => 'Montana'], |
69
|
|
|
["abbr" => 'NE', "name" => 'Nebraska'], |
70
|
|
|
["abbr" => 'NV', "name" => 'Nevada'], |
71
|
|
|
["abbr" => 'NH', "name" => 'New Hampshire'], |
72
|
|
|
["abbr" => 'NJ', "name" => 'New Jersey'], |
73
|
|
|
["abbr" => 'NM', "name" => 'New Mexico'], |
74
|
|
|
["abbr" => 'NY', "name" => 'New York'], |
75
|
|
|
["abbr" => 'NC', "name" => 'North Carolina'], |
76
|
|
|
["abbr" => 'ND', "name" => 'North Dakota'], |
77
|
|
|
["abbr" => 'OH', "name" => 'Ohio'], |
78
|
|
|
["abbr" => 'OK', "name" => 'Oklahoma'], |
79
|
|
|
["abbr" => 'OR', "name" => 'Oregon'], |
80
|
|
|
["abbr" => 'PA', "name" => 'Pennsylvania'], |
81
|
|
|
["abbr" => 'RI', "name" => 'Rhode Island'], |
82
|
|
|
["abbr" => 'SC', "name" => 'South Carolina'], |
83
|
|
|
["abbr" => 'SD', "name" => 'South Dakota'], |
84
|
|
|
["abbr" => 'TN', "name" => 'Tennessee'], |
85
|
|
|
["abbr" => 'TX', "name" => 'Texas'], |
86
|
|
|
["abbr" => 'UT', "name" => 'Utah'], |
87
|
|
|
["abbr" => 'VT', "name" => 'Vermont'], |
88
|
|
|
["abbr" => 'VA', "name" => 'Virginia'], |
89
|
|
|
["abbr" => 'WA', "name" => 'Washington'], |
90
|
|
|
["abbr" => 'WV', "name" => 'West Virginia'], |
91
|
|
|
["abbr" => 'WI', "name" => 'Wisconsin'], |
92
|
|
|
["abbr" => 'WY', "name" => 'Wyoming'], |
93
|
|
|
["abbr" => 'AS', "name" => 'American Samoa'], |
94
|
|
|
["abbr" => 'FM', "name" => 'Federated States Of Micronesia'], |
95
|
|
|
["abbr" => 'GU', "name" => 'Guam'], |
96
|
|
|
["abbr" => 'MH', "name" => 'Marshall Islands'], |
97
|
|
|
["abbr" => 'MP', "name" => 'Northern Mariana Islands'], |
98
|
|
|
["abbr" => 'PW', "name" => 'Pala'], |
99
|
|
|
["abbr" => 'PR', "name" => 'Puerto Rico'], |
100
|
|
|
["abbr" => 'VI', "name" => 'Virgin Islands'] |
101
|
|
|
], |
102
|
|
|
"CA" => [ |
103
|
|
|
["abbr" => 'AB', "name" => 'Alberta'], |
104
|
|
|
["abbr" => 'BC', "name" => 'British Columbia'], |
105
|
|
|
["abbr" => 'MB', "name" => 'Manitoba'], |
106
|
|
|
["abbr" => 'NB', "name" => 'New Brunswick'], |
107
|
|
|
["abbr" => 'NL', "name" => 'Newfoundland And Labrador'], |
108
|
|
|
["abbr" => 'NS', "name" => 'Nova Scotia'], |
109
|
|
|
["abbr" => 'NT', "name" => 'Northwest Territories'], |
110
|
|
|
["abbr" => 'NU', "name" => 'Nunavut'], |
111
|
|
|
["abbr" => 'ON', "name" => 'Ontario'], |
112
|
|
|
["abbr" => 'PE', "name" => 'Prince Edward Island'], |
113
|
|
|
["abbr" => 'QC', "name" => 'Quebec'], |
114
|
|
|
["abbr" => 'SK', "name" => 'Saskatchewan'], |
115
|
|
|
["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"], |
150
|
|
|
], |
151
|
|
|
]; |
152
|
|
|
|
153
|
12 |
|
protected function getSubject(string $country = null): string |
154
|
|
|
{ |
155
|
8 |
|
switch($country) |
156
|
|
|
{ |
157
|
12 |
|
case "US": |
158
|
9 |
|
case "MX": |
159
|
6 |
|
return "State"; |
160
|
6 |
|
case "CA": |
161
|
3 |
|
return "Province"; |
162
|
|
|
default: |
163
|
3 |
|
return "State or Province"; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
12 |
View Code Duplication |
protected function getStateAbbreviations($country = null) |
|
|
|
|
168
|
|
|
{ |
169
|
12 |
|
$x = []; |
170
|
12 |
|
foreach ($this->states as $c => $states) { |
171
|
12 |
|
if ($country === null || $c === $country) { |
172
|
12 |
|
foreach ($states as $state) { |
173
|
12 |
|
$x[] = $state['abbr']; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
12 |
|
return $x; |
178
|
|
|
} |
179
|
|
|
|
180
|
12 |
View Code Duplication |
protected function getStateNames($country = null) |
|
|
|
|
181
|
|
|
{ |
182
|
12 |
|
$x = []; |
183
|
12 |
|
foreach ($this->states as $c => $states) { |
184
|
12 |
|
if ($country === null || $c === $country) { |
185
|
12 |
|
foreach ($states as $state) { |
186
|
12 |
|
$x[] = $state['name']; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
12 |
|
return $x; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Determine if the validation rule passes. |
195
|
|
|
* |
196
|
|
|
* @param string $attribute |
197
|
|
|
* @param mixed $value |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
abstract public function passes($attribute, $value); |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get the validation error message. |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
abstract public function message(); |
208
|
|
|
} |
209
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.