Projeto inicial

master
commit 3780c10848

9
.gitignore vendored

@ -0,0 +1,9 @@
### Laravel template
/vendor
storage/*.key
.env
.idea
.phpunit.result.cache
.php-cs-fixer.cache
tools/php-cs-fixer/vendor
tools/php-cs-fixer/.php_cs.cache

@ -0,0 +1,35 @@
{
"name": "tobias/laravel-geo",
"description": "Biblioteca para converter endereços em coordenadas geográficas (latitude e longitude) e vice-versa.",
"type": "project",
"license": "MIT",
"autoload": {
"psr-4": {
"Ae3\\LaravelGeoLayer\\": "src/",
"Ae3\\LaravelGeoLayer\\Tests\\": "tests/"
}
},
"authors": [
{
"name": "José Tobias de Freitas Neto",
"email": "tobias@ae3tecnologia.com.br"
}
],
"require": {
"php": "^7.4",
"laravel/framework": ">=5.8",
"spatie/geocoder": "^3.15"
},
"minimum-stability": "stable",
"extra": {
"laravel": {
"providers": [
"Ae3\\LaravelGeoLayer\\app\\Providers\\LaravelGeoServiceProvider"
]
}
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"orchestra/testbench": "^6.41"
}
}

7980
composer.lock generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<php>
<server name="APP_ENV" value="testing"/>
</php>
</phpunit>

@ -0,0 +1,32 @@
<?php
namespace Ae3\LaravelGeoLayer\app\Providers;
use Illuminate\Support\ServiceProvider;
class LaravelGeoServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../../config/config.php', 'laravel-geo-layer');
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../../config/config.php' => config_path('laravel-geo-layer.php'),
], 'config');
}
}
}

@ -0,0 +1,19 @@
<?php
namespace Ae3\LaravelGeoLayer\app\Services\Contracts;
interface GeocodingServiceContract
{
/**
* @param string $address
* @return array
*/
public function getCoordinatesForAddress(string $address): array;
/**
* @param float $latitude
* @param float $longitude
* @return array
*/
public function getAddressForCoordinates(float $latitude, float $longitude): array;
}

@ -0,0 +1,53 @@
<?php
namespace Ae3\LaravelGeoLayer\app\Services;
use Ae3\LaravelGeoLayer\app\Services\Contracts\GeocodingServiceContract;
use GuzzleHttp\Client;
use Spatie\Geocoder\Geocoder;
class GeocodingService implements GeocodingServiceContract
{
/**
* @var Geocoder
*/
protected Geocoder $geocoder;
/**
* Constructs a new instance of the class.
*/
public function __construct()
{
$client = new Client();
$this->geocoder = new Geocoder($client);
$this->geocoder->setApiKey(config('laravel-geo-layer.key'));
$this->geocoder->setRegion(config('laravel-geo-layer.region'));
$this->geocoder->setCountry(config('laravel-geo-layer.country'));
$this->geocoder->setBounds(config('laravel-geo-layer.bounds'));
}
/**
* Retrieves the coordinates for the given address.
*
* @param string $address The address to retrieve coordinates for.
*
* @return array An array containing the latitude and longitude coordinates.
*/
public function getCoordinatesForAddress(string $address): array
{
return $this->geocoder->getCoordinatesForAddress($address);
}
/**
* Retrieves the address for the given coordinates.
*
* @param float $latitude The latitude of the coordinates.
* @param float $longitude The longitude of the coordinates.
*
* @return array The address information for the given coordinates.
*/
public function getAddressForCoordinates(float $latitude, float $longitude): array
{
return $this->geocoder->getAddressForCoordinates($latitude, $longitude);
}
}

