From 5f9c18f5a09b032938ea7c73b6e0a894221e4cc4 Mon Sep 17 00:00:00 2001 From: Konstantin Vasilev Date: Wed, 13 Nov 2019 23:55:18 +0300 Subject: [PATCH] Added result wrapper, fix readme --- README.md | 77 +++++---- examples/search.php | 2 +- src/PhoneInfo.php | 50 +++++- src/RegionResult.php | 389 +++++++++++++++++++++++++++++++++++++++++++ src/SearchResult.php | 153 +++++++++++++++++ 5 files changed, 628 insertions(+), 43 deletions(-) create mode 100644 src/RegionResult.php create mode 100644 src/SearchResult.php diff --git a/README.md b/README.md index 9d8ef01..81beaf3 100644 --- a/README.md +++ b/README.md @@ -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 ) ) diff --git a/examples/search.php b/examples/search.php index 8438ff3..f636081 100644 --- a/examples/search.php +++ b/examples/search.php @@ -12,7 +12,7 @@ $options = [ 'logger' => $logger, - 'libphonenumber' => true, +// 'libphonenumber' => true, // 'libphonenumber' => PhoneNumberUtil::getInstance(), ]; diff --git a/src/PhoneInfo.php b/src/PhoneInfo.php index deae988..88e9f11 100644 --- a/src/PhoneInfo.php +++ b/src/PhoneInfo.php @@ -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(); @@ -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() @@ -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; + } } \ No newline at end of file diff --git a/src/RegionResult.php b/src/RegionResult.php new file mode 100644 index 0000000..339b0ca --- /dev/null +++ b/src/RegionResult.php @@ -0,0 +1,389 @@ +country; + } + + /** + * @param string|null $country + */ + public function setCountry(?string $country): void + { + $this->country = $country; + } + + /** + * @return string|null + */ + public function getCountryIsoCode(): ?string + { + return $this->countryIsoCode; + } + + /** + * @param string|null $countryIsoCode + */ + public function setCountryIsoCode(?string $countryIsoCode): void + { + $this->countryIsoCode = $countryIsoCode; + } + + /** + * @return string|null + */ + public function getFederalDistrict(): ?string + { + return $this->federalDistrict; + } + + /** + * @param string|null $federalDistrict + */ + public function setFederalDistrict(?string $federalDistrict): void + { + $this->federalDistrict = $federalDistrict; + } + + /** + * @return string|null + */ + public function getFiasCode(): ?string + { + return $this->fiasCode; + } + + /** + * @param string|null $fiasCode + */ + public function setFiasCode(?string $fiasCode): void + { + $this->fiasCode = $fiasCode; + } + + /** + * @return int|null + */ + public function getFiasLevel(): ?int + { + return $this->fiasLevel; + } + + /** + * @param int|null $fiasLevel + */ + public function setFiasLevel(?int $fiasLevel): void + { + $this->fiasLevel = $fiasLevel; + } + + /** + * @return int|null + */ + public function getGeoLat(): ?int + { + return $this->geoLat; + } + + /** + * @param int|null $geoLat + */ + public function setGeoLat(?int $geoLat): void + { + $this->geoLat = $geoLat; + } + + /** + * @return int|null + */ + public function getGeoLon(): ?int + { + return $this->geoLon; + } + + /** + * @param int|null $geoLon + */ + public function setGeoLon(?int $geoLon): void + { + $this->geoLon = $geoLon; + } + + /** + * @return int|null + */ + public function getKladrId(): ?int + { + return $this->kladrId; + } + + /** + * @param int|null $kladrId + */ + public function setKladrId(?int $kladrId): void + { + $this->kladrId = $kladrId; + } + + /** + * @return int|null + */ + public function getOkato(): ?int + { + return $this->okato; + } + + /** + * @param int|null $okato + */ + public function setOkato(?int $okato): void + { + $this->okato = $okato; + } + + /** + * @return int|null + */ + public function getOktmo(): ?int + { + return $this->oktmo; + } + + /** + * @param int|null $oktmo + */ + public function setOktmo(?int $oktmo): void + { + $this->oktmo = $oktmo; + } + + /** + * @return int|null + */ + public function getPostalCode(): ?int + { + return $this->postalCode; + } + + /** + * @param int|null $postalCode + */ + public function setPostalCode(?int $postalCode): void + { + $this->postalCode = $postalCode; + } + + /** + * @return string|null + */ + public function getRegionName(): ?string + { + return $this->regionName; + } + + /** + * @param string|null $regionName + */ + public function setRegionName(?string $regionName): void + { + $this->regionName = $regionName; + } + + /** + * @return string|null + */ + public function getRegionFiasId(): ?string + { + return $this->regionFiasId; + } + + /** + * @param string|null $regionFiasId + */ + public function setRegionFiasId(?string $regionFiasId): void + { + $this->regionFiasId = $regionFiasId; + } + + /** + * @return string|null + */ + public function getRegionIsoCode(): ?string + { + return $this->regionIsoCode; + } + + /** + * @param string|null $regionIsoCode + */ + public function setRegionIsoCode(?string $regionIsoCode): void + { + $this->regionIsoCode = $regionIsoCode; + } + + /** + * @return int|null + */ + public function getRegionKladrId(): ?int + { + return $this->regionKladrId; + } + + /** + * @param int|null $regionKladrId + */ + public function setRegionKladrId(?int $regionKladrId): void + { + $this->regionKladrId = $regionKladrId; + } + + /** + * @return string|null + */ + public function getRegionType(): ?string + { + return $this->regionType; + } + + /** + * @param string|null $regionType + */ + public function setRegionType(?string $regionType): void + { + $this->regionType = $regionType; + } + + /** + * @return string|null + */ + public function getResult(): ?string + { + return $this->result; + } + + /** + * @param string|null $result + */ + public function setResult(?string $result): void + { + $this->result = $result; + } + + /** + * @return string|null + */ + public function getTimezone(): ?string + { + return $this->timezone; + } + + /** + * @param string|null $timezone + */ + public function setTimezone(?string $timezone): void + { + $this->timezone = $timezone; + } + + /** + * @return int|null + */ + public function getUpdated(): ?int + { + return $this->updated; + } + + /** + * @param int|null $updated + */ + public function setUpdated(?int $updated): void + { + $this->updated = $updated; + } +} \ No newline at end of file diff --git a/src/SearchResult.php b/src/SearchResult.php new file mode 100644 index 0000000..9c3ea91 --- /dev/null +++ b/src/SearchResult.php @@ -0,0 +1,153 @@ +code; + } + + /** + * @param int|null $code + */ + public function setCode(?int $code): void + { + $this->code = $code; + } + + /** + * @return int|null + */ + public function getNumberMin(): ?int + { + return $this->numberMin; + } + + /** + * @param int|null $numberMin + */ + public function setNumberMin(?int $numberMin): void + { + $this->numberMin = $numberMin; + } + + /** + * @return int|null + */ + public function getNumberMax(): ?int + { + return $this->numberMax; + } + + /** + * @param int|null $numberMax + */ + public function setNumberMax(?int $numberMax): void + { + $this->numberMax = $numberMax; + } + + /** + * @return int|null + */ + public function getRegionId(): ?int + { + return $this->regionId; + } + + /** + * @param int|null $regionId + */ + public function setRegionId(?int $regionId): void + { + $this->regionId = $regionId; + } + + /** + * @return int|null + */ + public function getOperatorId(): ?int + { + return $this->operatorId; + } + + /** + * @param int|null $operatorId + */ + public function setOperatorId(?int $operatorId): void + { + $this->operatorId = $operatorId; + } + + /** + * @return string|null + */ + public function getOperatorName(): ?string + { + return $this->operatorName; + } + + /** + * @param string|null $operatorName + */ + public function setOperatorName(?string $operatorName): void + { + $this->operatorName = $operatorName; + } + + /** + * @return RegionResult|null + */ + public function getRegion(): ?RegionResult + { + return $this->region; + } + + /** + * @param RegionResult|null $region + */ + public function setRegion(?RegionResult $region): void + { + $this->region = $region; + } +} \ No newline at end of file