1
|
|
|
<?php namespace DigitalCanvas\Options; |
2
|
|
|
|
3
|
|
|
use RuntimeException; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* States Options Class |
7
|
|
|
* |
8
|
|
|
* @package Options |
9
|
|
|
*/ |
10
|
|
|
class States |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* xml data file |
14
|
|
|
* @var string|null |
15
|
|
|
*/ |
16
|
|
|
private static $file; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* States data |
20
|
|
|
* @var array|null |
21
|
|
|
*/ |
22
|
|
|
private static $states; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Sets the xml file to use to load states |
26
|
|
|
* |
27
|
|
|
* @param string $file |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
* @throws RuntimeException |
31
|
|
|
*/ |
32
|
3 |
|
public static function setXML($file = null) |
33
|
|
|
{ |
34
|
3 |
|
if (is_null($file)) { |
35
|
1 |
|
$file = __DIR__ . '/states.xml'; |
36
|
|
|
} |
37
|
3 |
|
if (!is_file($file)) { |
38
|
1 |
|
throw new RuntimeException('States XML file does not exist.'); |
39
|
|
|
} |
40
|
2 |
|
self::$file = realpath($file); |
41
|
2 |
|
self::$states = null; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Loads the states from the xml file. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
10 |
|
private static function loadStates() |
50
|
|
|
{ |
51
|
10 |
|
if (is_null(self::$file)) { |
52
|
1 |
|
self::setXML(); |
53
|
|
|
} |
54
|
|
|
// Load xml file |
55
|
10 |
|
$xml = simplexml_load_file(self::$file); |
56
|
10 |
|
$states = array(); |
57
|
|
|
// loop through states |
58
|
10 |
|
foreach ($xml as $state) { |
59
|
10 |
|
$states[] = array( |
60
|
10 |
|
'abbreviation' => (string)$state->abbreviation, |
61
|
10 |
|
'name' => (string)$state->name, |
62
|
10 |
|
'country' => (string)$state->country, |
63
|
10 |
|
'countryname' => (string)$state->countryname |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
// Cache states |
67
|
10 |
|
self::$states = $states; |
68
|
|
|
// Clear xml instance |
69
|
10 |
|
unset($xml, $states, $state); |
70
|
10 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Clears the states from the cache. |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
11 |
|
public static function clearCache() |
78
|
|
|
{ |
79
|
11 |
|
self::$states = null; |
80
|
11 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Checks if data has been cached |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
1 |
|
public static function isCached() |
88
|
|
|
{ |
89
|
1 |
|
return !is_null(self::$states); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns array of states |
94
|
|
|
* |
95
|
|
|
* @param array $allowed_countries Array of allowed countries.<br> If null all are returned. |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
2 |
|
public static function getArray($allowed_countries = array('US')) |
100
|
|
|
{ |
101
|
|
|
// Load States if they are not yet loaded. |
102
|
2 |
|
if (is_null(self::$states)) { |
103
|
2 |
|
self::loadStates(); |
104
|
|
|
} |
105
|
2 |
|
$states = self::$states; |
106
|
2 |
|
foreach ($states as $key => $value) { |
|
|
|
|
107
|
2 |
|
if (!empty($allowed_countries) && !in_array($value['country'], $allowed_countries)) { |
108
|
2 |
|
unset($states[$key]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
2 |
|
return $states; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns states as key=>value pair array |
116
|
|
|
* |
117
|
|
|
* @param array $allowed_countries Array of allowed countries.<br> If null all are returned. |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
4 |
|
public static function getPairs($allowed_countries = array('US')) |
122
|
|
|
{ |
123
|
|
|
// Load States if they are not yet loaded. |
124
|
4 |
|
if (is_null(self::$states)) { |
125
|
4 |
|
self::loadStates(); |
126
|
|
|
} |
127
|
4 |
|
$states = array(); |
128
|
4 |
|
foreach (self::$states as $state) { |
|
|
|
|
129
|
4 |
|
if (!$allowed_countries || in_array($state['country'], $allowed_countries)) { |
|
|
|
|
130
|
4 |
|
$states[$state['abbreviation']] = $state['name']; |
131
|
|
|
} |
132
|
|
|
} |
133
|
4 |
|
return $states; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns states as key=>value pair array grouped by country |
138
|
|
|
* |
139
|
|
|
* @param array $allowed_countries Array of allowed countries.<br> If null all are returned. |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
1 |
|
public static function getMultiPairs($allowed_countries = array('US', 'CA')) |
144
|
|
|
{ |
145
|
|
|
// Load States if they are not yet loaded. |
146
|
1 |
|
if (is_null(self::$states)) { |
147
|
1 |
|
self::loadStates(); |
148
|
|
|
} |
149
|
1 |
|
$states = array(); |
150
|
1 |
|
foreach (self::$states as $state) { |
|
|
|
|
151
|
1 |
|
if (empty($allowed_countries) || in_array($state['country'], $allowed_countries)) { |
152
|
1 |
|
if (!array_key_exists($state['countryname'], $states)) { |
153
|
1 |
|
$states[$state['countryname']] = array(); |
154
|
|
|
} |
155
|
1 |
|
$states[$state['countryname']][$state['abbreviation']] = $state['name']; |
156
|
|
|
} |
157
|
|
|
} |
158
|
1 |
|
return $states; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns a single state by abbreviation |
163
|
|
|
* |
164
|
|
|
* @param string $abbreviation |
165
|
|
|
* @param string $country |
166
|
|
|
* @param bool $name_only |
167
|
|
|
* |
168
|
|
|
* @return string|array|null |
169
|
|
|
*/ |
170
|
3 |
|
public static function getState($abbreviation, $country = null, $name_only = true) |
171
|
|
|
{ |
172
|
|
|
// Load States if they are not yet loaded. |
173
|
3 |
|
if (is_null(self::$states)) { |
174
|
3 |
|
self::loadStates(); |
175
|
|
|
} |
176
|
3 |
|
foreach (self::$states as $state) { |
|
|
|
|
177
|
3 |
|
if ($abbreviation == $state['abbreviation'] && (!$country || $country == $state['country'])) { |
|
|
|
|
178
|
3 |
|
return ($name_only) ? $state['name'] : $state; |
179
|
|
|
} |
180
|
|
|
} |
181
|
1 |
|
return null; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.