Completed
Push — master ( c94af5...e42a12 )
by Christian
02:40
created

OpenWeatherMap::getRawWeatherHistory()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 3
Ratio 16.67 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 3
loc 18
ccs 0
cts 16
cp 0
rs 8.8571
cc 5
eloc 12
nc 4
nop 7
crap 30
1
<?php
2
/**
3
 * OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
4
 *
5
 * @license MIT
6
 *
7
 * Please see the LICENSE file distributed with this source code for further
8
 * information regarding copyright and licensing.
9
 *
10
 * Please visit the following links to read about the usage policies and the license of
11
 * OpenWeatherMap before using this class:
12
 *
13
 * @see http://www.OpenWeatherMap.org
14
 * @see http://www.OpenWeatherMap.org/terms
15
 * @see http://openweathermap.org/appid
16
 */
17
18
namespace Cmfcmf;
19
20
use Cmfcmf\OpenWeatherMap\AbstractCache;
21
use Cmfcmf\OpenWeatherMap\CurrentWeather;
22
use Cmfcmf\OpenWeatherMap\Exception as OWMException;
23
use Cmfcmf\OpenWeatherMap\Fetcher\CurlFetcher;
24
use Cmfcmf\OpenWeatherMap\Fetcher\FetcherInterface;
25
use Cmfcmf\OpenWeatherMap\Fetcher\FileGetContentsFetcher;
26
use Cmfcmf\OpenWeatherMap\WeatherForecast;
27
use Cmfcmf\OpenWeatherMap\WeatherHistory;
28
29
/**
30
 * Main class for the OpenWeatherMap-PHP-API. Only use this class.
31
 *
32
 * @api
33
 */
