Skip to content

Commit

Permalink
Add countries class
Browse files Browse the repository at this point in the history
  • Loading branch information
spekkionu committed May 22, 2016
1 parent 9b4c0a4 commit 616a84e
Show file tree
Hide file tree
Showing 5 changed files with 1,240 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
],
"autoload": {
"classmap": ["src/States.php"]
"classmap": ["src/States.php", "src/Countries.php"]
},
"require-dev": {
"phpunit/phpunit": "^4.8"
Expand Down
131 changes: 131 additions & 0 deletions src/Countries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
namespace DigitalCanvas\Options;

use RuntimeException;

class Countries
{
/**
* @var string
*/
private static $file;

/**
* @var array
*/
private static $countries;

/**
* Sets the xml file to use to load countries
*
* @param string|null $file
*
* @throws RuntimeException
*/
public static function setXML($file = null)
{
if (is_null($file)) {
$file = __DIR__ . '/countries.xml';
}
if (!is_file($file)) {
throw new RuntimeException('Country XML file does not exist.');
}
self::$file = realpath($file);
self::$countries = null;
}

/**
* Loads the countries from the xml file.
*/
private static function loadCountries()
{
if (is_null(self::$file)) {
self::setXML();
}
// Load xml file
$xml = simplexml_load_file(self::$file);
$countries = array();
// loop through states
foreach ($xml as $country) {
$countries[(string) $country->code] = array(
'code' => (string) $country->code,
'name' => (string) $country->name
);
}
// Cache states
self::$countries = $countries;
// Clear xml instance
unset($xml, $countries, $country);
}

/**
* Clears the data from the cache.
*/
public static function clearCache()
{
self::$countries = null;
}

/**
* Checks if data has been cached
*
* @return boolean
*/
public static function isCached()
{
return !is_null(self::$countries);
}

/**
* Returns array of countries
*
* @return array
*/
public static function getArray()
{
// Load States if they are not yet loaded.
if (is_null(self::$countries)) {
self::loadCountries();
}
return self::$countries;
}

/**
* Returns countries as abbreviation=>name pair array
*
* @return array
*/
public static function getPairs()
{
// Load States if they are not yet loaded.
if (is_null(self::$countries)) {
self::loadCountries();
}
$countries = array();
foreach (self::$countries as $key => $country) {
$countries[$country['code']] = $country['name'];
}
return $countries;
}

/**
* Returns a single country by abbreviation
*
* @param string $country
* @param bool $name_only
*
* @return string|array|bool
*/
public static function getCountry($country, $name_only = true)
{
// Load States if they are not yet loaded.
if (is_null(self::$countries)) {
self::loadCountries();
}
if (array_key_exists($country, self::$countries)) {
return ($name_only) ? self::$countries[$country]['name'] : self::$countries[$country];
} else {
return false;
}
}
}

0 comments on commit 616a84e

Please sign in to comment.