Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+63 -35
View File
@@ -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;
}
}
}