-
Notifications
You must be signed in to change notification settings - Fork 2
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 #62 from sherlockode/feature/place_finders
Add Nominatim place search for Open Street Map
- Loading branch information
Showing
11 changed files
with
209 additions
and
34 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
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,78 @@ | ||
<?php | ||
|
||
namespace Sherlockode\SyliusMondialRelayPlugin\PlaceFinder; | ||
|
||
use Sherlockode\SyliusMondialRelayPlugin\Manager\MapProviderManager; | ||
|
||
class NominatimPlaceFinder implements PlaceFinderInterface | ||
{ | ||
/** | ||
* @param string|null $type | ||
* | ||
* @return bool | ||
*/ | ||
public function supports(?string $type): bool | ||
{ | ||
return MapProviderManager::MAP_PROVIDER_OSM === $type; | ||
} | ||
|
||
/** | ||
* @param string $query | ||
* | ||
* @return array | ||
*/ | ||
public function search(string $query): array | ||
{ | ||
$places = $this->get(sprintf( | ||
'https://nominatim.openstreetmap.org/search?q=%s&format=jsonv2&countrycodes=FR', | ||
$query | ||
)); | ||
|
||
return array_values(array_map(function ($place) { | ||
return [ | ||
'id' => $place['place_id'], | ||
'label' => $place['display_name'], | ||
]; | ||
}, $places)); | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* | ||
* @return string|null | ||
*/ | ||
public function find(string $id): ?string | ||
{ | ||
$place = $this->get(sprintf('https://nominatim.openstreetmap.org/details?place_id=%s&format=json', $id)); | ||
|
||
return $place['addresstags']['postcode'] ?? null; | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* | ||
* @return array | ||
*/ | ||
private function get(string $url): array | ||
{ | ||
$ch = curl_init(); | ||
|
||
try { | ||
curl_setopt($ch, CURLOPT_URL, $url); | ||
curl_setopt($ch, CURLOPT_HEADER, false); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | ||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); | ||
curl_setopt($ch, CURLOPT_USERAGENT, 'Curl'); | ||
|
||
$raw = curl_exec($ch); | ||
$response = json_decode($raw, true); | ||
} catch (\Exception $exception) { | ||
$response = []; | ||
} finally { | ||
curl_close($ch); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace Sherlockode\SyliusMondialRelayPlugin\PlaceFinder; | ||
|
||
interface PlaceFinderInterface | ||
{ | ||
/** | ||
* @param string|null $type | ||
* | ||
* @return bool | ||
*/ | ||
public function supports(?string $type): bool; | ||
|
||
/** | ||
* @param string $query | ||
* | ||
* @return array | ||
*/ | ||
public function search(string $query): array; | ||
|
||
/** | ||
* @param string $id | ||
* | ||
* @return string|null | ||
*/ | ||
public function find(string $id): ?string; | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace Sherlockode\SyliusMondialRelayPlugin\PlaceFinder; | ||
|
||
class PlaceFinderRegistry | ||
{ | ||
/** | ||
* @var PlaceFinderInterface[] | ||
*/ | ||
private $finders; | ||
|
||
/** | ||
* @param iterable $finders | ||
*/ | ||
public function __construct(iterable $finders) | ||
{ | ||
$this->finders = $finders; | ||
} | ||
|
||
/** | ||
* @param string|null $type | ||
* | ||
* @return PlaceFinderInterface|null | ||
*/ | ||
public function getFinder(?string $type): ?PlaceFinderInterface | ||
{ | ||
foreach ($this->finders as $finder) { | ||
if ($finder->supports($type)) { | ||
return $finder; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="sherlockode.sylius_mondial_relay_plugin.place_finder.registry" class="Sherlockode\SyliusMondialRelayPlugin\PlaceFinder\PlaceFinderRegistry"> | ||
<argument type="tagged_iterator" tag="sherlockode.sylius_mondial_relay_plugin.place_finder" /> | ||
</service> | ||
<service id="sherlockode.sylius_mondial_relay_plugin.place_finder.google" class="Sherlockode\SyliusMondialRelayPlugin\PlaceFinder\GooglePlaceFinder"> | ||
<argument>%sherlockode_sylius_mondial_relay.google_api_key%</argument> | ||
<tag name="sherlockode.sylius_mondial_relay_plugin.place_finder" /> | ||
</service> | ||
<service id="sherlockode.sylius_mondial_relay_plugin.place_finder.nominatim" class="Sherlockode\SyliusMondialRelayPlugin\PlaceFinder\NominatimPlaceFinder"> | ||
<tag name="sherlockode.sylius_mondial_relay_plugin.place_finder" /> | ||
</service> | ||
</services> | ||
</container> |
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