vendor and env first commit
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace Webpatser\Countries;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* CountryList
|
||||
*
|
||||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->table = \Config::get('countries.table_name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the countries from the JSON file, if it hasn't already been loaded.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getCountries()
|
||||
{
|
||||
//Get the countries from the JSON file
|
||||
if (! $this->countries){
|
||||
$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
|
||||
*
|
||||
* @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',
|
||||
];
|
||||
|
||||
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
|
||||
*
|
||||
* @param string display
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getListForSelect($display = 'name')
|
||||
{
|
||||
foreach ($this->getList($display) as $key => $value) {
|
||||
$countries[$key] = $value[$display];
|
||||
}
|
||||
|
||||
//return the array
|
||||
return $countries;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Webpatser\Countries;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* CountriesFacade
|
||||
*
|
||||
*/
|
||||
class CountriesFacade extends Facade {
|
||||
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getFacadeAccessor() { return 'countries'; }
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace Webpatser\Countries;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
/**
|
||||
* CountryListServiceProvider
|
||||
*/
|
||||
class CountriesServiceProvider extends ServiceProvider {
|
||||
|
||||
/**
|
||||
* Indicates if loading of the provider is deferred.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $defer = false;
|
||||
|
||||
/**
|
||||
* Bootstrap the application.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
|
||||
public function boot()
|
||||
{
|
||||
// The publication files to publish
|
||||
$this->publishes([__DIR__ . '/../../config/config.php' => config_path('countries.php')]);
|
||||
|
||||
// Append the country settings
|
||||
$this->mergeConfigFrom(
|
||||
__DIR__ . '/../../config/config.php', 'countries'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register everything.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->registerCountries();
|
||||
$this->registerCommands();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function registerCountries()
|
||||
{
|
||||
$this->app->bind('countries', function($app)
|
||||
{
|
||||
return new Countries();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the artisan commands.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function registerCommands()
|
||||
{
|
||||
$this->app->singleton('command.countries.migration', function ($app) {
|
||||
return new MigrationCommand($app);
|
||||
});
|
||||
|
||||
$this->commands('command.countries.migration');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function provides()
|
||||
{
|
||||
return ['countries'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace Webpatser\Countries;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class MigrationCommand extends Command {
|
||||
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'countries:migration';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Creates a migration following the Laravel-countries specifications.';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$app = app();
|
||||
$app['view']->addNamespace('countries',substr(__DIR__,0,-8).'views');
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function fire()
|
||||
{
|
||||
$this->line('');
|
||||
$this->info('The migration process will create a migration file and a seeder for the countries list');
|
||||
|
||||
$this->line('');
|
||||
|
||||
if ( $this->confirm("Proceed with the migration creation? [Yes|no]") )
|
||||
{
|
||||
$this->line('');
|
||||
|
||||
$this->info( "Creating migration and seeder..." );
|
||||
if( $this->createMigration( 'countries' ) )
|
||||
{
|
||||
$this->line('');
|
||||
|
||||
if (version_compare(app()->version(), '5.5.0', '<')) {
|
||||
$this->call('optimize', []);
|
||||
}
|
||||
|
||||
$this->line('');
|
||||
|
||||
$this->info( "Migration successfully created!" );
|
||||
}
|
||||
else{
|
||||
$this->error(
|
||||
"Coudn't create migration.\n Check the write permissions".
|
||||
" within the app/database/migrations directory."
|
||||
);
|
||||
}
|
||||
|
||||
$this->line('');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias fire method.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->fire();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the migration
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
protected function createMigration()
|
||||
{
|
||||
//Create the migration
|
||||
$app = app();
|
||||
$migrationFiles = [
|
||||
$this->laravel->path."/../database/migrations/*_setup_countries_table.php" => 'countries::generators.migration',
|
||||
$this->laravel->path."/../database/migrations/*_charify_countries_table.php" => 'countries::generators.char_migration',
|
||||
];
|
||||
|
||||
$seconds = 0;
|
||||
|
||||
foreach ($migrationFiles as $migrationFile => $outputFile) {
|
||||
if (sizeof(glob($migrationFile)) == 0) {
|
||||
$migrationFile = str_replace('*', date('Y_m_d_His', strtotime('+' . $seconds . ' seconds')), $migrationFile);
|
||||
|
||||
$fs = fopen($migrationFile, 'x');
|
||||
if ($fs) {
|
||||
$output = "<?php\n\n" .$app['view']->make($outputFile)->with('table', 'countries')->render();
|
||||
|
||||
fwrite($fs, $output);
|
||||
fclose($fs);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
$seconds++;
|
||||
}
|
||||
}
|
||||
|
||||
//Create the seeder
|
||||
$seeder_file = $this->laravel->path."/../database/seeds/CountriesSeeder.php";
|
||||
$output = "<?php\n\n" .$app['view']->make('countries::generators.seeder')->render();
|
||||
|
||||
if (!file_exists( $seeder_file )) {
|
||||
$fs = fopen($seeder_file, 'x');
|
||||
if ($fs) {
|
||||
fwrite($fs, $output);
|
||||
fclose($fs);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Database settings
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The name of the table to create in the database
|
||||
|
|
||||
*/
|
||||
'table_name' => 'countries',
|
||||
];
|
||||
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1010 B |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 1.7 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1005 B |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.7 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1016 B |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.8 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 968 B |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 971 B |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 2.2 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 959 B |
|
After Width: | Height: | Size: 2.1 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 2.0 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.0 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 2.1 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 2.2 KiB |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1018 B |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.0 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 957 B |
|
After Width: | Height: | Size: 962 B |
|
After Width: | Height: | Size: 973 B |
|
After Width: | Height: | Size: 1.7 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 950 B |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 994 B |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 954 B |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 2.0 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.8 KiB |
|
After Width: | Height: | Size: 1.8 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 958 B |
|
After Width: | Height: | Size: 959 B |
|
After Width: | Height: | Size: 2.2 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 1.0 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 2.1 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 2.0 KiB |
|
After Width: | Height: | Size: 1.1 KiB |