3434class OpenWeatherMap
3535{
3636 /**
37- * @var string $weatherUrl The basic api url to fetch weather data from.
37+ * @var string The basic api url to fetch weather data from.
3838 */
39- private $ weatherUrl = " http://api.openweathermap.org/data/2.5/weather? " ;
39+ private $ weatherUrl = ' http://api.openweathermap.org/data/2.5/weather? ' ;
4040
4141 /**
42- * @var string $url The basic api url to fetch weekly forecast data from.
42+ * @var string The basic api url to fetch weekly forecast data from.
4343 */
44- private $ weatherHourlyForecastUrl = " http://api.openweathermap.org/data/2.5/forecast? " ;
44+ private $ weatherHourlyForecastUrl = ' http://api.openweathermap.org/data/2.5/forecast? ' ;
4545
4646 /**
47- * @var string $url The basic api url to fetch daily forecast data from.
47+ * @var string The basic api url to fetch daily forecast data from.
4848 */
49- private $ weatherDailyForecastUrl = " http://api.openweathermap.org/data/2.5/forecast/daily? " ;
49+ private $ weatherDailyForecastUrl = ' http://api.openweathermap.org/data/2.5/forecast/daily? ' ;
5050
5151 /**
52- * @var string $url The basic api url to fetch history weather data from.
52+ * @var string The basic api url to fetch history weather data from.
5353 */
54- private $ weatherHistoryUrl = " http://api.openweathermap.org/data/2.5/history/city? " ;
54+ private $ weatherHistoryUrl = ' http://api.openweathermap.org/data/2.5/history/city? ' ;
5555
5656 /**
5757 * The copyright notice. This is no official text, this hint was created regarding to http://openweathermap.org/copyright.
5858 *
59- * @var string $copyright
59+ * @var string
6060 */
61- const COPYRIGHT = " Weather data from <a href= \ "http://www.openweathermap.org \ ">OpenWeatherMap.org</a> " ;
61+ const COPYRIGHT = ' Weather data from <a href="http://www.openweathermap.org">OpenWeatherMap.org</a> ' ;
6262
6363 /**
64- * @var \Cmfcmf\OpenWeatherMap\AbstractCache|bool $cacheClass The cache class.
64+ * @var \Cmfcmf\OpenWeatherMap\AbstractCache|bool The cache class.
6565 */
6666 private $ cacheClass = false ;
6767
@@ -74,24 +74,21 @@ class OpenWeatherMap
7474 * @var bool
7575 */
7676 private $ wasCached = false ;
77-
77+
7878 /**
7979 * @var FetcherInterface The url fetcher.
8080 */
81-
8281 private $ fetcher ;
8382
8483 /**
85- * @var $apiKey.
84+ * @var string
8685 */
87-
8886 private $ apiKey = '' ;
8987
9088 /**
9189 * Constructs the OpenWeatherMap object.
9290 *
9391 * @param null|string $appid The API key. Defaults to null.
94- *
9592 * @param null|FetcherInterface $fetcher The interface to fetch the data from OpenWeatherMap. Defaults to
9693 * CurlFetcher() if cURL is available. Otherwise defaults to
9794 * FileGetContentsFetcher() using 'file_get_contents()'.
@@ -100,15 +97,16 @@ class OpenWeatherMap
10097 * @param int $seconds How long weather data shall be cached. Default 10 minutes.
10198 *
10299 * @throws \Exception If $cache is neither false nor a valid callable extending Cmfcmf\OpenWeatherMap\Util\Cache.
100+ *
103101 * @api
104102 */
105- public function __construct ($ appid = null , $ fetcher = null , $ cacheClass = false , $ seconds = 600 )
103+ public function __construct ($ fetcher = null , $ cacheClass = false , $ seconds = 600 )
106104 {
107105 if ($ cacheClass !== false && !($ cacheClass instanceof AbstractCache)) {
108- throw new \Exception (" The cache class must implement the FetcherInterface! " );
106+ throw new \Exception (' The cache class must implement the FetcherInterface! ' );
109107 }
110108 if (!is_numeric ($ seconds )) {
111- throw new \Exception ("\ $seconds must be numeric. " );
109+ throw new \Exception (' $seconds must be numeric. ' );
112110 }
113111 if (!isset ($ fetcher )) {
114112 $ fetcher = (function_exists ('curl_version ' )) ? new CurlFetcher () : new FileGetContentsFetcher ();
@@ -117,30 +115,30 @@ public function __construct($appid = null, $fetcher = null, $cacheClass = false,
117115 $ cacheClass = false ;
118116 }
119117
120- if (!is_null ($ appid )) {
121- $ this ->apiKey = $ appid ;
122- }
123118 $ this ->cacheClass = $ cacheClass ;
124119 $ this ->seconds = $ seconds ;
125120 $ this ->fetcher = $ fetcher ;
126121 }
127122
128- /**
129- * Sets the API Key
130- * @param string API key for the OpenWeatherMap account making the connection.
131- *
132- * @api
133- */
123+ /**
124+ * Sets the API Key.
125+ *
126+ * @param string API key for the OpenWeatherMap account making the connection.
127+ *
128+ * @api
129+ */
134130 public function setApiKey ($ appid )
135131 {
136132 $ this ->apiKey = $ appid ;
137133 }
138134
139- /**
140- * Returns the API Key
141- *
142- * @api
143- */
135+ /**
136+ * Returns the API Key.
137+ *
138+ * @return string
139+ *
140+ * @api
141+ */
144142 public function getApiKey ()
145143 {
146144 return $ this ->apiKey ;
@@ -154,7 +152,7 @@ public function getApiKey()
154152 * @param string $lang The language to use for descriptions, default is 'en'. For possible values see below.
155153 * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
156154 *
157- * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
155+ * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
158156 * @throws \InvalidArgumentException If an argument error occurs.
159157 *
160158 * @return CurrentWeather The weather object.
@@ -191,7 +189,7 @@ public function getWeather($query, $units = 'imperial', $lang = 'en', $appid = '
191189 libxml_use_internal_errors (true );
192190 libxml_clear_errors ();
193191
194- $ answer = $ this ->getRawWeatherData ($ query , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid , 'xml ' );
192+ $ answer = $ this ->getRawWeatherData ($ query , $ units , $ lang , $ appid , 'xml ' );
195193
196194 try {
197195 $ xml = new \SimpleXMLElement ($ answer );
@@ -202,7 +200,7 @@ public function getWeather($query, $units = 'imperial', $lang = 'en', $appid = '
202200 if (isset ($ error ['message ' ])) {
203201 throw new OWMException ($ error ['message ' ], $ error ['cod ' ]);
204202 } else {
205- throw new OWMException ('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $ answer );
203+ throw new OWMException ('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $ answer );
206204 }
207205 }
208206
@@ -218,7 +216,7 @@ public function getWeather($query, $units = 'imperial', $lang = 'en', $appid = '
218216 * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
219217 * @param int $days For how much days you want to get a forecast. Default 1, maximum: 16.
220218 *
221- * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
219+ * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
222220 * @throws \InvalidArgumentException If an argument error occurs.
223221 *
224222 * @return WeatherForecast The WeatherForecast object.
@@ -256,9 +254,9 @@ public function getWeatherForecast($query, $units = 'imperial', $lang = 'en', $a
256254 libxml_clear_errors ();
257255
258256 if ($ days <= 5 ) {
259- $ answer = $ this ->getRawHourlyForecastData ($ query , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid , 'xml ' );
257+ $ answer = $ this ->getRawHourlyForecastData ($ query , $ units , $ lang , $ appid , 'xml ' );
260258 } elseif ($ days <= 16 ) {
261- $ answer = $ this ->getRawDailyForecastData ($ query , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid , 'xml ' , $ days );
259+ $ answer = $ this ->getRawDailyForecastData ($ query , $ units , $ lang , $ appid , 'xml ' , $ days );
262260 } else {
263261 throw new \InvalidArgumentException ('Error: forecasts are only available for the next 16 days. $days must be lower than 17. ' );
264262 }
@@ -272,7 +270,7 @@ public function getWeatherForecast($query, $units = 'imperial', $lang = 'en', $a
272270 if (isset ($ error ['message ' ])) {
273271 throw new OWMException ($ error ['message ' ], $ error ['cod ' ]);
274272 } else {
275- throw new OWMException ('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $ answer );
273+ throw new OWMException ('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $ answer );
276274 }
277275 }
278276
@@ -282,15 +280,15 @@ public function getWeatherForecast($query, $units = 'imperial', $lang = 'en', $a
282280 /**
283281 * Returns the weather history for the place you specified as an object.
284282 *
285- * @param array|int|string $query The place to get weather information for. For possible values see below.
283+ * @param array|int|string $query The place to get weather information for. For possible values see below.
286284 * @param \DateTime $start
287285 * @param int $endOrCount
288286 * @param string $type
289- * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
290- * @param string $lang The language to use for descriptions, default is 'en'. For possible values see below.
291- * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
287+ * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
288+ * @param string $lang The language to use for descriptions, default is 'en'. For possible values see below.
289+ * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
292290 *
293- * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
291+ * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
294292 * @throws \InvalidArgumentException If an argument error occurs.
295293 *
296294 * @return WeatherHistory The WeatherHistory object.
@@ -341,7 +339,7 @@ public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $ty
341339 */
342340 public function getRawData ($ query , $ units = 'imperial ' , $ lang = 'en ' , $ appid = '' , $ mode = 'xml ' )
343341 {
344- return $ this ->getRawWeatherData ($ query , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid , $ mode );
342+ return $ this ->getRawWeatherData ($ query , $ units , $ lang , $ appid , $ mode );
345343 }
346344
347345 /**
@@ -385,7 +383,7 @@ public function getRawData($query, $units = 'imperial', $lang = 'en', $appid = '
385383 */
386384 public function getRawWeatherData ($ query , $ units = 'imperial ' , $ lang = 'en ' , $ appid = '' , $ mode = 'xml ' )
387385 {
388- $ url = $ this ->buildUrl ($ query , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid , $ mode , $ this ->weatherUrl );
386+ $ url = $ this ->buildUrl ($ query , $ units , $ lang , $ appid , $ mode , $ this ->weatherUrl );
389387
390388 return $ this ->cacheOrFetchResult ($ url );
391389 }
@@ -431,7 +429,7 @@ public function getRawWeatherData($query, $units = 'imperial', $lang = 'en', $ap
431429 */
432430 public function getRawHourlyForecastData ($ query , $ units = 'imperial ' , $ lang = 'en ' , $ appid = '' , $ mode = 'xml ' )
433431 {
434- $ url = $ this ->buildUrl ($ query , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid , $ mode , $ this ->weatherHourlyForecastUrl );
432+ $ url = $ this ->buildUrl ($ query , $ units , $ lang , $ appid , $ mode , $ this ->weatherHourlyForecastUrl );
435433
436434 return $ this ->cacheOrFetchResult ($ url );
437435 }
@@ -447,6 +445,7 @@ public function getRawHourlyForecastData($query, $units = 'imperial', $lang = 'e
447445 * @param int $cnt How many days of forecast shall be returned? Maximum (and default): 16
448446 *
449447 * @throws \InvalidArgumentException If $cnt is higher than 16.
448+ *
450449 * @return string Returns false on failure and the fetched data in the format you specified on success.
451450 *
452451 * Warning If an error occurred, OpenWeatherMap returns data in json format ALWAYS
@@ -482,24 +481,24 @@ public function getRawDailyForecastData($query, $units = 'imperial', $lang = 'en
482481 if ($ cnt > 16 ) {
483482 throw new \InvalidArgumentException ('$cnt must be 16 or below! ' );
484483 }
485- $ url = $ this ->buildUrl ($ query , $ units , $ lang , empty ( $ appid) ? $ this -> apiKey : $ appid , $ mode , $ this ->weatherDailyForecastUrl ) . "&cnt= $ cnt " ;
484+ $ url = $ this ->buildUrl ($ query , $ units , $ lang , $ appid , $ mode , $ this ->weatherDailyForecastUrl ). "&cnt= $ cnt " ;
486485
487486 return $ this ->cacheOrFetchResult ($ url );
488487 }
489488
490489 /**
491490 * Directly returns the xml/json/html string returned by OpenWeatherMap for the daily forecast.
492491 *
493- * @param array|int|string $query The place to get weather information for. For possible values see below.
494- * @param \DateTime $start The \DateTime object of the date to get the first weather information from.
495- * @param \DateTime|int $endOrCount Can be either a \DateTime object representing the end of the period to
496- * receive weather history data for or an integer counting the number of
497- * reports requested.
498- * @param string $type The period of the weather history requested. Can be either be either "tick",
499- * "hour" or "day".
500- * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
501- * @param string $lang The language to use for descriptions, default is 'en'. For possible values see below.
502- * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
492+ * @param array|int|string $query The place to get weather information for. For possible values see below.
493+ * @param \DateTime $start The \DateTime object of the date to get the first weather information from.
494+ * @param \DateTime|int $endOrCount Can be either a \DateTime object representing the end of the period to
495+ * receive weather history data for or an integer counting the number of
496+ * reports requested.
497+ * @param string $type The period of the weather history requested. Can be either be either "tick",
498+ * "hour" or "day".
499+ * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
500+ * @param string $lang The language to use for descriptions, default is 'en'. For possible values see below.
501+ * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
503502 *
504503 * @throws \InvalidArgumentException
505504 *
@@ -539,7 +538,7 @@ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1,
539538 throw new \InvalidArgumentException ('$type must be either "tick", "hour" or "day" ' );
540539 }
541540
542- $ queryUrl = $ this ->weatherHistoryUrl . $ this ->buildQueryUrlParameter ($ query ) . "&start= {$ start ->format ('U ' )}" ;
541+ $ queryUrl = $ this ->weatherHistoryUrl . $ this ->buildQueryUrlParameter ($ query ). "&start= {$ start ->format ('U ' )}" ;
543542
544543 if ($ endOrCount instanceof \DateTime) {
545544 $ queryUrl .= "&end= {$ endOrCount ->format ('U ' )}" ;
@@ -570,9 +569,10 @@ private function cacheOrFetchResult($url)
570569 /** @var \Cmfcmf\OpenWeatherMap\AbstractCache $cache */
571570 $ cache = $ this ->cacheClass ;
572571 $ cache ->setSeconds ($ this ->seconds );
573- $ this ->wasCached = false ;
572+ $ this ->wasCached = false ;
574573 if ($ cache ->isCached ($ url )) {
575- $ this ->wasCached =true ;
574+ $ this ->wasCached = true ;
575+
576576 return $ cache ->getCached ($ url );
577577 }
578578 $ result = $ this ->fetcher ->fetch ($ url );
@@ -592,7 +592,7 @@ private function cacheOrFetchResult($url)
592592 * @param $lang
593593 * @param $appid
594594 * @param $mode
595- * @param string $url The url to prepend.
595+ * @param string $url The url to prepend.
596596 *
597597 * @return bool|string The fetched url, false on failure.
598598 *
@@ -602,7 +602,7 @@ private function buildUrl($query, $units, $lang, $appid, $mode, $url)
602602 {
603603 $ queryUrl = $ this ->buildQueryUrlParameter ($ query );
604604
605- $ url = $ url . "$ queryUrl&units= $ units&lang= $ lang&mode= $ mode&APPID= " ;
605+ $ url = $ url. "$ queryUrl&units= $ units&lang= $ lang&mode= $ mode&APPID= " ;
606606 $ url .= empty ($ appid ) ? $ this ->apiKey : $ appid ;
607607
608608 return $ url ;
@@ -614,24 +614,25 @@ private function buildUrl($query, $units, $lang, $appid, $mode, $url)
614614 * @param $query
615615 *
616616 * @return string The built query string for the url.
617+ *
617618 * @throws \InvalidArgumentException If the query parameter is invalid.
618619 *
619620 * @internal
620621 */
621622 private function buildQueryUrlParameter ($ query )
622623 {
623624 switch ($ query ) {
624- case ( is_array ($ query ) && isset ($ query ['lat ' ]) && isset ($ query ['lon ' ]) && is_numeric ($ query ['lat ' ]) && is_numeric ($ query ['lon ' ]) ):
625+ case is_array ($ query ) && isset ($ query ['lat ' ]) && isset ($ query ['lon ' ]) && is_numeric ($ query ['lat ' ]) && is_numeric ($ query ['lon ' ]):
625626 return "lat= {$ query ['lat ' ]}&lon= {$ query ['lon ' ]}" ;
626- case ( is_numeric ($ query) ):
627+ case is_numeric ($ query ):
627628 return "id= $ query " ;
628- case ( is_string ($ query) ):
629- return " q= " . urlencode ($ query );
629+ case is_string ($ query ):
630+ return ' q= ' . urlencode ($ query );
630631 default :
631632 throw new \InvalidArgumentException ('Error: $query has the wrong format. See the documentation of OpenWeatherMap::getRawData() to read about valid formats. ' );
632633 }
633634 }
634-
635+
635636 /**
636637 * Returns whether or not the last result was fetched from the cache.
637638 *
@@ -641,4 +642,4 @@ public function wasCached()
641642 {
642643 return $ this ->wasCached ;
643644 }
644- }
645+ }
0 commit comments