| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
namespace OPTN\Includes\Utils; |
| 4 |
|
| 5 |
/** |
| 6 |
* Country decoder class |
| 7 |
*/ |
| 8 |
class CountryDecoder { |
| 9 |
|
| 10 |
/** |
| 11 |
* Country map |
| 12 |
* |
| 13 |
* @var array $country_map country map |
| 14 |
*/ |
| 15 |
private static $country_map = null; |
| 16 |
|
| 17 |
/** |
| 18 |
* Reads the country data from json and returns it |
| 19 |
* |
| 20 |
* @return array |
| 21 |
*/ |
| 22 |
private static function get_country_map() { |
| 23 |
if ( empty( self::$country_map ) ) { |
| 24 |
self::$country_map = wp_json_file_decode( plugin_dir_path( __FILE__ ) . '/country.json', array( 'associative' => true ) ); |
| 25 |
} |
| 26 |
|
| 27 |
return self::$country_map; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Decodes country code to actual country |
| 32 |
* |
| 33 |
* @param string $code country code. |
| 34 |
* @return string|null |
| 35 |
*/ |
| 36 |
public static function decode( $code ) { |
| 37 |
$code = strtoupper( $code ); |
| 38 |
|
| 39 |
$country_map = self::get_country_map(); |
| 40 |
|
| 41 |
return isset( $country_map[ $code ] ) ? $country_map[ $code ] : null; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Get total World Domination >:() |
| 47 |
* |
| 48 |
* @param int $count count. |
| 49 |
* @return float |
| 50 |
*/ |
| 51 |
public static function get_dom_pct( $count ) { |
| 52 |
$country_map = self::get_country_map(); |
| 53 |
|
| 54 |
$country_count = count( $country_map ); |
| 55 |
|
| 56 |
return 0 === $country_count ? 0 : min( round( ( $count / $country_count ) * 100, 2 ), 100 ); |
| 57 |
} |
| 58 |
} |
| 59 |
|