34
class OpenWeatherMap
35
{
36
    /**
37
     * The copyright notice. This is no official text, it was created by
38
     * following the guidelines at http://openweathermap.org/copyright.
39
     *
40
     * @var string $copyright
41
     */
42
    const COPYRIGHT = "Weather data from <a href=\"http://www.openweathermap.org\">OpenWeatherMap.org</a>";
43
44
    /**
45
     * @var string The basic api url to fetch weather data from.
46
     */
47
    private $weatherUrl = 'http://api.openweathermap.org/data/2.5/weather?';
48
49
    /**
50
     * @var string The basic api url to fetch weekly forecast data from.
51
     */
52
    private $weatherHourlyForecastUrl = 'http://api.openweathermap.org/data/2.5/forecast?';
53
54
    /**
55
     * @var string The basic api url to fetch daily forecast data from.
56
     */
57
    private $weatherDailyForecastUrl = 'http://api.openweathermap.org/data/2.5/forecast/daily?';
58
59
    /**
60
     * @var string The basic api url to fetch history weather data from.
61
     */
62
    private $weatherHistoryUrl = 'http://api.openweathermap.org/data/2.5/history/city?';
63
64
    /**
65
     * @var AbstractCache|bool $cache The cache to use.
66
     */
67
    private $cache = false;
68
69
    /**
70
     * @var int
71
     */
72
    private $seconds;
73
74
    /**
75
     * @var bool
76
     */
77
    private $wasCached = false;
78
79
    /**
80
     * @var FetcherInterface The url fetcher.
81
     */
82
    private $fetcher;
83
84
    /**
85
     * @var string
86
     */
87
    private $apiKey = '';
88
89
    /**
90
     * Constructs the OpenWeatherMap object.
91
     *
92
     * @param string                $apiKey  The OpenWeatherMap API key. Required and only optional for BC.
93
     * @param null|FetcherInterface $fetcher The interface to fetch the data from OpenWeatherMap. Defaults to
94
     *                                       CurlFetcher() if cURL is available. Otherwise defaults to
95
     *                                       FileGetContentsFetcher() using 'file_get_contents()'.
96
     * @param bool|string           $cache   If set to false, caching is disabled. Otherwise this must be a class
97
     *                                       extending AbstractCache. Defaults to false.
98
     * @param int $seconds                   How long weather data shall be cached. Default 10 minutes.
99
     *
100
     * @throws \Exception If $cache is neither false nor a valid callable extending Cmfcmf\OpenWeatherMap\Util\Cache.
101
     *
102
     * @api
103
     */
104
    public function __construct($apiKey = '', $fetcher = null, $cache = false, $seconds = 600)
105
    {
106
        if (!is_string($apiKey) || empty($apiKey)) {
107
            // BC
108
            $seconds = $cache !== false ? $cache : 600;
109
            $cache = $fetcher !== null ? $fetcher : false;
110
            $fetcher = $apiKey !== '' ? $apiKey : null;
111
        } else {
112
            $this->apiKey = $apiKey;
113
        }
114
115
        if ($cache !== false && !($cache instanceof AbstractCache)) {
116
            throw new \Exception('The cache class must implement the FetcherInterface!');
117
        }
118
        if (!is_numeric($seconds)) {
119
            throw new \Exception('$seconds must be numeric.');
120
        }
121
        if (!isset($fetcher)) {
122
            $fetcher = (function_exists('curl_version')) ? new CurlFetcher() : new FileGetContentsFetcher();
123
        }
124
        if ($seconds == 0) {
125
            $cache = false;
126
        }
127
128
        $this->cache = $cache;
129
        $this->seconds = $seconds;
0 ignored issues
show
Documentation Bug introduced by
It seems like $seconds can also be of type double or string. However, the property $seconds is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
130
        $this->fetcher = $fetcher;
0 ignored issues
show
Documentation Bug introduced by
It seems like $fetcher can also be of type string. However, the property $fetcher is declared as type object<Cmfcmf\OpenWeathe...tcher\FetcherInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
131
    }
132
133
    /**
134
     * Sets the API Key.
135
     *
136
     * @param string $apiKey API key for the OpenWeatherMap account.
137
     *
138
     * @api
139
     */
140
    public function setApiKey($apiKey)
141
    {
142
        $this->apiKey = $apiKey;
143
    }
144
145
    /**
146
     * Returns the API Key.
147
     *
148
     * @return string
149
     *
150
     * @api
151
     */
152
    public function getApiKey()
153
    {
154
        return $this->apiKey;
155
    }
156
157
    /**
158
     * Returns the current weather at the place you specified.
159
     *
160
     * @param array|int|string $query The place to get weather information for. For possible values see below.
161
     * @param string           $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
162
     * @param string           $lang  The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi.
163
     * @param string           $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
164
     *
165
     * @throws OpenWeatherMap\Exception  If OpenWeatherMap returns an error.
166
     * @throws \InvalidArgumentException If an argument error occurs.
167
     *
168
     * @return CurrentWeather The weather object.
169
     *
170
     * There are three ways to specify the place to get weather information for:
171
     * - Use the city name: $query must be a string containing the city name.
172
     * - Use the city id: $query must be an integer containing the city id.
173
     * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
174
     *
175
     * @api
176
     */
177
    public function getWeather($query, $units = 'imperial', $lang = 'en', $appid = '')
178
    {
179
        $answer = $this->getRawWeatherData($query, $units, $lang, $appid, 'xml');
180
        $xml = $this->parseXML($answer);
0 ignored issues
show
Bug introduced by
It seems like $answer defined by $this->getRawWeatherData..., $lang, $appid, 'xml') on line 179 can also be of type boolean; however, Cmfcmf\OpenWeatherMap::parseXML() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
181
182
        return new CurrentWeather($xml, $units);
183
    }
184
185
    /**
186
     * Returns the forecast for the place you specified. DANGER: Might return
187
     * fewer results than requested due to a bug in the OpenWeatherMap API!
188
     *
189
     * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather.
190
     * @param string           $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
191
     * @param string           $lang  The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi.
192
     * @param string           $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
193
     * @param int              $days  For how much days you want to get a forecast. Default 1, maximum: 16.
194
     *
195
     * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
196
     * @throws \InvalidArgumentException If an argument error occurs.
197
     *
198
     * @return WeatherForecast
199
     *
200
     * @api
201
     */
