Skip to content

Commit

Permalink
Added methods GeoObject::getFullAddressParts(), Response::getFirst(),…
Browse files Browse the repository at this point in the history
… fixed Api::load() method
  • Loading branch information
dmitry2080 committed Apr 10, 2016
1 parent aefceb4 commit 34ae5e3
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 14 deletions.
19 changes: 15 additions & 4 deletions source/Yandex/Geo/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)) {
Expand All @@ -80,6 +90,7 @@ public function load()
}

$this->_response = new \Yandex\Geo\Response($data);

return $this;
}

Expand Down
1 change: 0 additions & 1 deletion source/Yandex/Geo/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
/**
* Class Exception
* @package Yandex\Geo
* @author Dmitry Kuznetsov <[email protected]>
* @license The MIT License (MIT)
*/
class Exception extends \Exception
Expand Down
1 change: 0 additions & 1 deletion source/Yandex/Geo/Exception/CurlError.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion source/Yandex/Geo/Exception/ServerError.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 69 additions & 6 deletions source/Yandex/Geo/GeoObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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];
Expand All @@ -44,7 +67,7 @@ public function getRawData()
{
return $this->_rawData;
}

/**
* Обработанные данные
* @return array
Expand Down Expand Up @@ -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;
}
}
14 changes: 13 additions & 1 deletion source/Yandex/Geo/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
/**
* Class Response
* @package Yandex\Geo
* @author Dmitry Kuznetsov <[email protected]>
* @license The MIT License (MIT)
*/
class Response
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 34ae5e3

Please sign in to comment.