@ -0,0 +1,37 @@
<?php
return [
/*
* The api key used when sending Geocoding requests to Google.
*/
'key' => env('GOOGLE_MAPS_GEOCODING_API_KEY', ''),
/*
* The language param used to set response translations for textual data.
*
* More info: https://developers.google.com/maps/faq#languagesupport
*/
'language' => '',
/*
* The region param used to finetune the geocoding process.
*
* More info: https://developers.google.com/maps/documentation/geocoding/requests-geocoding#RegionCodes
*/
'region' => 'br',
/*
* The bounds param used to finetune the geocoding process.
*
* More info: https://developers.google.com/maps/documentation/geocoding/requests-geocoding#Viewports
*/
'bounds' => 'Roraima',
/*
* The country param used to limit results to a specific country.
*
* More info: https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRequests
*/
'country' => 'Brazil',
];

@ -0,0 +1,39 @@
<?php
return [
/*
* The api key used when sending Geocoding requests to Google.
*/
'key' => env('GOOGLE_MAPS_GEOCODING_API_KEY', ''),
/*
* The language param used to set response translations for textual data.
*
* More info: https://developers.google.com/maps/faq#languagesupport
*/
'language' => '',
/*
* The region param used to finetune the geocoding process.
*
* More info: https://developers.google.com/maps/documentation/geocoding/requests-geocoding#RegionCodes
*/
'region' => 'br',
/*
* The bounds param used to finetune the geocoding process.
*
* More info: https://developers.google.com/maps/documentation/geocoding/requests-geocoding#Viewports
*/
'bounds' => 'Roraima',
/*
* The country param used to limit results to a specific country.
*
* More info: https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRequests
*/
'country' => 'Brazil',
];

@ -0,0 +1,38 @@
<?php
namespace Ae3\LaravelGeoLayer\Tests;
use Ae3\LaravelGeoLayer\app\Providers\LaravelGeoServiceProvider;
use Orchestra\Testbench\TestCase as OrchestraTestCase;
class TestCase extends OrchestraTestCase
{
/**
* @return void
*/
public function setUp(): void
{
parent::setUp();
}
/**
* @param $app
* @return string[]
*/
protected function getPackageProviders($app): array
{
return [
LaravelGeoServiceProvider::class,
];
}
/**
* @param $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('laravel-geo-layer.key', 'sua_chave_api');
}
}

@ -0,0 +1,67 @@
<?php
namespace Unit;
use Ae3\LaravelGeoLayer\app\Services\GeocodingService;
use Ae3\LaravelGeoLayer\Tests\TestCase;
use Mockery;
class GeocodingServiceTest extends TestCase
{
/**
* @var GeocodingService
*/
private GeocodingService $geocodingService;
private $mockedGeocoder;
/**
* Set up the test
*/
public function setUp(): void
{
parent::setUp();
$this->mockedGeocoder = Mockery::mock('overload:Spatie\Geocoder\Geocoder');
// Configurando o mock para esperar a chamada de setApiKey
$this->mockedGeocoder->shouldReceive('setApiKey')->once()->withAnyArgs();
$this->mockedGeocoder->shouldReceive('setRegion')->once()->withAnyArgs();
$this->mockedGeocoder->shouldReceive('setCountry')->once()->withAnyArgs();
$this->mockedGeocoder->shouldReceive('setBounds')->once()->withAnyArgs();
// Configurando o mock para esperar a chamada de getCoordinatesForAddress
$this->mockedGeocoder->shouldReceive('getCoordinatesForAddress')
->once()
->with('Brussels, Belgium')
->andReturn(['lat' => '50.85045', 'lng' => '4.34878']);
$this->geocodingService = new GeocodingService();
}
/**
* Test getCoordinatesForAddress function
*/
public function testGetCoordinatesForAddress(): void
{
// Mocking Geocoder response
$this->mockedGeocoder->shouldReceive('getCoordinatesForAddress')
->once()
->with('Brussels, Belgium')
->andReturn(['lat' => '50.85045', 'lng' => '4.34878']);
// Testing the function
$result = $this->geocodingService->getCoordinatesForAddress('Brussels, Belgium');
$this->assertEquals(['lat' => '50.85045', 'lng' => '4.34878'], $result);
}
/**
* Tear down the test
*/
public function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
}
Loading…
Cancel
Save