Skip to content

Commit

Permalink
Added result wrapper, fix readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Onefivefournine committed Nov 13, 2019
1 parent 6a53421 commit 5f9c18f
Show file tree
Hide file tree
Showing 5 changed files with 628 additions and 43 deletions.
77 changes: 42 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,63 @@
# Phone standardization library
**PhoneInfo** is an php library to get information by russian phone number.
**PhoneInfo** is a php library to get information by russian phone number.

## Goal
Goal of this project is get information about russian phone easy and free.
Regional information takes from https://dadata.ru
Goal of this project is to get information about russian phone easy and free.
Regional information taken from https://dadata.ru

## Examples
Examples located at example directory.

## Base
Database stored localy (storage/default.sqlite) in sqlite3 database.
At this time only sqlite3 (PDO) allowed to storage data.
Database stored locally (storage/default.sqlite) in sqlite3 database.
For now only sqlite3 (PDO) is supported.

## Usage

- Installation via composer
```composer require alexmorbo/phoneinfo```
```
composer require alexmorbo/phoneinfo
```
- Create library object
```$library = new PhoneInfo();```
```
use PhoneLib\PhoneInfo;
$library = new PhoneInfo();
```
- Get information about phone
```$info = $library->search('79213333333');```
```
$info = $library->search('79213333333');
```
- Profit
```
Array
PhoneLib\SearchResult Object
(
[code] => 921
[number_min] => 79213000000
[number_max] => 79214499999
[region_id] => 2105
[operator_id] => 14
[operator] => ПАО "МегаФон"
[region] => Array
[code:PhoneLib\SearchResult:private] => 921
[numberMin:PhoneLib\SearchResult:private] => 79213000000
[numberMax:PhoneLib\SearchResult:private] => 79214499999
[regionId:PhoneLib\SearchResult:private] => 2105
[operatorId:PhoneLib\SearchResult:private] => 14
[operatorName:PhoneLib\SearchResult:private] => ПАО "МегаФон"
[region:PhoneLib\SearchResult:private] => PhoneLib\RegionResult Object
(
[country] => Россия
[country_iso_code] => RU
[federal_district] => Северо-Западный
[fias_code] => 78000000000000000000000
[fias_level] => 1
[geo_lat] => 59.9391313
[geo_lon] => 30.3159004
[kladr_id] => 7800000000000
[okato] => 40000000000
[oktmo] => 40000000
[postal_code] => 190000
[region] => Санкт-Петербург
[region_fias_id] => c2deb16a-0330-4f05-821f-1d09c93331e6
[region_iso_code] => RU-SPE
[region_kladr_id] => 7800000000000
[region_type] => г
[result] => г Санкт-Петербург
[timezone] => UTC+3
[updated] => 1573643679
[country:PhoneLib\RegionResult:private] => Россия
[countryIsoCode:PhoneLib\RegionResult:private] => RU
[federalDistrict:PhoneLib\RegionResult:private] => Северо-Западный
[fiasCode:PhoneLib\RegionResult:private] => 78000000000000000000000
[fiasLevel:PhoneLib\RegionResult:private] => 1
[geoLat:PhoneLib\RegionResult:private] => 59
[geoLon:PhoneLib\RegionResult:private] => 30
[kladrId:PhoneLib\RegionResult:private] => 7800000000000
[okato:PhoneLib\RegionResult:private] => 40000000000
[oktmo:PhoneLib\RegionResult:private] => 40000000
[postalCode:PhoneLib\RegionResult:private] => 190000
[regionName:PhoneLib\RegionResult:private] => Санкт-Петербург
[regionFiasId:PhoneLib\RegionResult:private] => c2deb16a-0330-4f05-821f-1d09c93331e6
[regionIsoCode:PhoneLib\RegionResult:private] => RU-SPE
[regionKladrId:PhoneLib\RegionResult:private] => 7800000000000
[regionType:PhoneLib\RegionResult:private] => г
[result:PhoneLib\RegionResult:private] => г Санкт-Петербург
[timezone:PhoneLib\RegionResult:private] => UTC+3
[updated:PhoneLib\RegionResult:private] => 1573643679
)
)
Expand Down
2 changes: 1 addition & 1 deletion examples/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

$options = [
'logger' => $logger,
'libphonenumber' => true,
// 'libphonenumber' => true,
// 'libphonenumber' => PhoneNumberUtil::getInstance(),
];

Expand Down
50 changes: 43 additions & 7 deletions src/PhoneInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private function prepare()
*
* @throws NumberParseException
*/
public function search(string $digits, string $region = 'RU'): array
public function search(string $digits, string $region = 'RU'): SearchResult
{
try {
$this->prepare();
Expand Down Expand Up @@ -231,21 +231,21 @@ public function search(string $digits, string $region = 'RU'): array
'where data.region_id = regions.id and data.operator_id = operators.id and '.$phone.' between number_min and number_max';
$st = $this->db->query($query);

$result = $st->fetch();
if (! $result) {
$data = $st->fetch();
if (! $data) {
return ['code' => -3, 'err' => 'Ничего не найдено'];
}

$result = array_merge(['phone' => $phone], $result);
$data = array_merge(['phone' => $phone], $data);

/**
* add region data
*/
$st = $this->db->prepare('select type, data from region_data where region_id = ?');
$st->execute([$result['region_id']]);
$result['region'] = $st->fetchAll(PDO::FETCH_KEY_PAIR);
$st->execute([$data['region_id']]);
$data['region'] = $st->fetchAll(PDO::FETCH_KEY_PAIR);

return $result;
return $this->formatDataToResult($data);
}

public function update()
Expand Down Expand Up @@ -617,4 +617,40 @@ private function updateRegionData(array $regions)
);
}
}

private function formatDataToResult(array $data): SearchResult
{
$result = new SearchResult();
if(isset($data['region'])) {
$region = new RegionResult();
$region->setCountry($data['region']['country']);
$region->setCountryIsoCode($data['region']['country_iso_code']);
$region->setFederalDistrict($data['region']['federal_district']);
$region->setFiasCode($data['region']['fias_code']);
$region->setFiasLevel($data['region']['fias_level']);
$region->setGeoLat($data['region']['geo_lat']);
$region->setGeoLon($data['region']['geo_lon']);
$region->setKladrId($data['region']['kladr_id']);
$region->setOkato($data['region']['okato']);
$region->setOktmo($data['region']['oktmo']);
$region->setPostalCode($data['region']['postal_code']);
$region->setRegionName($data['region']['region']);
$region->setRegionFiasId($data['region']['region_fias_id']);
$region->setRegionIsoCode($data['region']['region_iso_code']);
$region->setRegionKladrId($data['region']['region_kladr_id']);
$region->setRegionType($data['region']['region_type']);
$region->setResult($data['region']['result']);
$region->setTimezone($data['region']['timezone']);
$region->setUpdated($data['region']['updated']);
$result->setRegion($region);
$result->setRegionId($data['region_id']);
}

$result->setCode($data['code']);
$result->setNumberMax($data['number_max']);
$result->setNumberMin($data['number_min']);
$result->setOperatorId($data['operator_id']);
$result->setOperatorName($data['operator']);
return $result;
}
}
Loading

0 comments on commit 5f9c18f

Please sign in to comment.