202
    public function getWeatherForecast($query, $units = 'imperial', $lang = 'en', $appid = '', $days = 1)
203
    {
204
        if ($days <= 5) {
205
            $answer = $this->getRawHourlyForecastData($query, $units, $lang, $appid, 'xml');
206
        } elseif ($days <= 16) {
207
            $answer = $this->getRawDailyForecastData($query, $units, $lang, $appid, 'xml', $days);
208
        } else {
209
            throw new \InvalidArgumentException('Error: forecasts are only available for the next 16 days. $days must be 16 or lower.');
210
        }
211
        $xml = $this->parseXML($answer);
0 ignored issues
show
Bug introduced by
It seems like $answer can also be of type boolean; however, Cmfcmf\OpenWeatherMap::parseXML() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
212
213
        return new WeatherForecast($xml, $units, $days);
214
    }
215
216
    /**
217
     * Returns the DAILY forecast for the place you specified. DANGER: Might return
218
     * fewer results than requested due to a bug in the OpenWeatherMap API!
219
     *
220
     * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather.
221
     * @param string           $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
222
     * @param string           $lang  The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi.
223
     * @param string           $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
224
     * @param int              $days  For how much days you want to get a forecast. Default 1, maximum: 16.
225
     *
226
     * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
227
     * @throws \InvalidArgumentException If an argument error occurs.
228
     *
229
     * @return WeatherForecast
230
     *
231
     * @api
232
     */
233
    public function getDailyWeatherForecast($query, $units = 'imperial', $lang = 'en', $appid = '', $days = 1)
234
    {
235
        if ($days > 16) {
236
            throw new \InvalidArgumentException('Error: forecasts are only available for the next 16 days. $days must be 16 or lower.');
237
        }
238
239
        $answer = $this->getRawDailyForecastData($query, $units, $lang, $appid, 'xml', $days);
240
        $xml = $this->parseXML($answer);
0 ignored issues
show
Bug introduced by
It seems like $answer defined by $this->getRawDailyForeca..., $appid, 'xml', $days) on line 239 can also be of type boolean; however, Cmfcmf\OpenWeatherMap::parseXML() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
241
        return new WeatherForecast($xml, $units, $days);
242
    }
243
244
    /**
245
     * Returns the weather history for the place you specified.
246
     *
247
     * @param array|int|string $query      The place to get weather information for. For possible values see ::getWeather.
248
     * @param \DateTime        $start
249
     * @param int              $endOrCount
250
     * @param string           $type       Can either be 'tick', 'hour' or 'day'.
251
     * @param string           $units      Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
252
     * @param string           $lang       The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi.
253
     * @param string           $appid      Your app id, default ''. See http://openweathermap.org/appid for more details.
254
     *
255
     * @throws OpenWeatherMap\Exception  If OpenWeatherMap returns an error.
256
     * @throws \InvalidArgumentException If an argument error occurs.
257
     *
258
     * @return WeatherHistory
259
     *
260
     * @api
261
     */
262
    public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $units = 'imperial', $lang = 'en', $appid = '')
