1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebTheory\Saveyour\Selections; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use WebTheory\Saveyour\Contracts\OptionsProviderInterface; |
7
|
|
|
|
8
|
|
|
class StateSelectOptions implements OptionsProviderInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
protected $groups = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
protected $text = 'name'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $reSort = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
public const STATES = [ |
29
|
|
|
'AL' => 'Alabama', |
30
|
|
|
'AK' => 'Alaska', |
31
|
|
|
'AZ' => 'Arizona', |
32
|
|
|
'AR' => 'Arkansas', |
33
|
|
|
'CA' => 'California', |
34
|
|
|
'CO' => 'Colorado', |
35
|
|
|
'CT' => 'Connecticut', |
36
|
|
|
'DE' => 'Delaware', |
37
|
|
|
'DC' => 'District of Columbia', |
38
|
|
|
'FL' => 'Florida', |
39
|
|
|
'GA' => 'Georgia', |
40
|
|
|
'HI' => 'Hawaii', |
41
|
|
|
'ID' => 'Idaho', |
42
|
|
|
'IL' => 'Illinois', |
43
|
|
|
'IN' => 'Indiana', |
44
|
|
|
'IA' => 'Iowa', |
45
|
|
|
'KS' => 'Kansas', |
46
|
|
|
'KY' => 'Kentucky', |
47
|
|
|
'LA' => 'Louisiana', |
48
|
|
|
'ME' => 'Maine', |
49
|
|
|
'MD' => 'Maryland', |
50
|
|
|
'MA' => 'Massachusetts', |
51
|
|
|
'MI' => 'Michigan', |
52
|
|
|
'MN' => 'Minnesota', |
53
|
|
|
'MS' => 'Mississippi', |
54
|
|
|
'MO' => 'Missouri', |
55
|
|
|
'MT' => 'Montana', |
56
|
|
|
'NE' => 'Nebraska', |
57
|
|
|
'NV' => 'Nevada', |
58
|
|
|
'NH' => 'New Hampshire', |
59
|
|
|
'NJ' => 'New Jersey', |
60
|
|
|
'NM' => 'New Mexico', |
61
|
|
|
'NY' => 'New York', |
62
|
|
|
'NC' => 'North Carolina', |
63
|
|
|
'ND' => 'North Dakota', |
64
|
|
|
'OH' => 'Ohio', |
65
|
|
|
'OK' => 'Oklahoma', |
66
|
|
|
'OR' => 'Oregon', |
67
|
|
|
'PA' => 'Pennsylvania', |
68
|
|
|
'RI' => 'Rhode Island', |
69
|
|
|
'SC' => 'South Carolina', |
70
|
|
|
'SD' => 'South Dakota', |
71
|
|
|
'TN' => 'Tennessee', |
72
|
|
|
'TX' => 'Texas', |
73
|
|
|
'UT' => 'Utah', |
74
|
|
|
'VT' => 'Vermont', |
75
|
|
|
'VA' => 'Virginia', |
76
|
|
|
'WA' => 'Washington', |
77
|
|
|
'WV' => 'West Virginia', |
78
|
|
|
'WI' => 'Wisconsin', |
79
|
|
|
'WY' => 'Wyoming', |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* |
84
|
|
|
*/ |
85
|
|
|
public const TERRITORIES = [ |
86
|
|
|
"AS" => "American Samoa", |
87
|
|
|
"GU" => "Guam", |
88
|
|
|
"MP" => "Northern Mariana Islands", |
89
|
|
|
"PR" => "Puerto Rico", |
90
|
|
|
"UM" => "United States Minor Outlying Islands", |
91
|
|
|
"VI" => "Virgin Islands", |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* |
96
|
|
|
*/ |
97
|
|
|
public const ARMED_FORCES = [ |
98
|
|
|
"AA" => "Armed Forces Americas", |
99
|
|
|
"AP" => "Armed Forces Pacific", |
100
|
|
|
"AE" => "Armed Forces Other", |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* |
105
|
|
|
*/ |
106
|
|
|
protected const MAP = [ |
107
|
|
|
'States' => self::STATES, |
108
|
|
|
'Territories' => self::TERRITORIES, |
109
|
|
|
'ArmedForces' => self::ARMED_FORCES |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* |
114
|
|
|
*/ |
115
|
|
|
protected const ALLOWED_GROUPS = ['States', 'Territories', 'ArmedForces']; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* |
119
|
|
|
*/ |
120
|
|
|
protected const ALLOWED_TEXT = ['abbr', 'name']; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* |
124
|
|
|
*/ |
125
|
3 |
|
public function __construct(array $groups = ['States'], string $text = 'name', $reSort = false) |
126
|
|
|
{ |
127
|
3 |
|
$this->setGroups($groups); |
128
|
|
|
$this->setText($text); |
129
|
|
|
|
130
|
|
|
$this->reSort = $reSort; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* |
135
|
|
|
*/ |
136
|
3 |
|
protected function setGroups(array $groups) |
137
|
|
|
{ |
138
|
3 |
|
foreach ($groups as $group) { |
139
|
3 |
|
if (!in_array($group, static::ALLOWED_GROUPS)) { |
140
|
3 |
|
$valid = implode(', ', static::ALLOWED_GROUPS); |
141
|
3 |
|
$alert = "{$group} is not an allowed group. Allowed groups include {$valid}."; |
142
|
|
|
|
143
|
3 |
|
throw new InvalidArgumentException($alert); |
144
|
3 |
|
} elseif (!in_array($group, $this->groups)) { |
145
|
3 |
|
$this->groups[] = $group; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* |
154
|
|
|
*/ |
155
|
|
|
protected function setText(string $text) |
156
|
|
|
{ |
157
|
|
|
if (!in_array($text, static::ALLOWED_TEXT)) { |
158
|
|
|
$valid = implode(', ', static::ALLOWED_TEXT); |
159
|
|
|
$alert = "valid text includes {$valid} only"; |
160
|
|
|
|
161
|
|
|
throw new InvalidArgumentException($alert); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->text = $text; |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* |
171
|
|
|
*/ |
172
|
|
|
protected function getOptions() |
173
|
|
|
{ |
174
|
|
|
$options = []; |
175
|
|
|
|
176
|
|
|
foreach ($this->groups as $group) { |
177
|
|
|
foreach (static::MAP[$group] as $abbr => $name) { |
178
|
|
|
$options[] = [ |
179
|
|
|
'name' => $name, |
180
|
|
|
'abbr' => $abbr |
181
|
|
|
]; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ($this->reSort) { |
186
|
|
|
sort($options); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $options; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* |
194
|
|
|
*/ |
195
|
|
|
public function provideSelectionsData(): array |
196
|
|
|
{ |
197
|
|
|
return $this->getOptions(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* |
202
|
|
|
*/ |
203
|
|
|
public function defineSelectionText($item): string |
204
|
|
|
{ |
205
|
|
|
return $item[$this->text]; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* |
210
|
|
|
*/ |
211
|
|
|
public function defineSelectionValue($item): string |
212
|
|
|
{ |
213
|
|
|
return $item['abbr']; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|