Skip to content

Commit 0796196

Browse files
committed
update serialize/unserialize methods for response object
1 parent 24b63f5 commit 0796196

File tree

1 file changed

+48
-84
lines changed

1 file changed

+48
-84
lines changed

source/Yandex/Locator/Response.php

Lines changed: 48 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,134 +8,98 @@
88
* @license The MIT License (MIT)
99
* @see http://api.yandex.ru/locator/doc/dg/api/geolocation-api_json.xml?lang=ru#id04EE5318
1010
*/
11-
class Response implements \Serializable
11+
class Response
1212
{
1313
/**
14-
* @var float Широта в градусах. Имеет десятичное представление с точностью до семи знаков после запятой
14+
* @var array Исходные данные
1515
*/
16-
protected $_latitude;
17-
/**
18-
* @var float Долгота в градусах. Имеет десятичное представление с точностью до семи знаков после запятой
19-
*/
20-
protected $_longitude;
21-
/**
22-
* @var float Высота над поверхностью мирового океана
23-
*/
24-
protected $_altitude;
25-
/**
26-
* @var float Максимальное расстояние от указанной точки, в пределах которого находится мобильное устройство
27-
*/
28-
protected $_precision;
29-
/**
30-
* @var float Максимальное отклонение от указанной высоты
31-
*/
32-
protected $_altitudePrecision;
33-
/**
34-
* @var string Обозначение способа, которым определено местоположение
35-
*/
36-
protected $_type;
16+
protected $_data;
3717

3818
public function __construct(array $data)
3919
{
40-
if (is_array($data)) {
41-
$this->_latitude = isset($data['position']['latitude']) && !empty($data['position']['latitude']) ? (float)$data['position']['latitude'] : 0.0;
42-
$this->_longitude = isset($data['position']['longitude']) && !empty($data['position']['longitude']) ? (float)$data['position']['longitude'] : 0.0;
43-
$this->_altitude = isset($data['position']['altitude']) && !empty($data['position']['altitude']) ? (float)$data['position']['altitude'] : 0.0;
44-
$this->_precision = isset($data['position']['precision']) && !empty($data['position']['precision']) ? (float)$data['position']['precision'] : 0.0;
45-
$this->_altitudePrecision = isset($data['position']['altitude_precision']) && !empty($data['position']['altitude_precision']) ? (float)$data['position']['altitude_precision'] : 0.0;
46-
$this->_type = isset($data['position']['type']) && !empty($data['position']['type']) ? trim($data['position']['type']) : 0.0;
47-
}
48-
}
49-
50-
/**
51-
* (PHP 5 >= 5.1.0)<br/>
52-
* String representation of object
53-
* @link http://php.net/manual/en/serializable.serialize.php
54-
* @see \Serializable::serialize()
55-
* @return string the string representation of the object or null
56-
*/
57-
public function serialize()
58-
{
59-
$data = array();
60-
$reflection = new \ReflectionClass(get_called_class());
61-
foreach ($reflection->getProperties() as $property) {
62-
$property->setAccessible(true);
63-
$key = $property->getName();
64-
$value = $property->getValue($this);
65-
$data[$key] = $value;
66-
}
67-
return serialize($data);
20+
$this->_data = $data;
6821
}
6922

70-
/**
71-
* (PHP 5 >= 5.1.0)<br/>
72-
* Constructs the object
73-
* @link http://php.net/manual/en/serializable.unserialize.php
74-
* @see \Serializable::unserialize()
75-
* @param string $serialized The string representation of the object.
76-
* @return void
77-
*/
78-
public function unserialize($serialized)
23+
public function __sleep()
7924
{
80-
$serialized = unserialize($serialized);
81-
if (is_array($serialized)) {
82-
$reflection = new \ReflectionClass(get_called_class());
83-
foreach ($serialized as $key => $value) {
84-
$property = $reflection->getProperty($key);
85-
$property->setAccessible(true);
86-
$property->setValue($this, $value);
87-
if ($property->isPrivate() || $property->isProtected()) {
88-
$property->setAccessible(false);
89-
}
90-
}
91-
}
25+
return array('_data');
9226
}
9327

9428
/**
95-
* @return float
29+
* Высота над поверхностью мирового океана
30+
* @return float|null
9631
*/
9732
public function getAltitude()
9833
{
99-
return $this->_altitude;
34+
$result = null;
35+
if (isset($data['position']['altitude']) && !empty($data['position']['altitude'])) {
36+
$result = (float)$data['position']['altitude'];
37+
}
38+
return $result;
10039
}
10140

10241
/**
103-
* @return float
42+
* Максимальное отклонение от указанной высоты
43+
* @return float|null
10444
*/
10545
public function getAltitudePrecision()
10646
{
107-
return $this->_altitudePrecision;
47+
$result = null;
48+
if (isset($data['position']['altitude_precision']) && !empty($data['position']['altitude_precision'])) {
49+
$result = (float)$data['position']['altitude_precision'];
50+
}
51+
return $result;
10852
}
10953

11054
/**
111-
* @return float
55+
* Широта в градусах. Имеет десятичное представление с точностью до семи знаков после запятой
56+
* @return float|null
11257
*/
11358
public function getLatitude()
11459
{
115-
return $this->_latitude;
60+
$result = null;
61+
if (isset($data['position']['latitude']) && !empty($data['position']['latitude'])) {
62+
$result = (float)$data['position']['latitude'];
63+
}
64+
return $result;
11665
}
11766

11867
/**
119-
* @return float
68+
* Долгота в градусах. Имеет десятичное представление с точностью до семи знаков после запятой
69+
* @return float|null
12070
*/
12171
public function getLongitude()
12272
{
123-
return $this->_longitude;
73+
$result = null;
74+
if (isset($data['position']['longitude']) && !empty($data['position']['longitude'])) {
75+
$result = (float)$data['position']['longitude'];
76+
}
77+
return $result;
12478
}
12579

12680
/**
127-
* @return float
81+
* Максимальное расстояние от указанной точки, в пределах которого находится мобильное устройство
82+
* @return float|null
12883
*/
12984
public function getPrecision()
13085
{
131-
return $this->_precision;
86+
$result = null;
87+
if (isset($data['position']['precision']) && !empty($data['position']['precision'])) {
88+
$result = (float)$data['position']['precision'];
89+
}
90+
return $result;
13291
}
13392

13493
/**
135-
* @return string
94+
* Обозначение способа, которым определено местоположение
95+
* @return string|null
13696
*/
13797
public function getType()
13898
{
139-
return $this->_type;
99+
$result = null;
100+
if (isset($data['position']['type']) && !empty($data['position']['type'])) {
101+
$result = trim($data['position']['type']);
102+
}
103+
return $result;
140104
}
141105
}

0 commit comments

Comments
 (0)