Skip to content

Commit

Permalink
spring cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
webpatser committed May 16, 2018
1 parent 032562f commit 000d7aa
Showing 1 changed file with 78 additions and 79 deletions.
157 changes: 78 additions & 79 deletions src/Webpatser/Countries/Countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,28 @@
*
*/
class Countries extends Model {

/**
* @var string
* Path to the directory containing countries data.
*/
protected $countries;

/**
* @var string
* The table for the countries in the database, is "countries" by default.
*/
protected $table;


/**
* @var array
*/
protected $countries = [];

/**
* @var string
* The table for the countries in the database, is "countries" by default.
*/
protected $table;

/**
* Constructor.
*
* @return void
*/
public function __construct()
{
$this->table = \Config::get('countries.table_name');
$this->table = \Config::get('countries.table_name');
}

/**
* Get the countries from the JSON file, if it hasn't already been loaded.
*
Expand All @@ -40,77 +39,77 @@ public function __construct()
protected function getCountries()
{
//Get the countries from the JSON file
if (!$this->countries || sizeof($this->countries) == 0){
if (sizeof($this->countries) == 0) {
$this->countries = json_decode(file_get_contents(__DIR__ . '/Models/countries.json'), true);
}

//Return the countries
return $this->countries;
}

/**
* Returns one country
*
* @param string $id The country id

/**
* Returns one country
*
* @return array
*/
public function getOne($id)
{
* @param string $id The country id
*
* @return array
*/
public function getOne($id)
{
$countries = $this->getCountries();
return $countries[$id];
}

/**
* Returns a list of countries
*
* @param string sort
*
* @return array
*/
public function getList($sort = null)
{
//Get the countries list
$countries = $this->getCountries();

//Sorting
$validSorts = [
'capital',
'citizenship',
'country-code',
'currency',
'currency_code',
'currency_sub_unit',
'full_name',
'iso_3166_2',
'iso_3166_3',
'name',
'region-code',
'sub-region-code',
'eea',
'calling_code',
'currency_symbol',
'flag',
return $countries[$id];
}
/**
* Returns a list of countries
*
* @param string sort
*
* @return array
*/
public function getList($sort = null)
{
//Get the countries list
$countries = $this->getCountries();
//Sorting
$validSorts = [
'capital',
'citizenship',
'country-code',
'currency',
'currency_code',
'currency_sub_unit',
'full_name',
'iso_3166_2',
'iso_3166_3',
'name',
'region-code',
'sub-region-code',
'eea',
'calling_code',
'currency_symbol',
'flag',
];

if (!is_null($sort) && in_array($sort, $validSorts)){
uasort($countries, function($a, $b) use ($sort) {
if (!isset($a[$sort]) && !isset($b[$sort])){
return 0;
} elseif (!isset($a[$sort])){
return -1;
} elseif (!isset($b[$sort])){
return 1;
} else {
return strcasecmp($a[$sort], $b[$sort]);
}
});
}

//Return the countries
return $countries;
}

if (!is_null($sort) && in_array($sort, $validSorts)){
uasort($countries, function($a, $b) use ($sort) {
if (!isset($a[$sort]) && !isset($b[$sort])){
return 0;
} elseif (!isset($a[$sort])){
return -1;
} elseif (!isset($b[$sort])){
return 1;
} else {
return strcasecmp($a[$sort], $b[$sort]);
}
});
}
//Return the countries
return $countries;
}
/**
* Returns a list of countries suitable to use with a select element in Laravelcollective\html
* Will show the value and sort by the column specified in the display attribute
Expand All @@ -124,7 +123,7 @@ public function getListForSelect($display = 'name')
foreach ($this->getList($display) as $key => $value) {
$countries[$key] = $value[$display];
}

//return the array
return $countries;
}
Expand Down

0 comments on commit 000d7aa

Please sign in to comment.