StateSelectOptions::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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