Passed
Push — master ( f3aa4a...79e4eb )
by Davide
40s
created
src/DavidePastore/Ipinfo/Ipinfo.php 1 patch
Indentation   +250 added lines, -250 removed lines patch added patch discarded remove patch
@@ -11,254 +11,254 @@
 block discarded – undo
11 11
  */
12 12
 class Ipinfo
13 13
 {
14
-    /**
15
-     * The base url of the ipinfo service.
16
-     *
17
-     * @var string
18
-     */
19
-    const BASE_URL = 'http://ipinfo.io/';
20
-
21
-    /**
22
-     * The ip string.
23
-     *
24
-     * @var string
25
-     */
26
-    const IP = 'ip';
27
-
28
-    /**
29
-     * The hostname string.
30
-     *
31
-     * @var string
32
-     */
33
-    const HOSTNAME = 'hostname';
34
-
35
-    /**
36
-     * The loc string.
37
-     *
38
-     * @var string
39
-     */
40
-    const LOC = 'loc';
41
-
42
-    /**
43
-     * The org string.
44
-     *
45
-     * @var string
46
-     */
47
-    const ORG = 'org';
48
-
49
-    /**
50
-     * The city string.
51
-     *
52
-     * @var string
53
-     */
54
-    const CITY = 'city';
55
-
56
-    /**
57
-     * The region string.
58
-     *
59
-     * @var string
60
-     */
61
-    const REGION = 'region';
62
-
63
-    /**
64
-     * The country string.
65
-     *
66
-     * @var string
67
-     */
68
-    const COUNTRY = 'country';
69
-
70
-    /**
71
-     * The phone string.
72
-     *
73
-     * @var string
74
-     */
75
-    const PHONE = 'phone';
76
-
77
-    /**
78
-     * The geo string.
79
-     *
80
-     * @var string
81
-     */
82
-    const GEO = 'geo';
83
-
84
-    /**
85
-     * The postal string.
86
-     *
87
-     * @var string
88
-     */
89
-    const POSTAL = 'postal';
90
-
91
-    /**
92
-     * All the settings.
93
-     *
94
-     * @var array
95
-     */
96
-    protected $settings;
97
-
98
-    /**
99
-     * Create an Ipinfo instance.
100
-     *
101
-     * @param array $settings An array with all the settings.
102
-     *                        Supported keys are:
103
-     *                        - token: string the developer token;
104
-     *                        - debug: boolean active or not the debug.
105
-     */
106
-    public function __construct($settings = array())
107
-    {
108
-        //Merge user settings
109
-        $this->settings = array_merge(array(
110
-                'token' => '',
111
-                'debug' => false,
112
-        ), $settings);
113
-    }
114
-
115
-    /**
116
-     * Get all the info about your own ip address.
117
-     *
118
-     * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
119
-     */
120
-    public function getYourOwnIpDetails()
121
-    {
122
-        $response = $this->makeCurlRequest($this::BASE_URL.'json');
123
-        $response = $this->jsonDecodeResponse($response);
124
-
125
-        return new Host($response);
126
-    }
127
-
128
-    /**
129
-     * Get all the info about an ip address.
130
-     *
131
-     * @param string $ipAddress The ip address.
132
-     *
133
-     * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
134
-     */
135
-    public function getFullIpDetails($ipAddress)
136
-    {
137
-        $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress);
138
-        $response = $this->jsonDecodeResponse($response);
139
-
140
-        return new Host($response);
141
-    }
142
-
143
-    /**
144
-     * Get a specific field value.
145
-     *
146
-     * @param string $ipAddress The ip address.
147
-     * @param string $field     The field.
148
-     *
149
-     * @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip.
150
-     *                                           This could returns an Host object if you call it with for the field
151
-     *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
152
-     */
153
-    public function getSpecificField($ipAddress, $field)
154
-    {
155
-        $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field);
156
-        $response = $this->checkGeo($field, $response);
157
-
158
-        return $response;
159
-    }
160
-
161
-    /**
162
-     * Get a specific field value of your own ip address.
163
-     *
164
-     * @param string $field The field.
165
-     *
166
-     * @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip.
167
-     *                                           This could returns an Host object if you call it with for the field
168
-     *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
169
-     */
170
-    public function getYourOwnIpSpecificField($field)
171
-    {
172
-        $response = $this->makeCurlRequest($this::BASE_URL.$field);
173
-        $response = $this->checkGeo($field, $response);
174
-
175
-        return $response;
176
-    }
177
-
178
-    /**
179
-     * Use the /geo call to get just the geolocation information, which will often be
180
-     * faster than getting the full response.
181
-     *
182
-     * @param string $ipAddress The ip address.
183
-     *
184
-     * @return \DavidePastore\Ipinfo\Host
185
-     */
186
-    public function getIpGeoDetails($ipAddress)
187
-    {
188
-        return $this->getSpecificField($ipAddress, $this::GEO);
189
-    }
190
-
191
-    /**
192
-     * Check if the response is GEO and set the parameters accordingly.
193
-     *
194
-     * @param string $field    The field value.
195
-     * @param string $response The response from the server.
196
-     *
197
-     * @return Ambigous <\DavidePastore\Ipinfo\Host, string> Returns an Host object if the request is
198
-     *                  of the GEO type, a string otherwise. If the field value is different from the GEO type, it will
199
-     *                  delete the last character ('\n').
200
-     */
201
-    private function checkGeo($field, $response)
202
-    {
203
-        if ($field == $this::GEO) {
204
-            $response = $this->jsonDecodeResponse($response);
205
-            $response = new Host($response);
206
-        } else {
207
-            $response = substr($response, 0, -1);
208
-        }
209
-
210
-        return $response;
211
-    }
212
-
213
-    /**
214
-     * Make a curl request.
215
-     *
216
-     * @param string $address The address of the request.
217
-     *
218
-     * @return string Returns the response from the request.
219
-     */
220
-    private function makeCurlRequest($address)
221
-    {
222
-        $curl = curl_init();
223
-
224
-        if (!empty($this->settings['token'])) {
225
-            $address .= '?token='.$this->settings['token'];
226
-        }
227
-
228
-        if ($this->settings['debug']) {
229
-            echo 'Request address: '.$address."\n";
230
-        }
231
-
232
-        curl_setopt_array($curl, array(
233
-            CURLOPT_RETURNTRANSFER => 1,
234
-            CURLOPT_URL => $address,
235
-        ));
236
-
237
-        $response = curl_exec($curl);
238
-
239
-        curl_close($curl);
240
-
241
-        return $response;
242
-    }
243
-
244
-    /**
245
-     * Returns the json decoded associative array.
246
-     * @param  string $response Response from the http call.
247
-     * @return array           Returns the associative array with the response.
248
-     * @throws RateLimitExceedException    If you exceed the rate limit.
249
-     */
250
-    private function jsonDecodeResponse($response)
251
-    {
252
-        if ($response) {
253
-            // Check if the response contains an error message
254
-            if (strpos($response, 'Rate limit exceeded.') !== false) {
255
-                throw new RateLimitExceedException("You exceed the rate limit. The complete response is $response");
256
-            } else {
257
-                $response = json_decode($response, true);
258
-            }
259
-        } else {
260
-            $response = array();
261
-        }
262
-        return $response;
263
-    }
14
+	/**
15
+	 * The base url of the ipinfo service.
16
+	 *
17
+	 * @var string
18
+	 */
19
+	const BASE_URL = 'http://ipinfo.io/';
20
+
21
+	/**
22
+	 * The ip string.
23
+	 *
24
+	 * @var string
25
+	 */
26
+	const IP = 'ip';
27
+
28
+	/**
29
+	 * The hostname string.
30
+	 *
31
+	 * @var string
32
+	 */
33
+	const HOSTNAME = 'hostname';
34
+
35
+	/**
36
+	 * The loc string.
37
+	 *
38
+	 * @var string
39
+	 */
40
+	const LOC = 'loc';
41
+
42
+	/**
43
+	 * The org string.
44
+	 *
45
+	 * @var string
46
+	 */
47
+	const ORG = 'org';
48
+
49
+	/**
50
+	 * The city string.
51
+	 *
52
+	 * @var string
53
+	 */
54
+	const CITY = 'city';
55
+
56
+	/**
57
+	 * The region string.
58
+	 *
59
+	 * @var string
60
+	 */
61
+	const REGION = 'region';
62
+
63
+	/**
64
+	 * The country string.
65
+	 *
66
+	 * @var string
67
+	 */
68
+	const COUNTRY = 'country';
69
+
70
+	/**
71
+	 * The phone string.
72
+	 *
73
+	 * @var string
74
+	 */
75
+	const PHONE = 'phone';
76
+
77
+	/**
78
+	 * The geo string.
79
+	 *
80
+	 * @var string
81
+	 */
82
+	const GEO = 'geo';
83
+
84
+	/**
85
+	 * The postal string.
86
+	 *
87
+	 * @var string
88
+	 */
89
+	const POSTAL = 'postal';
90
+
91
+	/**
92
+	 * All the settings.
93
+	 *
94
+	 * @var array
95
+	 */
96
+	protected $settings;
97
+
98
+	/**
99
+	 * Create an Ipinfo instance.
100
+	 *
101
+	 * @param array $settings An array with all the settings.
102
+	 *                        Supported keys are:
103
+	 *                        - token: string the developer token;
104
+	 *                        - debug: boolean active or not the debug.
105
+	 */
106
+	public function __construct($settings = array())
107
+	{
108
+		//Merge user settings
109
+		$this->settings = array_merge(array(
110
+				'token' => '',
111
+				'debug' => false,
112
+		), $settings);
113
+	}
114
+
115
+	/**
116
+	 * Get all the info about your own ip address.
117
+	 *
118
+	 * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
119
+	 */
120
+	public function getYourOwnIpDetails()
121
+	{
122
+		$response = $this->makeCurlRequest($this::BASE_URL.'json');
123
+		$response = $this->jsonDecodeResponse($response);
124
+
125
+		return new Host($response);
126
+	}
127
+
128
+	/**
129
+	 * Get all the info about an ip address.
130
+	 *
131
+	 * @param string $ipAddress The ip address.
132
+	 *
133
+	 * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
134
+	 */
135
+	public function getFullIpDetails($ipAddress)
136
+	{
137
+		$response = $this->makeCurlRequest($this::BASE_URL.$ipAddress);
138
+		$response = $this->jsonDecodeResponse($response);
139
+
140
+		return new Host($response);
141
+	}
142
+
143
+	/**
144
+	 * Get a specific field value.
145
+	 *
146
+	 * @param string $ipAddress The ip address.
147
+	 * @param string $field     The field.
148
+	 *
149
+	 * @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip.
150
+	 *                                           This could returns an Host object if you call it with for the field
151
+	 *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
152
+	 */
153
+	public function getSpecificField($ipAddress, $field)
154
+	{
155
+		$response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field);
156
+		$response = $this->checkGeo($field, $response);
157
+
158
+		return $response;
159
+	}
160
+
161
+	/**
162
+	 * Get a specific field value of your own ip address.
163
+	 *
164
+	 * @param string $field The field.
165
+	 *
166
+	 * @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip.
167
+	 *                                           This could returns an Host object if you call it with for the field
168
+	 *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
169
+	 */
170
+	public function getYourOwnIpSpecificField($field)
171
+	{
172
+		$response = $this->makeCurlRequest($this::BASE_URL.$field);
173
+		$response = $this->checkGeo($field, $response);
174
+
175
+		return $response;
176
+	}
177
+
178
+	/**
179
+	 * Use the /geo call to get just the geolocation information, which will often be
180
+	 * faster than getting the full response.
181
+	 *
182
+	 * @param string $ipAddress The ip address.
183
+	 *
184
+	 * @return \DavidePastore\Ipinfo\Host
185
+	 */
186
+	public function getIpGeoDetails($ipAddress)
187
+	{
188
+		return $this->getSpecificField($ipAddress, $this::GEO);
189
+	}
190
+
191
+	/**
192
+	 * Check if the response is GEO and set the parameters accordingly.
193
+	 *
194
+	 * @param string $field    The field value.
195
+	 * @param string $response The response from the server.
196
+	 *
197
+	 * @return Ambigous <\DavidePastore\Ipinfo\Host, string> Returns an Host object if the request is
198
+	 *                  of the GEO type, a string otherwise. If the field value is different from the GEO type, it will
199
+	 *                  delete the last character ('\n').
200
+	 */
201
+	private function checkGeo($field, $response)
202
+	{
203
+		if ($field == $this::GEO) {
204
+			$response = $this->jsonDecodeResponse($response);
205
+			$response = new Host($response);
206
+		} else {
207
+			$response = substr($response, 0, -1);
208
+		}
209
+
210
+		return $response;
211
+	}
212
+
213
+	/**
214
+	 * Make a curl request.
215
+	 *
216
+	 * @param string $address The address of the request.
217
+	 *
218
+	 * @return string Returns the response from the request.
219
+	 */
220
+	private function makeCurlRequest($address)
221
+	{
222
+		$curl = curl_init();
223
+
224
+		if (!empty($this->settings['token'])) {
225
+			$address .= '?token='.$this->settings['token'];
226
+		}
227
+
228
+		if ($this->settings['debug']) {
229
+			echo 'Request address: '.$address."\n";
230
+		}
231
+
232
+		curl_setopt_array($curl, array(
233
+			CURLOPT_RETURNTRANSFER => 1,
234
+			CURLOPT_URL => $address,
235
+		));
236
+
237
+		$response = curl_exec($curl);
238
+
239
+		curl_close($curl);
240
+
241
+		return $response;
242
+	}
243
+
244
+	/**
245
+	 * Returns the json decoded associative array.
246
+	 * @param  string $response Response from the http call.
247
+	 * @return array           Returns the associative array with the response.
248
+	 * @throws RateLimitExceedException    If you exceed the rate limit.
249
+	 */
250
+	private function jsonDecodeResponse($response)
251
+	{
252
+		if ($response) {
253
+			// Check if the response contains an error message
254
+			if (strpos($response, 'Rate limit exceeded.') !== false) {
255
+				throw new RateLimitExceedException("You exceed the rate limit. The complete response is $response");
256
+			} else {
257
+				$response = json_decode($response, true);
258
+			}
259
+		} else {
260
+			$response = array();
261
+		}
262
+		return $response;
263
+	}
264 264
 }
Please login to merge, or discard this patch.