Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
kolirt committed Jul 27, 2023
0 parents commit dbfebb6
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 kolirt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Laravel Openstreetmap

## Installation
```
$ composer require kolirt/laravel-openstreetmap
```
```
$ php artisan openstreetmap:install
```

## Methods

#### Details by place_id
```
Kolirt\Openstreetmap\Facade\Openstreetmap::details(int $place_id);
```

#### Reverse
```
Kolirt\Openstreetmap\Facade\Openstreetmap::reverse(float $lat, float $lng);
```

#### Search
```
Kolirt\Openstreetmap\Facade\Openstreetmap::search(string $q, int $limit = 10);
```

#### Search by params
```
Kolirt\Openstreetmap\Facade\Openstreetmap::searchByParams($streetname = null,
$housenumber = null,
$city = null,
$state = null,
$country = null,
$postalcode = null,
int $limit = 10);
```
33 changes: 33 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "kolirt/laravel-openstreetmap",
"description": "Package for openstreetmap.org",
"keywords": ["laravel", "openstreetmap", "map"],
"license": "MIT",
"version": "1.0.3",
"authors": [
{
"name": "kolirt"
}
],
"require": {
"guzzlehttp/guzzle": "> 6.0.0"
},
"autoload": {
"files": [
"src/helpers.php"
],
"psr-4": {
"Kolirt\\Openstreetmap\\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Kolirt\\Openstreetmap\\ServiceProvider"
],
"aliases": {
"Openstreetmap": "Kolirt\\Openstreetmap\\Facade\\Openstreetmap"
}
}
}
}
6 changes: 6 additions & 0 deletions config/openstreetmap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'lang' => 'en',
'timeout' => 1
];
32 changes: 32 additions & 0 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Kolirt\Openstreetmap\Commands;

use Illuminate\Console\Command;

class InstallCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'openstreetmap:install';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Instalation Openstreetmap package';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->call('vendor:publish', ['--provider' => 'Kolirt\\Openstreetmap\\ServiceProvider']);
}
}
21 changes: 21 additions & 0 deletions src/Facade/Openstreetmap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Kolirt\Openstreetmap\Facade;

use Illuminate\Support\Facades\Facade;

/**
* @method static object|null details(int $place_id)
* @method static object|null reverse(float $lat, float $lng)
* @method static array|null search(string $q, int $limit = 10)
* @method static array|null searchByParams($streetname = null, $housenumber = null, $city = null, $state = null, $country = null, $postalcode = null, int $limit = 10)
*/
class Openstreetmap extends Facade
{

protected static function getFacadeAccessor()
{
return 'openstreetmap';
}

}
112 changes: 112 additions & 0 deletions src/Openstreetmap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Kolirt\Openstreetmap;

use GuzzleHttp\Client;

class Openstreetmap
{

private $client;
private $api = 'https://nominatim.openstreetmap.org/';

public function __construct()
{
$this->client = new Client([
'base_uri' => $this->api,
'timeout' => config('openstreetmap.timeout', 1)
]);
}

public function details(int $place_id)
{
try {
$request = $this->client->get('details', [
'query' => [
'place_id' => $place_id,
'format' => 'json',
'accept-language' => config('openstreetmap.lang')
]
]);

if ($request->getStatusCode() == 200) {
return json_decode($request->getBody()->getContents());
}
} catch (\Exception $e) {
return null;
}
}

public function reverse(float $lat, float $lng)
{
try {
$request = $this->client->get('reverse', [
'query' => [
'lat' => $lat,
'lon' => $lng,
'format' => 'json',
'accept-language' => config('openstreetmap.lang')
]
]);

if ($request->getStatusCode() == 200) {
$result = json_decode($request->getBody()->getContents());

if (is_null($result->error ?? null)) {
return $result;
}

return null;
}
} catch (\Exception $e) {
return null;
}
}

public function search(string $q, int $limit = 10)
{
try {
$request = $this->client->get('search', [
'query' => [
'q' => $q,
'polygon_geojson' => 1,
'limit' => $limit,
'format' => 'json',
'accept-language' => config('openstreetmap.lang')
]
]);

if ($request->getStatusCode() == 200) {
return json_decode($request->getBody()->getContents());
}
} catch (\Exception $e) {
return null;
}
}

public function searchByParams($streetname = null, $housenumber = null, $city = null, $state = null, $country = null, $postalcode = null, int $limit = 10)
{
try {
$request = $this->client->get('search', [
'query' => [
'street' => $housenumber . ' ' . $streetname,
'city' => $city,
'state' => $state,
'country' => $country,
'postalcode' => $postalcode,
'polygon_geojson' => 1,
'limit' => $limit,
'format' => 'json',
'accept-language' => config('openstreetmap.lang')
]
]);

if ($request->getStatusCode() == 200) {
return json_decode($request->getBody()->getContents());
}
} catch (\Exception $e) {
return null;
}
}

}
36 changes: 36 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Kolirt\Openstreetmap;

use Illuminate\Support\ServiceProvider as BaseServiceProvider;

class ServiceProvider extends BaseServiceProvider
{
protected $commands = [
Commands\InstallCommand::class
];

/**
* Bootstrap any application services.
*/
public function boot()
{
$this->mergeConfigFrom(__DIR__ . '/../config/openstreetmap.php', 'openstreetmap');

$this->publishes([
__DIR__ . '/../config/openstreetmap.php' => config_path('openstreetmap.php')
]);
}

/**
* Register any application services.
*/
public function register()
{
$this->commands($this->commands);

app()->bind('openstreetmap', function(){
return new Openstreetmap();
});
}
}
2 changes: 2 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php

0 comments on commit dbfebb6

Please sign in to comment.