Passed
Push — master ( 3c0766...793f0f )
by Chris
05:28
created

StateSelectOptions   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 87
c 1
b 0
f 0
dl 0
loc 152
ccs 21
cts 21
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setGroups() 0 10 4
A getOptions() 0 5 1
A getSelection() 0 9 2
A __construct() 0 4 1
1
<?php
2
3
namespace WebTheory\Saveyour\Fields\Selections;
4
5
use InvalidArgumentException;
6
use WebTheory\Saveyour\Contracts\SelectionProviderInterface;
7
8
class StateSelectOptions implements SelectionProviderInterface
9
{
10
    /**
11
     *
12
     */
13
    protected $groups = [];
14
15
    /**
16
     *
17
     */
18
    protected $reSort;
19
20
    /**
21
     *
22
     */
23
    protected const ALLOWED_GROUPS = ['States', 'Territories', 'ArmedForces'];
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 12
    public function __construct(array $groups = ['States'], $reSort = false)
116
    {
117 12
        $this->setGroups($groups);
118 9
        $this->reSort = $reSort;
119 9
    }
120
121
    /**
122
     *
123
     */
124 12
    protected function setGroups(array $groups)
125
    {
126 12
        foreach ($groups as $group) {
127 12
            if (!in_array($group, static::ALLOWED_GROUPS)) {
128 3
                $valid = implode(', ', static::ALLOWED_GROUPS);
129 3
                $alert = "{$group} is not an allowed group. Allowed groups include {$valid}.";
130
131 3
                throw new InvalidArgumentException($alert);
132 12
            } elseif (!in_array($group, $this->groups)) {
133 12
                $this->groups[] = $group;
134
            }
135
        }
136 9
    }
137
138
    /**
139
     *
140
     */
141 9
    public function getSelection(): array
142
    {
143 9
        $groups = array_merge(...$this->getOptions());
144
145 9
        if ($this->reSort) {
146 3
            sort($groups);
147
        }
148
149 9
        return $groups;
150
    }
151
152
    /**
153
     *
154
     */
155 9
    protected function getOptions()
156
    {
157
        return array_map(function ($group) {
158 9
            return static::MAP[$group];
159 9
        }, $this->groups);
160
    }
161
}
162