getCoordinatesForAddress agora retorna um dto

master
parent ed115ecd2e
commit fa6bb2a910

@ -0,0 +1,31 @@
<?php
namespace Ae3\LaravelGeoLayer\app\DataTransferObjects;
use Spatie\LaravelData\Data;
class GeocodingResponseData extends Data
{
/**
* @param float $lat
* @param float $lng
* @param string $accuracy
* @param string $formatted_address
* @param array $viewport
* @param array $address_components
* @param bool $partial_match
* @param string $place_id
* @param array $types
*/
public function __construct(
public float $lat,
public float $lng,
public string $accuracy,
public string $formatted_address,
public array $viewport,
public array $address_components,
public bool $partial_match,
public string $place_id,
public array $types
) {}
}

@ -2,13 +2,18 @@
namespace Ae3\LaravelGeoLayer\app\Services\Contracts; namespace Ae3\LaravelGeoLayer\app\Services\Contracts;
use Ae3\LaravelGeoLayer\app\DataTransferObjects\GeocodingResponseData;
interface GeocodingServiceContract interface GeocodingServiceContract
{ {
/** /**
* @param string $address * Retrieves the coordinates for the given address.
* @return array *
* @param string $address The address to retrieve coordinates for.
*
* @return GeocodingResponseData An object containing the latitude and longitude coordinates.
*/ */
public function getCoordinatesForAddress(string $address): array; public function getCoordinatesForAddress(string $address): GeocodingResponseData;
/** /**
* @param float $latitude * @param float $latitude

@ -2,6 +2,7 @@
namespace Ae3\LaravelGeoLayer\app\Services; namespace Ae3\LaravelGeoLayer\app\Services;
use Ae3\LaravelGeoLayer\app\DataTransferObjects\GeocodingResponseData;
use Ae3\LaravelGeoLayer\app\Services\Contracts\GeocodingServiceContract; use Ae3\LaravelGeoLayer\app\Services\Contracts\GeocodingServiceContract;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Spatie\Geocoder\Geocoder; use Spatie\Geocoder\Geocoder;
@ -16,9 +17,10 @@ class GeocodingService implements GeocodingServiceContract
/** /**
* Constructs a new instance of the class. * Constructs a new instance of the class.
*/ */
public function __construct() public function __construct(
protected readonly Client $client
)
{ {
$client = new Client();
$this->geocoder = new Geocoder($client); $this->geocoder = new Geocoder($client);
$this->geocoder->setApiKey(config('laravel-geo-layer.key')); $this->geocoder->setApiKey(config('laravel-geo-layer.key'));
$this->geocoder->setRegion(config('laravel-geo-layer.region')); $this->geocoder->setRegion(config('laravel-geo-layer.region'));
@ -31,11 +33,13 @@ class GeocodingService implements GeocodingServiceContract
* *
* @param string $address The address to retrieve coordinates for. * @param string $address The address to retrieve coordinates for.
* *
* @return array An array containing the latitude and longitude coordinates. * @return GeocodingResponseData An array containing the latitude and longitude coordinates.
*/ */
public function getCoordinatesForAddress(string $address): array public function getCoordinatesForAddress(string $address): GeocodingResponseData
{ {
return $this->geocoder->getCoordinatesForAddress($address); return GeocodingResponseData::from(
$this->geocoder->getCoordinatesForAddress($address)
);
} }
/** /**

Loading…
Cancel
Save