Skip to content

Commit ae57433

Browse files
committed
Created project
0 parents  commit ae57433

20 files changed

+1241
-0
lines changed

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: php
2+
3+
php:
4+
- 5.6
5+
- 7.1
6+
- 7.2
7+
- 7.3
8+
- 7.4
9+
10+
env:
11+
- LARAVEL_VERSION=5.6.*
12+
- LARAVEL_VERSION=5.7.*
13+
- LARAVEL_VERSION=5.8.*
14+
- LARAVEL_VERSION=6.*
15+
- LARAVEL_VERSION=7.*
16+
- LARAVEL_VERSION=8.*
17+
18+
matrix:
19+
fast_finish: true
20+
21+
before_script:
22+
- composer update
23+
24+
script:
25+
- phpunit

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Dmitry Kovalev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 505 additions & 0 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "movemove-io/dadata",
3+
"description": "Laravel SDK for working with the DaData.RU service API",
4+
"type": "laravel-package",
5+
"license": "MIT",
6+
"version": "1.0.1",
7+
"authors": [
8+
{
9+
"name": "Dmitry Kovalev",
10+
"email": "[email protected]",
11+
"homepage": "https://movemove.io"
12+
}
13+
],
14+
"minimum-stability": "dev",
15+
"autoload": {
16+
"psr-4": {
17+
"MoveMoveIo\\DaData\\": "src/"
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"MoveMoveIo\\DaData\\Tests": "tests/"
23+
}
24+
},
25+
"require": {}
26+
}

config/dadata.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| DaData config
7+
|--------------------------------------------------------------------------
8+
|
9+
| To receive the parameters of the token and the secret, you need to
10+
| register on the https://dadata.ru/ website and get credentials from the cabinet.
11+
|
12+
*/
13+
14+
'token' => env('DADATA_TOKEN', null),
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| Secret key for standardization
19+
|--------------------------------------------------------------------------
20+
*/
21+
'secret' => env('DADATA_SECRET', null),
22+
23+
/*
24+
|--------------------------------------------------------------------------
25+
| DaData timeout
26+
|--------------------------------------------------------------------------
27+
| The maximum number of seconds to wait for a response
28+
*/
29+
'timeout' => env('DADATA_TIMEOUT', 10),
30+
31+
];

src/DaDataAddressService.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace MoveMoveIo\DaData;
4+
5+
use MoveMoveIo\DaData\Enums\Language;
6+
7+
/**
8+
* Class DaDataAddressService
9+
* @package MoveMoveIo\DaData
10+
*/
11+
class DaDataAddressService extends DaDataService
12+
{
13+
14+
/**
15+
* Standardization address from string to FIAS address object
16+
*
17+
* Splits an address from a string into fields (region, city, street, house, apartment)
18+
* according to KLADR/FIAS. Determines the postal code, time zone, nearest subway stations,
19+
* coordinates, apartment cost and other information about the address.
20+
*
21+
* @param string $address
22+
* @return array
23+
* @throws \Exception
24+
*/
25+
public function standardization(string $address) : array
26+
{
27+
return $this->cleanerApi()->post('clean/address', [$address]);
28+
}
29+
30+
/**
31+
* Auto detection by part of address
32+
*
33+
* Helps the person quickly enter the correct address on a web form or app.
34+
* For Russian Federation and the whole world.
35+
*
36+
* @param string $query
37+
* @param int $count
38+
* @param int $language
39+
* @param array $locations
40+
* @param array $locations_geo
41+
* @param array $locations_boost
42+
* @param array $from_bound
43+
* @param array $to_bound
44+
* @return array
45+
* @throws \Exception
46+
*/
47+
public function prompt(
48+
string $query,
49+
int $count = 10,
50+
int $language = Language::RU,
51+
array $locations = [],
52+
array $locations_geo = [],
53+
array $locations_boost = [],
54+
array $from_bound = [],
55+
array $to_bound = []
56+
) : array
57+
{
58+
59+
return $this->suggestApi()->post('rs/suggest/address', [
60+
'query' => $query,
61+
'count' => $count,
62+
'language' => Language::$map[$language] ?? Language::$map[Language::RU],
63+
'locations' => $locations,
64+
'locations_geo' => $locations_geo,
65+
'locations_boost' => $locations_boost,
66+
'from_bound' => $from_bound,
67+
'to_bound' => $to_bound,
68+
]);
69+
}
70+
71+
/**
72+
* GEOcoding
73+
*
74+
* Determines coordinates by address from a string. At the same time it returns the
75+
* postal code and, in general, all data on the address
76+
*
77+
* @param string $address
78+
* @return array
79+
* @throws \Exception
80+
*/
81+
public function geocoding(string $address) : array
82+
{
83+
return $this->cleanerApi()->post(''. [$address]);
84+
}
85+
86+
/**
87+
* Revert GEOcoding
88+
*
89+
* Returns all information about the address by coordinates.
90+
* Works for homes, streets and cities.
91+
*
92+
* @param string $address
93+
* @return array
94+
* @throws \Exception
95+
*/
96+
public function revertGeocoding(string $address) : array
97+
{
98+
return $this->cleanerApi()->post(''. [$address]);
99+
}
100+
101+
/**
102+
* Define city by IPv4
103+
*
104+
* @param string $ipv4
105+
* @return array
106+
* @throws \Exception
107+
*/
108+
public function ipv4coding(string $ipv4) : array
109+
{
110+
return $this->cleanerApi()->post(''. [$ipv4]);
111+
}
112+
113+
}

