8
8
* @license The MIT License (MIT)
9
9
* @see http://api.yandex.ru/locator/doc/dg/api/geolocation-api_json.xml?lang=ru#id04EE5318
10
10
*/
11
- class Response implements \Serializable
11
+ class Response
12
12
{
13
13
/**
14
- * @var float Широта в градусах. Имеет десятичное представление с точностью до семи знаков после запятой
14
+ * @var array Исходные данные
15
15
*/
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 ;
37
17
38
18
public function __construct (array $ data )
39
19
{
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 ;
68
21
}
69
22
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 ()
79
24
{
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 ' );
92
26
}
93
27
94
28
/**
95
- * @return float
29
+ * Высота над поверхностью мирового океана
30
+ * @return float|null
96
31
*/
97
32
public function getAltitude ()
98
33
{
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 ;
100
39
}
101
40
102
41
/**
103
- * @return float
42
+ * Максимальное отклонение от указанной высоты
43
+ * @return float|null
104
44
*/
105
45
public function getAltitudePrecision ()
106
46
{
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 ;
108
52
}
109
53
110
54
/**
111
- * @return float
55
+ * Широта в градусах. Имеет десятичное представление с точностью до семи знаков после запятой
56
+ * @return float|null
112
57
*/
113
58
public function getLatitude ()
114
59
{
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 ;
116
65
}
117
66
118
67
/**
119
- * @return float
68
+ * Долгота в градусах. Имеет десятичное представление с точностью до семи знаков после запятой
69
+ * @return float|null
120
70
*/
121
71
public function getLongitude ()
122
72
{
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 ;
124
78
}
125
79
126
80
/**
127
- * @return float
81
+ * Максимальное расстояние от указанной точки, в пределах которого находится мобильное устройство
82
+ * @return float|null
128
83
*/
129
84
public function getPrecision ()
130
85
{
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 ;
132
91
}
133
92
134
93
/**
135
- * @return string
94
+ * Обозначение способа, которым определено местоположение
95
+ * @return string|null
136
96
*/
137
97
public function getType ()
138
98
{
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 ;
140
104
}
141
105
}
0 commit comments