Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.96% |
190 / 192 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ISO3166Trait | |
98.96% |
190 / 192 |
|
50.00% |
1 / 2 |
34 | |
0.00% |
0 / 1 |
| getBy2Code | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getRegion | |
100.00% |
190 / 190 |
|
100.00% |
1 / 1 |
33 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Localization |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Localization; |
| 16 | |
| 17 | /** |
| 18 | * ISO 3166 regional grouping |
| 19 | * |
| 20 | * @package phpOMS\Localization |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | trait ISO3166Trait |
| 26 | { |
| 27 | /** |
| 28 | * Get value by 2 code |
| 29 | * |
| 30 | * @param string $code 2-code |
| 31 | * |
| 32 | * @return mixed |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | */ |
| 36 | public static function getBy2Code(string $code) : mixed |
| 37 | { |
| 38 | /** @var string $code3 */ |
| 39 | $code3 = ISO3166TwoEnum::getName($code); |
| 40 | |
| 41 | return self::getByName($code3); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get countries in a region |
| 46 | * |
| 47 | * @param string $region Region name |
| 48 | * |
| 49 | * @return array |
| 50 | * |
| 51 | * @since 1.0.0 |
| 52 | */ |
| 53 | public static function getRegion(string $region) : array |
| 54 | { |
| 55 | $region = \strtolower($region); |
| 56 | |
| 57 | switch ($region) { |
| 58 | case 'europe': |
| 59 | return \array_merge( |
| 60 | self::getRegion('north-europe'), |
| 61 | self::getRegion('south-europe'), |
| 62 | self::getRegion('east-europe'), |
| 63 | self::getRegion('west-europe') |
| 64 | ); |
| 65 | case 'asia': |
| 66 | return \array_merge( |
| 67 | self::getRegion('central-asia'), |
| 68 | self::getRegion('south-asia'), |
| 69 | self::getRegion('southeast-asia'), |
| 70 | self::getRegion('east-asia'), |
| 71 | self::getRegion('west-asia') |
| 72 | ); |
| 73 | case 'america': |
| 74 | return \array_merge( |
| 75 | self::getRegion('north-america'), |
| 76 | self::getRegion('south-america'), |
| 77 | self::getRegion('central-america'), |
| 78 | self::getRegion('caribbean') |
| 79 | ); |
| 80 | case 'oceania': |
| 81 | return \array_merge( |
| 82 | self::getRegion('australia'), |
| 83 | self::getRegion('polynesia'), |
| 84 | self::getRegion('melanesia'), |
| 85 | self::getRegion('micronesia'), |
| 86 | self::getRegion('antartica') |
| 87 | ); |
| 88 | case 'africa': |
| 89 | return \array_merge( |
| 90 | self::getRegion('north-africa'), |
| 91 | self::getRegion('south-africa'), |
| 92 | self::getRegion('east-africa'), |
| 93 | self::getRegion('west-africa'), |
| 94 | self::getRegion('central-africa') |
| 95 | ); |
| 96 | case 'eu': |
| 97 | return [ |
| 98 | self::_AUT, self::_BEL, self::_BGR, self::_HRV, self::_CYP, |
| 99 | self::_CZE, self::_DNK, self::_EST, self::_FIN, self::_FRA, |
| 100 | self::_DEU, self::_GRC, self::_HUN, self::_IRL, self::_ITA, |
| 101 | self::_LVA, self::_LTU, self::_LUX, self::_MLT, self::_NLD, |
| 102 | self::_POL, self::_PRT, self::_ROU, self::_SVK, self::_SVN, |
| 103 | self::_ESP, self::_SWE, |
| 104 | ]; |
| 105 | case 'euro': |
| 106 | return [ |
| 107 | self::_AUT, self::_BEL, self::_HRV, self::_CYP, self::_EST, |
| 108 | self::_FIN, self::_FRA, self::_DEU, self::_GRC, self::_IRL, |
| 109 | self::_ITA, self::_LVA, self::_LTU, self::_LUX, self::_MLT, |
| 110 | self::_NLD, self::_PRT, self::_SVK, self::_SVN, self::_ESP, |
| 111 | ]; |
| 112 | case 'north-europe': |
| 113 | return [ |
| 114 | self::_ALA, self::_DNK, self::_EST, self::_FRO, self::_FIN, |
| 115 | self::_GGY, self::_ISL, self::_IRL, self::_IMN, self::_JEY, |
| 116 | self::_LVA, self::_LTU, self::_NOR, self::_SJM, self::_SWE, |
| 117 | self::_GBR, |
| 118 | ]; |
| 119 | case 'south-europe': |
| 120 | return [ |
| 121 | self::_ALB, self::_AND, self::_BIH, self::_HRV, self::_GIB, |
| 122 | self::_GRC, self::_ITA, self::_XXK, self::_MLT, self::_MNE, |
| 123 | self::_MKD, self::_PRT, self::_SMR, self::_SRB, self::_SVN, |
| 124 | self::_ESP, self::_VAT, |
| 125 | ]; |
| 126 | case 'east-europe': |
| 127 | return [ |
| 128 | self::_BLR, self::_BGR, self::_CZE, self::_HUN, self::_MDA, |
| 129 | self::_POL, self::_ROU, self::_RUS, self::_SVK, self::_UKR, |
| 130 | ]; |
| 131 | case 'west-europe': |
| 132 | return [ |
| 133 | self::_AUT, self::_BEL, self::_FRA, self::_DEU, self::_LIE, |
| 134 | self::_LUX, self::_NLD, self::_MCO, self::_CHE, |
| 135 | ]; |
| 136 | case 'middle-east': |
| 137 | return [ |
| 138 | self::_BHR, self::_CYP, self::_EGY, self::_IRN, self::_IRQ, |
| 139 | self::_ISR, self::_JOR, self::_KWT, self::_LBN, self::_OMN, |
| 140 | self::_PSE, self::_QAT, self::_SAU, self::_SYR, self::_TUR, |
| 141 | self::_ARE, self::_YEM, |
| 142 | ]; |
| 143 | case 'south-america': |
| 144 | return [ |
| 145 | self::_ARG, self::_BOL, self::_BVT, self::_BRA, self::_CHL, |
| 146 | self::_COL, self::_ECU, self::_FLK, self::_GUF, self::_GUY, |
| 147 | self::_PRY, self::_PER, self::_SGS, self::_SUR, self::_URY, |
| 148 | self::_VEN, |
| 149 | ]; |
| 150 | case 'north-america': |
| 151 | return [ |
| 152 | self::_BMU, self::_CAN, self::_GRL, self::_SPM, self::_USA, |
| 153 | ]; |
| 154 | case 'central-america': |
| 155 | return [ |
| 156 | self::_BLZ, self::_CRI, self::_SLV, self::_HND, self::_MEX, |
| 157 | self::_NIC, self::_PAN, self::_GTM, |
| 158 | ]; |
| 159 | case 'caribbean': |
| 160 | return [ |
| 161 | self::_ATG, self::_ABW, self::_BHS, self::_BRB, self::_VGB, |
| 162 | self::_CYM, self::_CUB, self::_CUW, self::_DMA, self::_VIR, |
| 163 | self::_DOM, self::_GRD, self::_GLP, self::_HTI, self::_JAM, |
| 164 | self::_MTQ, self::_MSR, self::_PRI, self::_BLM, self::_KNA, |
| 165 | self::_LCA, self::_MAF, self::_SXM, self::_VCT, self::_TTO, |
| 166 | self::_TCA, self::_AIA, self::_BES, |
| 167 | ]; |
| 168 | case 'central-asia': |
| 169 | return [ |
| 170 | self::_KAZ, self::_KGZ, self::_TJK, self::_TKM, self::_UZB, |
| 171 | ]; |
| 172 | case 'south-asia': |
| 173 | return [ |
| 174 | self::_AFG, self::_BGD, self::_BTN, self::_IND, self::_IRN, |
| 175 | self::_MDV, self::_NPL, self::_PAK, self::_LKA, |
| 176 | ]; |
| 177 | case 'southeast-asia': |
| 178 | return [ |
| 179 | self::_BRN, self::_KHM, self::_TLS, self::_IDN, self::_LAO, |
| 180 | self::_MYS, self::_MMR, self::_PHL, self::_SGP, self::_THA, |
| 181 | self::_VNM, |
| 182 | ]; |
| 183 | case 'east-asia': |
| 184 | return [ |
| 185 | self::_CHN, self::_HKG, self::_JPN, self::_MAC, self::_MNG, |
| 186 | self::_KOR, self::_PRK, self::_TWN, |
| 187 | ]; |
| 188 | case 'west-asia': |
| 189 | return [ |
| 190 | self::_ARM, self::_AZE, self::_BHR, self::_CYP, self::_GEO, |
| 191 | self::_IRQ, self::_ISR, self::_JOR, self::_KWT, self::_LBN, |
| 192 | self::_OMN, self::_PSE, self::_QAT, self::_SAU, self::_SYR, |
| 193 | self::_TUR, self::_ARE, self::_YEM, |
| 194 | ]; |
| 195 | case 'central-africa': |
| 196 | return [ |
| 197 | self::_AGO, self::_CMR, self::_CAF, self::_TCD, self::_COD, |
| 198 | self::_GNQ, self::_GAB, self::_COG, self::_STP, |
| 199 | ]; |
| 200 | case 'south-africa': |
| 201 | return [ |
| 202 | self::_BWA, self::_SWZ, self::_LSO, self::_NAM, self::_ZAF, |
| 203 | ]; |
| 204 | case 'north-africa': |
| 205 | return [ |
| 206 | self::_DZA, self::_EGY, self::_LBY, self::_MAR, self::_SSD, |
| 207 | self::_SDN, self::_TUN, self::_ESH, |
| 208 | ]; |
| 209 | case 'east-africa': |
| 210 | return [ |
| 211 | self::_IOT, self::_BDI, self::_COM, self::_DJI, self::_ERI, |
| 212 | self::_ETH, self::_KEN, self::_MDG, self::_MWI, self::_MUS, |
| 213 | self::_MYT, self::_MOZ, self::_RWA, self::_SYC, self::_SOM, |
| 214 | self::_TZA, self::_UGA, self::_ZMB, self::_ZWE, self::_REU, |
| 215 | ]; |
| 216 | case 'west-africa': |
| 217 | return [ |
| 218 | self::_BEN, self::_BFA, self::_CPV, self::_GMB, self::_GHA, |
| 219 | self::_GIN, self::_GNB, self::_CIV, self::_LBR, self::_MLI, |
| 220 | self::_MRT, self::_NER, self::_NGA, self::_SHN, self::_SEN, |
| 221 | self::_SLE, self::_TGO, |
| 222 | ]; |
| 223 | case 'australia': |
| 224 | return [ |
| 225 | self::_AUS, self::_CXR, self::_CCK, self::_HMD, self::_NZL, |
| 226 | self::_NFK, |
| 227 | ]; |
| 228 | case 'polynesia': |
| 229 | return [ |
| 230 | self::_WSM, self::_COK, self::_PYF, self::_NIU, self::_PCN, |
| 231 | self::_WSM, self::_TKL, self::_TON, self::_TUV, self::_WLF, |
| 232 | ]; |
| 233 | case 'melanesia': |
| 234 | return [ |
| 235 | self::_FJI, self::_NCL, self::_PNG, self::_SLB, self::_VUT, |
| 236 | ]; |
| 237 | case 'micronesia': |
| 238 | return [ |
| 239 | self::_FSM, self::_GUM, self::_KIR, self::_MHL, self::_NRU, |
| 240 | self::_MNP, self::_PLW, self::_UMI, |
| 241 | ]; |
| 242 | case 'antarctica': |
| 243 | return [ |
| 244 | self::_ATA, self::_ATF, |
| 245 | ]; |
| 246 | default: |
| 247 | return []; |
| 248 | } |
| 249 | } |
| 250 | } |