263
    {
264 View Code Duplication
        if (!in_array($type, array('tick', 'hour', 'day'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
265
            throw new \InvalidArgumentException('$type must be either "tick", "hour" or "day"');
266
        }
267
268
        $xml = json_decode($this->getRawWeatherHistory($query, $start, $endOrCount, $type, $units, $lang, $appid), true);
269
270
        if ($xml['cod'] != 200) {
271
            throw new OWMException($xml['message'], $xml['cod']);
272
        }
273
274
        return new WeatherHistory($xml, $query);
275
    }
276
277
    /**
278
     * Directly returns the xml/json/html string returned by OpenWeatherMap for the current weather.
279
     *
280
     * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather.
281
     * @param string           $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
282
     * @param string           $lang  The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi.
283
     * @param string           $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
284
     * @param string           $mode  The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default).
285
     *
286
     * @return string Returns false on failure and the fetched data in the format you specified on success.
287
     *
288
     * Warning: If an error occurs, OpenWeatherMap ALWAYS returns json data.
289
     *
290
     * @api
291
     */
292
    public function getRawWeatherData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml')
293
    {
294
        $url = $this->buildUrl($query, $units, $lang, $appid, $mode, $this->weatherUrl);
295
296
        return $this->cacheOrFetchResult($url);
297
    }
298
299
    /**
300
     * Directly returns the xml/json/html string returned by OpenWeatherMap for the hourly forecast.
301
     *
302
     * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather.
303
     * @param string           $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
304
     * @param string           $lang  The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi.
305
     * @param string           $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
306
     * @param string           $mode  The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default).
307
     *
308
     * @return string Returns false on failure and the fetched data in the format you specified on success.
309
     *
310
     * Warning: If an error occurs, OpenWeatherMap ALWAYS returns json data.
311
     *
312
     * @api
313
     */
314
    public function getRawHourlyForecastData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml')
315
    {
316
        $url = $this->buildUrl($query, $units, $lang, $appid, $mode, $this->weatherHourlyForecastUrl);
317
318
        return $this->cacheOrFetchResult($url);
319
    }
320
321
    /**
322
     * Directly returns the xml/json/html string returned by OpenWeatherMap for the daily forecast.
323
     *
324
     * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather.
325
     * @param string           $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
326
     * @param string           $lang  The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi.
327
     * @param string           $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
328
     * @param string           $mode  The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default)
329
     * @param int              $cnt   How many days of forecast shall be returned? Maximum (and default): 16
330
     *
331
     * @throws \InvalidArgumentException If $cnt is higher than 16.
332
     *
333
     * @return string Returns false on failure and the fetched data in the format you specified on success.
334
     *
335
     * Warning: If an error occurs, OpenWeatherMap ALWAYS returns json data.
336
     *
337
     * @api
338
     */
339
    public function getRawDailyForecastData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml', $cnt = 16)
340
    {
341
        if ($cnt > 16) {
342
            throw new \InvalidArgumentException('$cnt must be 16 or lower!');
343
        }
344
        $url = $this->buildUrl($query, $units, $lang, $appid, $mode, $this->weatherDailyForecastUrl) . "&cnt=$cnt";
345
346
        return $this->cacheOrFetchResult($url);
347
    }
348
349
    /**
350
     * Directly returns the xml/json/html string returned by OpenWeatherMap for the weather history.
351
     *
352
     * @param array|int|string $query      The place to get weather information for. For possible values see ::getWeather.
353
     * @param \DateTime        $start      The \DateTime object of the date to get the first weather information from.
354
     * @param \DateTime|int    $endOrCount Can be either a \DateTime object representing the end of the period to
355
     *                                     receive weather history data for or an integer counting the number of
356
     *                                     reports requested.
357
     * @param string           $type       The period of the weather history requested. Can be either be either "tick",
358
     *                                     "hour" or "day".
359
     * @param string           $units      Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
360
     * @param string           $lang       The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi.
361
     * @param string           $appid      Your app id, default ''. See http://openweathermap.org/appid for more details.
362
     *
363
     * @throws \InvalidArgumentException
364
     *
365
     * @return string Returns false on failure and the fetched data in the format you specified on success.
366
     *
367
     * Warning If an error occurred, OpenWeatherMap ALWAYS returns data in json format.
368
     *
369
     * @api
370
     */
371
    public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $units = 'imperial', $lang = 'en', $appid = '')
372
    {
373 View Code Duplication
        if (!in_array($type, array('tick', 'hour', 'day'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
374
            throw new \InvalidArgumentException('$type must be either "tick", "hour" or "day"');
375
        }
376
377
        $url = $this->buildUrl($query, $units, $lang, $appid, 'json', $this->weatherHistoryUrl);
378
        $url .= "&type=$type&start={$start->format('U')}";
379
        if ($endOrCount instanceof \DateTime) {
380
            $url .= "&end={$endOrCount->format('U')}";
381
        } elseif (is_numeric($endOrCount) && $endOrCount > 0) {
382
            $url .= "&cnt=$endOrCount";
383
        } else {
384
            throw new \InvalidArgumentException('$endOrCount must be either a \DateTime or a positive integer.');
385
        }
386
387
        return $this->cacheOrFetchResult($url);
388
    }
389
390
    /**
391
     * Returns whether or not the last result was fetched from the cache.
392
     *
393
     * @return bool true if last result was fetched from cache, false otherwise.
394
     */
395
    public function wasCached()
396
    {
397
        return $this->wasCached;
398
    }
399
400
    /**
401
     * @deprecated Use {@link self::getRawWeatherData()} instead.
402
     */
403
    public function getRawData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml')
404
    {
405
        return $this->getRawWeatherData($query, $units, $lang, $appid, $mode);
406
    }
407
408
    /**
409
     * Fetches the result or delivers a cached version of the result.
410
     *
411
     * @param string $url
412
     *
413
     * @return string
414
     */
415
    private function cacheOrFetchResult($url)
416
    {
417
        if ($this->cache !== false) {
418
            /** @var AbstractCache $cache */
419
            $cache = $this->cache;
420
            $cache->setSeconds($this->seconds);
421
            if ($cache->isCached($url)) {
422
                $this->wasCached = true;
423
                return $cache->getCached($url);
424
            }
425
            $result = $this->fetcher->fetch($url);
426
            $cache->setCached($url, $result);
427
        } else {
428
            $result = $this->fetcher->fetch($url);
429
        }
430
        $this->wasCached = false;
431
432
        return $result;
433
    }
434
435
    /**
436
     * Build the url to fetch weather data from.
437
     *
438
     * @param        $query
439
     * @param        $units
440
     * @param        $lang
441
     * @param        $appid
442
     * @param        $mode
443
     * @param string $url   The url to prepend.
444
     *
445
     * @return bool|string The fetched url, false on failure.
446
     */
447
    private function buildUrl($query, $units, $lang, $appid, $mode, $url)
448
    {
449
        $queryUrl = $this->buildQueryUrlParameter($query);
450
451
        $url = $url."$queryUrl&units=$units&lang=$lang&mode=$mode&APPID=";
452
        $url .= empty($appid) ? $this->apiKey : $appid;
453
454
        return $url;
455
    }
456
457
    /**
458
     * Builds the query string for the url.
459
     *
460
     * @param mixed $query
461
     *
462
     * @return string The built query string for the url.
463
     *
464
     * @throws \InvalidArgumentException If the query parameter is invalid.
465
     */
466
    private function buildQueryUrlParameter($query)
467
    {
468
        switch ($query) {
469
            case is_array($query) && isset($query['lat']) && isset($query['lon']) && is_numeric($query['lat']) && is_numeric($query['lon']):
470
                return "lat={$query['lat']}&lon={$query['lon']}";
471
            case is_numeric($query):
472
                return "id=$query";
473
            case is_string($query):
474
                return 'q='.urlencode($query);
475
            default:
476
                throw new \InvalidArgumentException('Error: $query has the wrong format. See the documentation of OpenWeatherMap::getWeather() to read about valid formats.');
477
        }
478
    }
479
480
    /**
481
     * @param string $answer The content returned by OpenWeatherMap.
482
     *
483
     * @return \SimpleXMLElement
484
     * @throws OWMException If the content isn't valid XML.
485
     */
486
    private function parseXML($answer)
487
    {
488
        // Disable default error handling of SimpleXML (Do not throw E_WARNINGs).
489
        libxml_use_internal_errors(true);
490
        libxml_clear_errors();
491
        try {
492
            return new \SimpleXMLElement($answer);
493
        } catch (\Exception $e) {
494
            // Invalid xml format. This happens in case OpenWeatherMap returns an error.
495
            // OpenWeatherMap always uses json for errors, even if one specifies xml as format.
496
            $error = json_decode($answer, true);
497
            if (isset($error['message'])) {
498
                throw new OWMException($error['message'], $error['cod']);
499
            } else {
500
                throw new OWMException('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $answer);
501
            }
502
        }
503
    }
504
}
505