Passed
Push — master ( 5de986...4c5faf )
by Chris
02:41
created

StateSelectOptions::provideSelectionsData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 12
    public function __construct(array $groups = ['States'], string $text = 'name', $reSort = false)
126
    {
127 12
        $this->setGroups($groups);
128 9
        $this->setText($text);
129
130 9
        $this->reSort = $reSort;
131 9
    }
132
133
    /**
134
     *
135
     */
136 12
    protected function setGroups(array $groups)
137
    {
138 12
        foreach ($groups as $group) {
139 12
            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 12
            } elseif (!in_array($group, $this->groups)) {
145 12
                $this->groups[] = $group;
146
            }
147
        }
148
149 9
        return $this;
150
    }
151
152
    /**
153
     *
154
     */
155 9
    protected function setText(string $text)
156
    {
157 9
        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 9
        $this->text = $text;
165
166 9
        return $this;
167
    }
168
169
    /**
170
     *
171
     */
172 9
    protected function getOptions()
173
    {
174 9
        $options = [];
175
176 9
        foreach ($this->groups as $group) {
177 9
            foreach (static::MAP[$group] as $abbr => $name) {
178 9
                $options[] = [
179 9
                    'name' => $name,
180 9
                    'abbr' => $abbr
181
                ];
182
            }
183
        }
184
185 9
        if ($this->reSort) {
186 3
            asort($options);
187
        }
188
189 9
        return $options;
190
    }
191
192
    /**
193
     *
194
     */
195 9
    public function provideSelectionsData(): array
196
    {
197 9
        return $this->getOptions();
198
    }
199
200
    /**
201
     *
202
     */
203 9
    public function defineSelectionText($item): string
204
    {
205 9
        return $item[$this->text];
206
    }
207
208
    /**
209
     *
210
     */
211 9
    public function defineSelectionValue($item): string
212
    {
213 9
        return $item['abbr'];
214
    }
215
}
216