-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added methods GeoObject::getFullAddressParts(), Response::getFirst(),…
… fixed Api::load() method
- Loading branch information
1 parent
aefceb4
commit 34ae5e3
Showing
6 changed files
with
97 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
/** | ||
* Class Api | ||
* @package Yandex\Geo | ||
* @author Dmitry Kuznetsov <[email protected]> | ||
* @license The MIT License (MIT) | ||
* @see http://api.yandex.ru/maps/doc/geocoder/desc/concepts/About.xml | ||
*/ | ||
|
@@ -56,12 +55,23 @@ public function __construct($version = null) | |
$this->clear(); | ||
} | ||
|
||
public function load() | ||
/** | ||
* @param array $options Curl options | ||
* @return $this | ||
* @throws Exception | ||
* @throws Exception\CurlError | ||
* @throws Exception\ServerError | ||
*/ | ||
public function load(array $options = []) | ||
{ | ||
$apiUrl = sprintf('https://geocode-maps.yandex.ru/%s/?%s', $this->_version, http_build_query($this->_filters)); | ||
$curl = curl_init($apiUrl); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | ||
curl_setopt($curl, CURLOPT_HTTPGET, 1); | ||
$options += array( | ||
CURLOPT_RETURNTRANSFER => 1, | ||
CURLOPT_HTTPGET => 1, | ||
CURLOPT_FOLLOWLOCATION => 1, | ||
); | ||
curl_setopt_array($curl, $options); | ||
$data = curl_exec($curl); | ||
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | ||
if (curl_errno($curl)) { | ||
|
@@ -80,6 +90,7 @@ public function load() | |
} | ||
|
||
$this->_response = new \Yandex\Geo\Response($data); | ||
|
||
return $this; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
/** | ||
* Class Exception | ||
* @package Yandex\Geo | ||
* @author Dmitry Kuznetsov <[email protected]> | ||
* @license The MIT License (MIT) | ||
*/ | ||
class Exception extends \Exception | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
/** | ||
* Class CurlError | ||
* @package Yandex\Geo\Exception | ||
* @author Dmitry Kuznetsov <[email protected]> | ||
* @license The MIT License (MIT) | ||
*/ | ||
class CurlError extends \Yandex\Geo\Exception | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
/** | ||
* Class ServerError | ||
* @package Yandex\Geo\Exception | ||
* @author Dmitry Kuznetsov <[email protected]> | ||
* @license The MIT License (MIT) | ||
*/ | ||
class ServerError extends \Yandex\Geo\Exception | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,19 @@ | |
/** | ||
* Class GeoObject | ||
* @package Yandex\Geo | ||
* @author Dmitry Kuznetsov <[email protected]> | ||
* @license The MIT License (MIT) | ||
*/ | ||
class GeoObject | ||
{ | ||
protected $_addressHierarchy = [ | ||
'Country' => array('AdministrativeArea'), | ||
'AdministrativeArea' => array('SubAdministrativeArea'), | ||
'SubAdministrativeArea' => array('Locality'), | ||
'Locality' => array('DependentLocality', 'Thoroughfare'), | ||
'DependentLocality' => array('DependentLocality', 'Thoroughfare'), | ||
'Thoroughfare' => array('Premise'), | ||
'Premise' => array(), | ||
]; | ||
protected $_data; | ||
protected $_rawData; | ||
|
||
|
@@ -17,11 +25,26 @@ public function __construct(array $rawData) | |
$data = array( | ||
'Address' => $rawData['metaDataProperty']['GeocoderMetaData']['text'], | ||
); | ||
array_walk_recursive($rawData, function($value, $key) use(&$data) { | ||
if (in_array($key, array('CountryName', 'CountryNameCode', 'AdministrativeAreaName', 'SubAdministrativeAreaName', 'LocalityName', 'DependentLocalityName', 'ThoroughfareName', 'PremiseNumber'))) { | ||
$data[$key] = $value; | ||
array_walk_recursive( | ||
$rawData, | ||
function ($value, $key) use (&$data) { | ||
if (in_array( | ||
$key, | ||
array( | ||
'CountryName', | ||
'CountryNameCode', | ||
'AdministrativeAreaName', | ||
'SubAdministrativeAreaName', | ||
'LocalityName', | ||
'DependentLocalityName', | ||
'ThoroughfareName', | ||
'PremiseNumber', | ||
) | ||
)) { | ||
$data[$key] = $value; | ||
} | ||
} | ||
}); | ||
); | ||
if (isset($rawData['Point']['pos'])) { | ||
$pos = explode(' ', $rawData['Point']['pos']); | ||
$data['Longitude'] = (float)$pos[0]; | ||
|
@@ -44,7 +67,7 @@ public function getRawData() | |
{ | ||
return $this->_rawData; | ||
} | ||
|
||
/** | ||
* Обработанные данные | ||
* @return array | ||
|
@@ -151,4 +174,44 @@ public function getPremiseNumber() | |
{ | ||
return isset($this->_data['PremiseNumber']) ? $this->_data['PremiseNumber'] : null; | ||
} | ||
|
||
/** | ||
* Полный адрес | ||
* @return array | ||
*/ | ||
public function getFullAddressParts() | ||
{ | ||
return array_unique( | ||
$this->_parseLevel( | ||
$this->_rawData['metaDataProperty']['GeocoderMetaData']['AddressDetails']['Country'], | ||
'Country' | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* | ||
* @param array $level | ||
* @param String $levelName | ||
* @param array $address | ||
* @return array | ||
*/ | ||
protected function _parseLevel(array $level, $levelName, &$address = []) | ||
{ | ||
if (!isset($this->_addressHierarchy[$levelName])) { | ||
return; | ||
} | ||
|
||
$nameProp = $levelName === 'Premise' ? 'PremiseNumber' : $levelName.'Name'; | ||
$address[] = $level[$nameProp]; | ||
|
||
foreach ($this->_addressHierarchy[$levelName] as $child) { | ||
if (!isset($level[$child])) { | ||
continue; | ||
} | ||
$this->_parseLevel($level[$child], $child, $address); | ||
} | ||
|
||
return $address; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
/** | ||
* Class Response | ||
* @package Yandex\Geo | ||
* @author Dmitry Kuznetsov <[email protected]> | ||
* @license The MIT License (MIT) | ||
*/ | ||
class Response | ||
|
@@ -45,6 +44,19 @@ public function getList() | |
return $this->_list; | ||
} | ||
|
||
/** | ||
* @return null|GeoObject | ||
*/ | ||
public function getFirst() | ||
{ | ||
$result = null; | ||
if (count($this->_list)) { | ||
$result = $this->_list[0]; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Возвращает исходный запрос | ||
* @return string|null | ||
|