1
+ <?php
2
+ namespace Yandex \Locator ;
3
+
4
+ /**
5
+ * Class Api
6
+ * @package Yandex\Locator
7
+ * @author Dmitry Kuznetsov <[email protected] >
8
+ * @license The MIT License (MIT)
9
+ * @see http://api.yandex.ru/locator/
10
+ */
11
+ class Api
12
+ {
13
+ /**
14
+ * @var string Версия используемого api
15
+ */
16
+ protected $ _version = '1.0 ' ;
17
+ /**
18
+ * @var array
19
+ */
20
+ protected $ _filters = array ();
21
+ /**
22
+ * @var \Yandex\Locator\Response|null
23
+ */
24
+ protected $ _response ;
25
+
26
+ /**
27
+ * @see http://api.yandex.ru/maps/form.xml
28
+ * @param string $token
29
+ * @param null|string $version
30
+ */
31
+ public function __construct ($ token , $ version = null )
32
+ {
33
+ if (!empty ($ version )) {
34
+ $ this ->_version = (string )$ version ;
35
+ }
36
+ $ this ->_transport = new \Yandex \Locator \Transport ();
37
+ $ this ->_filters ['common ' ] = array (
38
+ 'version ' => $ this ->_version ,
39
+ 'api_key ' => $ token
40
+ );
41
+ $ this ->reset ();
42
+ }
43
+
44
+ /**
45
+ * Сброс фильтров
46
+ * @return self
47
+ */
48
+ public function reset ()
49
+ {
50
+ $ this ->_filters ['gsm_cells ' ] = array ();
51
+ $ this ->_filters ['wifi_networks ' ] = array ();
52
+ $ this ->_filters ['ip ' ] = array ();
53
+ return $ this ;
54
+ }
55
+
56
+ /**
57
+ * @throws Exception
58
+ * @return self
59
+ */
60
+ public function load ()
61
+ {
62
+ $ apiUrl = 'http://api.lbs.yandex.net/geolocation ' ;
63
+ $ data = $ this ->_transport ->post ($ apiUrl , $ this ->_filters );
64
+ if ($ data ['code ' ] == 200 ) {
65
+ $ tmp = json_decode ($ data ['data ' ], true );
66
+ if (!is_null ($ tmp )) {
67
+ $ this ->_response = new \Yandex \Locator \Response ($ tmp );
68
+ } else {
69
+ $ msg = sprintf ('Bad response: %s ' , var_export ($ data , true ));
70
+ throw new \Yandex \Locator \Exception (trim ($ msg ));
71
+ }
72
+ } else {
73
+ $ msg = strip_tags ($ data ['data ' ]);
74
+ throw new \Yandex \Locator \Exception (trim ($ msg ), $ data ['code ' ]);
75
+ }
76
+ return $ this ;
77
+ }
78
+
79
+ /**
80
+ * @return null|Response
81
+ */
82
+ public function getResponse ()
83
+ {
84
+ return $ this ->_response ;
85
+ }
86
+
87
+ /**
88
+ * Добавление описание соты
89
+ * @param int $countryCode Код страны (MCC, Mobile Country Code)
90
+ * @param int $operatorId Код сети мобильной связи (MNC, Mobile Network Code).
91
+ * @param int $cellId Идентификатор соты (CID, Cell Identifier)
92
+ * @param int $lac Код местоположения (LAC, Location area code).
93
+ * @param null|int $signalStrength Уровень сигнала (dBm). Элемент зарезервирован
94
+ * @param null|int $age Время в миллисекундах, прошедшее с момента получения данных. Элемент зарезервирован
95
+ * @return self
96
+ */
97
+ public function setGsmCells ($ countryCode , $ operatorId , $ cellId , $ lac , $ signalStrength = null , $ age = null )
98
+ {
99
+ $ data = array (
100
+ 'countrycode ' => $ countryCode ,
101
+ 'operatorid ' => $ operatorId ,
102
+ 'cellid ' => $ cellId ,
103
+ 'lac ' => $ lac
104
+ );
105
+ if (!empty ($ signalStrength )) {
106
+ $ data ['signal_strength ' ] = $ signalStrength ;
107
+ }
108
+ if (!empty ($ age )) {
109
+ $ data ['age ' ] = $ age ;
110
+ }
111
+ $ this ->_filters ['gsm_cells ' ][] = $ data ;
112
+ return $ this ;
113
+ }
114
+
115
+ /**
116
+ * Описание wi-fi сети
117
+ * @param string $mac MAC-адрес в символьном представлении. Байты могут разделяться дефисом, точкой, двоеточием или указываться слитно без разделителя
118
+ * @param null|int $signalStrength Уровень сигнала (dBm). Элемент зарезервирован
119
+ * @param null|int $age Время в миллисекундах, прошедшее с момента получения данных. Элемент зарезервирован
120
+ * @return self
121
+ */
122
+ public function setWiFiNetworks ($ mac , $ signalStrength = null , $ age = null )
123
+ {
124
+ $ data = array (
125
+ 'mac ' => (string )$ mac
126
+ );
127
+ if (!empty ($ signalStrength )) {
128
+ $ data ['signal_strength ' ] = $ signalStrength ;
129
+ }
130
+ if (!empty ($ age )) {
131
+ $ data ['age ' ] = $ age ;
132
+ }
133
+ $ this ->_filters ['wifi_networks ' ][] = $ data ;
134
+ return $ this ;
135
+ }
136
+
137
+ /**
138
+ * Описание IP
139
+ * @param string $ip IP-адрес мобильного устройства
140
+ * @return self
141
+ */
142
+ public function setIp ($ ip )
143
+ {
144
+ $ this ->_filters ['ip ' ]['address_v4 ' ] = (string )$ ip ;
145
+ return $ this ;
146
+ }
147
+ }
0 commit comments