1 | <?php namespace DigitalCanvas\Options; |
||
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() |
|
81 | |||
82 | /** |
||
83 | * Checks if data has been cached |
||
84 | * |
||
85 | * @return bool |
||
86 | */ |
||
87 | 1 | public static function isCached() |
|
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')) |
|
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')) |
|
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')) |
|
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) |
|
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.