src/DaDataService.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace MoveMoveIo\DaData;
4+
5+
use MoveMoveIo\DaData\Providers\CleanerDaDataProvider;
6+
use MoveMoveIo\DaData\Providers\SuggestDaDataProvider;
7+
8+
/**
9+
* Class DaDataService
10+
* @package MoveMoveIo\DaData
11+
*/
12+
class DaDataService
13+
{
14+
15+
/**
16+
* @var string|null
17+
*/
18+
protected $token;
19+
20+
/**
21+
* @var string|null
22+
*/
23+
protected $secret;
24+
25+
/**
26+
* @var int
27+
*/
28+
protected $timeout;
29+
30+
/**
31+
* @var CleanerDaDataProvider
32+
*/
33+
protected $cleanerApi;
34+
35+
/**
36+
* @var SuggestDaDataProvider
37+
*/
38+
protected $suggestApi;
39+
40+
/**
41+
* DaDataService constructor.
42+
*/
43+
public function __construct()
44+
{
45+
$this->token = config('dadata.token');
46+
$this->secret = config('dadata.secret');
47+
$this->timeout = config('dadata.timeout');
48+
}
49+
50+
/**
51+
* @param string $v
52+
* @return CleanerDaDataProvider
53+
*/
54+
public function cleanerApi(string $v = 'v1') : CleanerDaDataProvider
55+
{
56+
if (! $this->cleanerApi instanceof CleanerDaDataProvider)
57+
{
58+
$this->cleanerApi = new CleanerDaDataProvider($this->token, $this->secret, $this->timeout, $v);
59+
}
60+
61+
return $this->cleanerApi;
62+
}
63+
64+
/**
65+
* @param string $v
66+
* @return SuggestDaDataProvider
67+
*/
68+
public function suggestApi(string $v = '4_1') : SuggestDaDataProvider
69+
{
70+
if (! $this->suggestApi instanceof SuggestDaDataProvider) {
71+
$this->suggestApi = new SuggestDaDataProvider($this->token, $this->secret, $this->timeout, $v);
72+
}
73+
74+
return $this->suggestApi;
75+
}
76+
77+
}

src/DaDataServiceProvider.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace MoveMoveIo\DaData;
4+
5+
use Carbon\Laravel\ServiceProvider;
6+
7+
/**
8+
* Class DaDataServiceProvider
9+
* @package MoveMoveIo\DaData
10+
*/
11+
class DaDataServiceProvider extends ServiceProvider
12+
{
13+
14+
/**
15+
* Register services.
16+
*
17+
* @return void
18+
*/
19+
public function register()
20+
{
21+
$this->app->bind('da_data_address', function () {
22+
return new DaDataAddressService();
23+
});
24+
}
25+
26+
/**
27+
* Bootstrap services.
28+
*
29+
* @return void
30+
*/
31+
public function boot()
32+
{
33+
$this->publishes([
34+
__DIR__ . '/../config/dadata.php' => config_path('dadata.php'),
35+
]);
36+
}
37+
38+
39+
}

src/Enums/Language.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace MoveMoveIo\DaData\Enums;
4+
5+
/**
6+
* Class Language
7+
* @package MoveMoveIo\DaData\Enums
8+
*/
9+
class Language
10+
{
11+
12+
const RU = 1;
13+
const EN = 2;
14+
15+
public static $map = [
16+
self::RU => 'ru',
17+
self::EN => 'en',
18+
];
19+
20+
}

src/Exceptions/Handler.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace MoveMoveIo\DaData\Exceptions;
4+
5+
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
6+
7+
class Handler extends ExceptionHandler
8+
{
9+
public function register()
10+
{
11+
12+
}
13+
}

0 commit comments

Comments
 (0)