Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -11,43 +11,50 @@
|
||||
namespace IPS\GeoLocation;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use BadMethodCallException;
|
||||
use IPS\GeoLocation\GeoCoder\Google;
|
||||
use IPS\GeoLocation\GeoCoder\Mapbox;
|
||||
use IPS\Settings;
|
||||
use function defined;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* GeoCoder abstract class
|
||||
*/
|
||||
abstract class _GeoCoder
|
||||
abstract class GeoCoder
|
||||
{
|
||||
/**
|
||||
* @brief Cached GeoCoder instance
|
||||
*/
|
||||
protected static $instance = NULL;
|
||||
protected static GeoCoder|null $instance = NULL;
|
||||
|
||||
/**
|
||||
* Return instance of GeoCoder
|
||||
*
|
||||
* @return \IPS\GeoCoder
|
||||
* @throws \BadMethodCallException
|
||||
* @return GeoCoder|null
|
||||
* @throws BadMethodCallException
|
||||
*/
|
||||
public static function i()
|
||||
public static function i(): GeoCoder|null
|
||||
{
|
||||
if( static::$instance === NULL )
|
||||
{
|
||||
if ( \IPS\Settings::i()->googlemaps and \IPS\Settings::i()->google_maps_api_key )
|
||||
if ( Settings::i()->googlemaps and Settings::i()->google_maps_api_key )
|
||||
{
|
||||
static::$instance = new \IPS\GeoLocation\GeoCoder\Google();
|
||||
static::$instance = new Google();
|
||||
}
|
||||
elseif ( \IPS\Settings::i()->mapbox and \IPS\Settings::i()->mapbox_api_key )
|
||||
elseif ( Settings::i()->mapbox and Settings::i()->mapbox_api_key )
|
||||
{
|
||||
static::$instance = new \IPS\GeoLocation\GeoCoder\Mapbox();
|
||||
static::$instance = new Mapbox();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadMethodCallException;
|
||||
throw new BadMethodCallException;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,40 +11,53 @@
|
||||
namespace IPS\GeoLocation\GeoCoder;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use BadFunctionCallException;
|
||||
use BadMethodCallException;
|
||||
use IPS\GeoLocation;
|
||||
use IPS\GeoLocation\GeoCoder;
|
||||
use IPS\Http\Request\Exception;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Settings;
|
||||
use RuntimeException;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function is_array;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Google GeoCoder class
|
||||
*/
|
||||
class _Google extends \IPS\GeoLocation\GeoCoder
|
||||
class Google extends GeoCoder
|
||||
{
|
||||
/**
|
||||
* Get by location string
|
||||
*
|
||||
* @param string $location
|
||||
* @return \IPS\GeoLocation
|
||||
* @throws \BadFunctionCallException
|
||||
* @return GeoLocation
|
||||
* @throws BadFunctionCallException
|
||||
*/
|
||||
public static function decodeLocation( string $location )
|
||||
public static function decodeLocation( string $location ): GeoLocation
|
||||
{
|
||||
if ( \IPS\Settings::i()->googlemaps AND \IPS\Settings::i()->google_maps_api_key )
|
||||
if ( Settings::i()->googlemaps AND Settings::i()->google_maps_api_key )
|
||||
{
|
||||
$data = \IPS\Http\Url::external( "https://maps.googleapis.com/maps/api/geocode/json" )->setQueryString( array(
|
||||
$data = Url::external( "https://maps.googleapis.com/maps/api/geocode/json" )->setQueryString( array(
|
||||
'address' => $location,
|
||||
'sensor' => 'false',
|
||||
'key' => \IPS\Settings::i()->google_maps_api_key_secret ?: \IPS\Settings::i()->google_maps_api_key
|
||||
'key' => Settings::i()->google_maps_api_key_secret ?: Settings::i()->google_maps_api_key
|
||||
) )->request()->get()->decodeJson();
|
||||
|
||||
$obj = new \IPS\GeoLocation;
|
||||
$obj = new GeoLocation;
|
||||
|
||||
$_address = '';
|
||||
|
||||
/* Make sure the response from Google is valid */
|
||||
if( isset( $data['results'] ) AND \is_array( $data['results'] ) AND \count( $data['results'] ) )
|
||||
if( isset( $data['results'] ) AND is_array( $data['results'] ) AND count( $data['results'] ) )
|
||||
{
|
||||
if( isset( $data['results'][0]['geometry'] ) AND $data['results'][0]['geometry']['location'] )
|
||||
{
|
||||
@@ -83,6 +96,11 @@ class _Google extends \IPS\GeoLocation\GeoCoder
|
||||
$obj->region = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'administrative_area_level_2' )
|
||||
{
|
||||
$obj->county = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'locality' )
|
||||
{
|
||||
$obj->city = $component['long_name'];
|
||||
@@ -99,37 +117,37 @@ class _Google extends \IPS\GeoLocation\GeoCoder
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadFunctionCallException;
|
||||
throw new BadFunctionCallException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get by latitude and longitude
|
||||
*
|
||||
* @param float $lat Latitude
|
||||
* @param float $long Longitude
|
||||
* @return \IPS\GeoLocation
|
||||
* @throws \BadFunctionCallException
|
||||
* @throws \IPS\Http\Request\Exception
|
||||
* @param float $lat Latitude
|
||||
* @param float $long Longitude
|
||||
* @return GeoLocation
|
||||
* @throws BadFunctionCallException
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function decodeLatLong( $lat, $long )
|
||||
public static function decodeLatLong( float $lat, float $long ): GeoLocation
|
||||
{
|
||||
if ( \IPS\Settings::i()->googlemaps AND \IPS\Settings::i()->google_maps_api_key )
|
||||
if ( Settings::i()->googlemaps AND Settings::i()->google_maps_api_key )
|
||||
{
|
||||
$data = \IPS\Http\Url::external( "https://maps.googleapis.com/maps/api/geocode/json" )->setQueryString( array(
|
||||
$data = Url::external( "https://maps.googleapis.com/maps/api/geocode/json" )->setQueryString( array(
|
||||
'latlng' => "{$lat},{$long}",
|
||||
'sensor' => 'false',
|
||||
'key' => \IPS\Settings::i()->google_maps_api_key_secret ?: \IPS\Settings::i()->google_maps_api_key
|
||||
'key' => Settings::i()->google_maps_api_key_secret ?: Settings::i()->google_maps_api_key
|
||||
) )->request()->get()->decodeJson();
|
||||
|
||||
$obj = new \IPS\GeoLocation;
|
||||
$obj = new GeoLocation;
|
||||
$obj->lat = $lat;
|
||||
$obj->long = $long;
|
||||
|
||||
$_address = '';
|
||||
|
||||
/* Make sure the response from Google is valid */
|
||||
if( isset( $data['results'] ) AND \is_array( $data['results'] ) AND \count( $data['results'] ) )
|
||||
if( isset( $data['results'] ) AND is_array( $data['results'] ) AND count( $data['results'] ) )
|
||||
{
|
||||
foreach( $data['results'][0]['address_components'] as $component )
|
||||
{
|
||||
@@ -157,6 +175,11 @@ class _Google extends \IPS\GeoLocation\GeoCoder
|
||||
$obj->region = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'administrative_area_level_2' )
|
||||
{
|
||||
$obj->county = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'locality' )
|
||||
{
|
||||
$obj->city = $component['long_name'];
|
||||
@@ -173,36 +196,36 @@ class _Google extends \IPS\GeoLocation\GeoCoder
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadFunctionCallException;
|
||||
throw new BadFunctionCallException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latitude and longitude for the current object. Address must be set.
|
||||
*
|
||||
* @param \IPS\GeoLocation $geoLocation Geolocation object
|
||||
* @param bool $setAddress Whether or not to update the address information from the GeoCoder service
|
||||
* @param GeoLocation $geoLocation Geolocation object
|
||||
* @param bool $setAddress Whether or not to update the address information from the GeoCoder service
|
||||
* @return void
|
||||
* @throws \BadMethodCallException
|
||||
* @throws BadMethodCallException
|
||||
*/
|
||||
public function setLatLong( \IPS\GeoLocation &$geoLocation, $setAddress = FALSE )
|
||||
public function setLatLong(GeoLocation &$geoLocation, bool $setAddress=FALSE ) : void
|
||||
{
|
||||
if ( \IPS\Settings::i()->googlemaps AND \IPS\Settings::i()->google_maps_api_key AND $geoLocation->toString() )
|
||||
if ( Settings::i()->googlemaps AND Settings::i()->google_maps_api_key AND $geoLocation->toString() )
|
||||
{
|
||||
try
|
||||
{
|
||||
$data = \IPS\Http\Url::external( "https://maps.googleapis.com/maps/api/geocode/json" )->setQueryString( array(
|
||||
$data = Url::external( "https://maps.googleapis.com/maps/api/geocode/json" )->setQueryString( array(
|
||||
'address' => $geoLocation->toString(),
|
||||
'sensor' => 'false',
|
||||
'key' => \IPS\Settings::i()->google_maps_api_key_secret ?: \IPS\Settings::i()->google_maps_api_key
|
||||
'key' => Settings::i()->google_maps_api_key_secret ?: Settings::i()->google_maps_api_key
|
||||
) )->request()->get()->decodeJson();
|
||||
}
|
||||
catch( \RuntimeException $e )
|
||||
catch( RuntimeException $e )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !\count( $data['results'] ) )
|
||||
if ( !count( $data['results'] ) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -257,7 +280,7 @@ class _Google extends \IPS\GeoLocation\GeoCoder
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadFunctionCallException;
|
||||
throw new BadFunctionCallException;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,38 +11,51 @@
|
||||
namespace IPS\GeoLocation\GeoCoder;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use BadFunctionCallException;
|
||||
use BadMethodCallException;
|
||||
use IPS\GeoLocation;
|
||||
use IPS\GeoLocation\GeoCoder;
|
||||
use IPS\Http\Request\Exception;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Settings;
|
||||
use RuntimeException;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function is_array;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mapbox GeoCoder class
|
||||
*/
|
||||
class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
class Mapbox extends GeoCoder
|
||||
{
|
||||
/**
|
||||
* Get by location string
|
||||
*
|
||||
* @param string $location
|
||||
* @return \IPS\GeoLocation
|
||||
* @throws \BadFunctionCallException
|
||||
* @return GeoLocation
|
||||
* @throws BadFunctionCallException
|
||||
*/
|
||||
public static function decodeLocation( string $location )
|
||||
public static function decodeLocation( string $location ): GeoLocation
|
||||
{
|
||||
if ( \IPS\Settings::i()->mapbox AND \IPS\Settings::i()->mapbox_api_key )
|
||||
if ( Settings::i()->mapbox AND Settings::i()->mapbox_api_key )
|
||||
{
|
||||
$data = \IPS\Http\Url::external( "https://api.mapbox.com/geocoding/v5/mapbox.places/{$location}.json" )->setQueryString( array(
|
||||
'access_token' => \IPS\Settings::i()->mapbox_api_key,
|
||||
$data = Url::external( "https://api.mapbox.com/geocoding/v5/mapbox.places/{$location}.json" )->setQueryString( array(
|
||||
'access_token' => Settings::i()->mapbox_api_key,
|
||||
) )->request()->get()->decodeJson();
|
||||
|
||||
$obj = new \IPS\GeoLocation;
|
||||
$obj = new GeoLocation;
|
||||
|
||||
$_address = '';
|
||||
|
||||
/* Make sure the response from Mapbox is valid */
|
||||
if( isset( $data['results'] ) AND \is_array( $data['results'] ) AND \count( $data['results'] ) )
|
||||
if( isset( $data['results'] ) AND is_array( $data['results'] ) AND count( $data['results'] ) )
|
||||
{
|
||||
if( isset( $data['results'][0]['geometry'] ) AND isset( $data['results'][0]['geometry']['coordinates'] ) )
|
||||
{
|
||||
@@ -78,6 +91,11 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
$obj->region = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'administrative_area_level_2' )
|
||||
{
|
||||
$obj->county = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'locality' )
|
||||
{
|
||||
$obj->city = $component['long_name'];
|
||||
@@ -94,36 +112,36 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadFunctionCallException;
|
||||
throw new BadFunctionCallException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get by latitude and longitude
|
||||
*
|
||||
* @param float $lat Latitude
|
||||
* @param float $long Longitude
|
||||
* @return \IPS\GeoLocation
|
||||
* @throws \BadFunctionCallException
|
||||
* @throws \IPS\Http\Request\Exception
|
||||
* @param float $lat Latitude
|
||||
* @param float $long Longitude
|
||||
* @return GeoLocation
|
||||
* @throws BadFunctionCallException
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function decodeLatLong( $lat, $long )
|
||||
public static function decodeLatLong( float $lat, float $long ): GeoLocation
|
||||
{
|
||||
if ( \IPS\Settings::i()->mapbox AND \IPS\Settings::i()->mapbox_api_key )
|
||||
if ( Settings::i()->mapbox AND Settings::i()->mapbox_api_key )
|
||||
{
|
||||
$location = $long . ',' . $lat;
|
||||
$data = \IPS\Http\Url::external( "https://api.mapbox.com/geocoding/v5/mapbox.places/{$location}.json" )->setQueryString( array(
|
||||
'access_token' => \IPS\Settings::i()->mapbox_api_key,
|
||||
$data = Url::external( "https://api.mapbox.com/geocoding/v5/mapbox.places/{$location}.json" )->setQueryString( array(
|
||||
'access_token' => Settings::i()->mapbox_api_key,
|
||||
) )->request()->get()->decodeJson();
|
||||
|
||||
$obj = new \IPS\GeoLocation;
|
||||
$obj = new GeoLocation;
|
||||
$obj->lat = $lat;
|
||||
$obj->long = $long;
|
||||
|
||||
$_address = '';
|
||||
|
||||
/* Make sure the response from Mapbox is valid */
|
||||
if( isset( $data['results'] ) AND \is_array( $data['results'] ) AND \count( $data['results'] ) )
|
||||
if( isset( $data['results'] ) AND is_array( $data['results'] ) AND count( $data['results'] ) )
|
||||
{
|
||||
foreach( $data['results'][0]['address_components'] as $component )
|
||||
{
|
||||
@@ -151,6 +169,11 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
$obj->region = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'administrative_area_level_2' )
|
||||
{
|
||||
$obj->county = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'locality' )
|
||||
{
|
||||
$obj->city = $component['long_name'];
|
||||
@@ -167,34 +190,34 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadFunctionCallException;
|
||||
throw new BadFunctionCallException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latitude and longitude for the current object. Address must be set.
|
||||
*
|
||||
* @param \IPS\GeoLocation $geoLocation Geolocation object
|
||||
* @param bool $setAddress Whether or not to update the address information from the GeoCoder service
|
||||
* @param GeoLocation $geoLocation Geolocation object
|
||||
* @param bool $setAddress Whether or not to update the address information from the GeoCoder service
|
||||
* @return void
|
||||
* @throws \BadMethodCallException
|
||||
* @throws BadMethodCallException
|
||||
*/
|
||||
public function setLatLong( \IPS\GeoLocation &$geoLocation, $setAddress = FALSE )
|
||||
public function setLatLong( GeoLocation &$geoLocation, bool $setAddress=FALSE ) : void
|
||||
{
|
||||
if ( \IPS\Settings::i()->mapbox AND \IPS\Settings::i()->mapbox_api_key AND $geoLocation->toString() )
|
||||
if ( Settings::i()->mapbox AND Settings::i()->mapbox_api_key AND $geoLocation->toString() )
|
||||
{
|
||||
try
|
||||
{
|
||||
$data = \IPS\Http\Url::external( "https://api.mapbox.com/geocoding/v5/mapbox.places/" . urlencode( html_entity_decode( $geoLocation->toString() ) ) . ".json" )->setQueryString( array(
|
||||
'access_token' => \IPS\Settings::i()->mapbox_api_key,
|
||||
$data = Url::external( "https://api.mapbox.com/geocoding/v5/mapbox.places/" . urlencode( html_entity_decode( $geoLocation->toString() ) ) . ".json" )->setQueryString( array(
|
||||
'access_token' => Settings::i()->mapbox_api_key,
|
||||
) )->request()->get()->decodeJson();
|
||||
}
|
||||
catch( \RuntimeException $e )
|
||||
catch( RuntimeException $e )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !isset( $data['features'] ) or !\count( $data['features'] ) )
|
||||
if ( !isset( $data['features'] ) or !count( $data['features'] ) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -206,7 +229,7 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
|
||||
if( $setAddress === TRUE )
|
||||
{
|
||||
if( isset( $data['results'] ) AND \is_array( $data['results'] ) AND \count( $data['results'] ) )
|
||||
if( isset( $data['results'] ) AND is_array( $data['results'] ) AND count( $data['results'] ) )
|
||||
{
|
||||
foreach( $data['results'][0]['address_components'] as $component )
|
||||
{
|
||||
@@ -234,6 +257,11 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
$geoLocation->region = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'administrative_area_level_2' )
|
||||
{
|
||||
$geoLocation->county = $component['long_name'];
|
||||
}
|
||||
|
||||
if( $component['types'][0] == 'locality' )
|
||||
{
|
||||
$geoLocation->city = $component['long_name'];
|
||||
@@ -249,7 +277,7 @@ class _Mapbox extends \IPS\GeoLocation\GeoCoder
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \BadFunctionCallException;
|
||||
throw new BadFunctionCallException;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user