-
-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2525 from jeroendesloovere/integrate-geolocation-…
…vendor Integrate Geolocation in Backend Location Model
- Loading branch information
Showing
7 changed files
with
131 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace ForkCMS\Utility; | ||
|
||
use Backend\Core\Language\Language; | ||
use Common\ModulesSettings; | ||
use JeroenDesloovere\Geolocation\Geolocation as API; | ||
use JeroenDesloovere\Geolocation\Result\Coordinates; | ||
use JeroenDesloovere\Geolocation\Exception; | ||
use Symfony\Component\Intl\Intl; | ||
|
||
class Geolocation | ||
{ | ||
/** @var API */ | ||
private $api; | ||
|
||
public function __construct(ModulesSettings $settings) | ||
{ | ||
$this->api = new API($settings->get('Core', 'google_maps_key')); | ||
} | ||
|
||
/** | ||
* @param string|null $street | ||
* @param string|null $streetNumber | ||
* @param string|null $city | ||
* @param string|null $zip | ||
* @param string|null $country | ||
* @return array - Example: ['latitude' => 50.8864, 'longitude' => 3.42928] | ||
*/ | ||
public function getCoordinates( | ||
string $street = null, | ||
string $streetNumber = null, | ||
string $city = null, | ||
string $zip = null, | ||
string $country = null | ||
): array { | ||
if (!empty($country)) { | ||
$country = Intl::getRegionBundle()->getCountryName($country, Language::getInterfaceLanguage()); | ||
} | ||
|
||
try { | ||
/** @var Coordinates $coordinates */ | ||
$coordinates = $this->api->getCoordinates( | ||
$street, | ||
$streetNumber, | ||
$city, | ||
$zip, | ||
$country | ||
); | ||
} catch (Exception $e) { | ||
$coordinates = null; | ||
} | ||
|
||
return [ | ||
'latitude' => ($coordinates instanceof Coordinates) ? $coordinates->getLatitude() : null, | ||
'longitude' => ($coordinates instanceof Coordinates) ? $coordinates->getLongitude() : null, | ||
]; | ||
} | ||
} |