Skip to content

Commit

Permalink
updated autodiscovery
Browse files Browse the repository at this point in the history
and auto download commands for self-updates
added new columns
  • Loading branch information
Manash Sonowal committed Jan 13, 2020
1 parent 69e307c commit bc39916
Show file tree
Hide file tree
Showing 16 changed files with 1,028 additions and 581 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ What you dont get:

`composer require igaster/laravel_cities`

this package uses autodiscover or else you can add manually

- Add Service Provider in app.php:

```php
Expand All @@ -41,6 +43,13 @@ wget http://download.geonames.org/export/dump/allCountries.zip && unzip allCount
wget http://download.geonames.org/export/dump/hierarchy.zip && unzip hierarchy.zip && rm hierarchy.zip
```

or otherwise you can use
```
artisan geo:download
```

Download a *.txt files from geonames.org By default it will download allcountries and hierarchy files otherwise you can pass flag --countries for specific countries

- Migrate and Seed. Run:

```
Expand Down
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
}
],
"require": {
"illuminate/contracts": "~5.1 || ^6.0"
"illuminate/contracts": "~5.5 || ^6.0"
},
"extra": {
"branch-alias": {
"dev-master": "3.2-dev"
},
"laravel": {
"providers": [
"Igaster\\LaravelCities\\GeoServiceProvider"
]
}
},
"require-dev": {
"orchestra/testbench": "^4.0",
Expand Down
171 changes: 85 additions & 86 deletions src/Geo.php
Original file line number Diff line number Diff line change
@@ -1,87 +1,84 @@
<?php namespace Igaster\LaravelCities;
<?php

namespace Igaster\LaravelCities;

use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Support\Facades\Route;
use Igaster\LaravelCities\dbTree\EloquentTreeItem;

class Geo extends EloquentTreeItem {
protected $table = 'geo';
protected $guarded = [];
public $timestamps = false;
class Geo extends EloquentTreeItem
{
protected $table = 'geo';
protected $guarded = [];
public $timestamps = false;

const LEVEL_COUNTRY = 'PCLI';
const LEVEL_CAPITAL = 'PPLC';
const LEVEL_1 = 'ADM1';
const LEVEL_2 = 'ADM2';
const LEVEL_3 = 'ADM3';


protected $casts = [
'alternames' => 'array',
];
protected $casts = ['alternames' => 'array'];

// Hide From JSON
protected $hidden = [
'alternames',
'left',
'right',
'depth',
];

protected $hidden = ['alternames', 'left', 'right', 'depth'];

// ----------------------------------------------
// Scopes
// ----------------------------------------------

public function scopeCountry($query, $countryCode){
public function scopeCountry($query, $countryCode)
{
return $query->where('country', $countryCode);
}

public function scopeCapital($query){
return $query->where('level',Geo::LEVEL_CAPITAL);
public function scopeCapital($query)
{
return $query->where('level', self::LEVEL_CAPITAL);
}

public function scopeLevel($query,$level){
return $query->where('level',$level);
public function scopeLevel($query, $level)
{
return $query->where('level', $level);
}

public function scopeDescendants($query){
public function scopeDescendants($query)
{
return $query->where('left', '>', $this->left)->where('right', '<', $this->right);
}

public function scopeAncenstors($query){
return $query->where('left','<', $this->left)->where('right', '>', $this->right);
public function scopeAncenstors($query)
{
return $query->where('left', '<', $this->left)->where('right', '>', $this->right);
}

public function scopeChildren($query){
return $query->where(function($query)
{
public function scopeChildren($query)
{
return $query->where(function ($query) {
$query->where('left', '>', $this->left)
->where('right', '<', $this->right)
->where('depth', $this->depth+1);
});
->where('depth', $this->depth + 1);
});
}

public function scopeSearch($query,$search){
$search = '%'.mb_strtolower($search).'%';
public function scopeSearch($query, $search)
{
$search = '%' . mb_strtolower($search) . '%';

return $query->where(function($query) use($search)
{
return $query->where(function ($query) use ($search) {
$query->whereRaw('LOWER(alternames) LIKE ?', [$search])
->orWhereRaw('LOWER(name) LIKE ?', [$search]);
});

}

public function scopeAreDescentants($query,Geo $parent){
return $query->where(function($query) use($parent)
{
public function scopeAreDescentants($query, Geo $parent)
{
return $query->where(function ($query) use ($parent) {
$query->where('left', '>', $parent->left)
->where('right', '<', $parent->right);
});
}

public function scopeTest($query){
public function scopeTest($query)
{
return $query;
}

Expand All @@ -90,105 +87,115 @@ public function scopeTest($query){
// ----------------------------------------------

// public function setXxxAttribute($value){
// $this->attributes['xxx'] = $value;
// $this->attributes['xxx'] = $value;
// }

// ----------------------------------------------
// Relations
// ----------------------------------------------


// ----------------------------------------------
// Methods
// ----------------------------------------------

// search in `name` and `alternames` / return collection
public static function searchNames($name, Geo $parent =null){
public static function searchNames($name, Geo $parent = null)
{
$query = self::search($name)->orderBy('name', 'ASC');

if ($parent){
if ($parent) {
$query->areDescentants($parent);
}

return $query->get();
}

// get all Countries
public static function getCountries(){
return self::level(Geo::LEVEL_COUNTRY)->orderBy('name')->get();
public static function getCountries()
{
return self::level(self::LEVEL_COUNTRY)->orderBy('name')->get();
}

// get Country by country Code (eg US,GR)
public static function getCountry($countryCode){
return self::level(Geo::LEVEL_COUNTRY)->country($countryCode)->first();
public static function getCountry($countryCode)
{
return self::level(self::LEVEL_COUNTRY)->country($countryCode)->first();
}

// get multiple item by Ids
public static function getByIds(array $Ids = []){
return self::whereIn('id',$Ids)->orderBy('name')->get();
public static function getByIds(array $Ids = [])
{
return self::whereIn('id', $Ids)->orderBy('name')->get();
}

// is imediate Child of $item ?
public function isChildOf(Geo $item){
return ($this->left > $item->left) && ($this->right < $item->right) && ($this->depth == $item->depth+1);
public function isChildOf(Geo $item)
{
return ($this->left > $item->left) && ($this->right < $item->right) && ($this->depth == $item->depth + 1);
}

// is imediate Parent of $item ?
public function isParentOf(Geo $item){
return ($this->left < $item->left) && ($this->right > $item->right) && ($this->depth == $item->depth-1);
public function isParentOf(Geo $item)
{
return ($this->left < $item->left) && ($this->right > $item->right) && ($this->depth == $item->depth - 1);
}

// is Child of $item (any depth) ?
public function isDescendantOf(Geo $item){
public function isDescendantOf(Geo $item)
{
return ($this->left > $item->left) && ($this->right < $item->right);
}

// is Parent of $item (any depth) ?
public function isAncenstorOf(Geo $item){
public function isAncenstorOf(Geo $item)
{
return ($this->left < $item->left) && ($this->right > $item->right);
}

// retrieve by name
public static function findName($name){
return self::where('name',$name)->first();
// retrieve by name
public static function findName($name)
{
return self::where('name', $name)->first();
}

// get all imediate Children (Collection)
public function getChildren(){
return self::descendants()->where('depth', $this->depth+1)->orderBy('name')->get();
public function getChildren()
{
return self::descendants()->where('depth', $this->depth + 1)->orderBy('name')->get();
}

// get Parent (Geo)
public function getParent(){
return self::ancenstors()->where('depth', $this->depth-1)->first();
public function getParent()
{
return self::ancenstors()->where('depth', $this->depth - 1)->first();
}

// get all Ancnstors (Collection) ordered by level (Country -> City)
public function getAncensors(){
public function getAncensors()
{
return self::ancenstors()->orderBy('depth')->get();
}

// get all Descendants (Collection) Alphabetical
public function getDescendants(){
public function getDescendants()
{
return self::descendants()->orderBy('level')->orderBy('name')->get();
}



// Return only $fields as Json. null = Show all
public function fliterFields($fields = null){

if (is_string($fields)){ // Comma Seperated List (eg Url Param)
// Return only $fields as Json. null = Show all
public function fliterFields($fields = null)
{
if (is_string($fields)) { // Comma Seperated List (eg Url Param)
$fields = explode(',', $fields);
}

if(empty($fields)){
if (empty($fields)) {
$this->hidden = [];
} else {
$this->hidden = ['id','parent_id','left','right','depth','name','alternames','country','level','population','lat','long'];
$this->hidden = ['id', 'parent_id', 'left', 'right', 'depth', 'name', 'alternames', 'country', 'level', 'population', 'lat', 'long'];
foreach ($fields as $field) {
$index = array_search($field, $this->hidden);
if($index !== false){
if ($index !== false) {
unset($this->hidden[$index]);
}
};
Expand All @@ -202,16 +209,8 @@ public function fliterFields($fields = null){
// Routes
// ----------------------------------------------

public static function ApiRoutes(){
Route::group(['prefix' => 'geo'], function(){
Route::get('search/{name}/{parent_id?}', '\Igaster\LaravelCities\GeoController@search');
Route::get('item/{id}', '\Igaster\LaravelCities\GeoController@item');
Route::get('items/{ids}', '\Igaster\LaravelCities\GeoController@items');
Route::get('children/{id}', '\Igaster\LaravelCities\GeoController@children');
Route::get('parent/{id}', '\Igaster\LaravelCities\GeoController@parent');
Route::get('country/{code}', '\Igaster\LaravelCities\GeoController@country');
Route::get('countries', '\Igaster\LaravelCities\GeoController@countries');
});
public static function ApiRoutes()
{
require_once __DIR__ . '/routes.php';
}

}
}
Loading

0 comments on commit bc39916

Please sign in to comment.