3434class OpenWeatherMap
3535{
3636 /**
37- * The copyright notice. This is no official text, this hint was created
38- * following to http://openweathermap.org/copyright.
37+ * The copyright notice. This is no official text, it was created by
38+ * following the guidelines at http://openweathermap.org/copyright.
3939 *
4040 * @var string $copyright
4141 */
4242 const COPYRIGHT = "Weather data from <a href= \"http://www.openweathermap.org \">OpenWeatherMap.org</a> " ;
4343
4444 /**
45- * @var string $weatherUrl The basic api url to fetch weather data from.
45+ * @var string The basic api url to fetch weather data from.
4646 */
47- private $ weatherUrl = " http://api.openweathermap.org/data/2.5/weather? " ;
47+ private $ weatherUrl = ' http://api.openweathermap.org/data/2.5/weather? ' ;
4848
4949 /**
50- * @var string $url The basic api url to fetch weekly forecast data from.
50+ * @var string The basic api url to fetch weekly forecast data from.
5151 */
52- private $ weatherHourlyForecastUrl = " http://api.openweathermap.org/data/2.5/forecast? " ;
52+ private $ weatherHourlyForecastUrl = ' http://api.openweathermap.org/data/2.5/forecast? ' ;
5353
5454 /**
55- * @var string $url The basic api url to fetch daily forecast data from.
55+ * @var string The basic api url to fetch daily forecast data from.
5656 */
57- private $ weatherDailyForecastUrl = " http://api.openweathermap.org/data/2.5/forecast/daily? " ;
57+ private $ weatherDailyForecastUrl = ' http://api.openweathermap.org/data/2.5/forecast/daily? ' ;
5858
5959 /**
60- * @var string $url The basic api url to fetch history weather data from.
60+ * @var string The basic api url to fetch history weather data from.
6161 */
62- private $ weatherHistoryUrl = " http://api.openweathermap.org/data/2.5/history/city? " ;
62+ private $ weatherHistoryUrl = ' http://api.openweathermap.org/data/2.5/history/city? ' ;
6363
6464 /**
65- * @var AbstractCache|bool $cacheClass The cache class .
65+ * @var AbstractCache|bool $cache The cache to use .
6666 */
67- private $ cacheClass = false ;
67+ private $ cache = false ;
6868
6969 /**
7070 * @var int
@@ -75,47 +75,85 @@ class OpenWeatherMap
7575 * @var bool
7676 */
7777 private $ wasCached = false ;
78-
78+
7979 /**
8080 * @var FetcherInterface The url fetcher.
8181 */
82-
8382 private $ fetcher ;
8483
84+ /**
85+ * @var string
86+ */
87+ private $ apiKey = '' ;
88+
8589 /**
8690 * Constructs the OpenWeatherMap object.
8791 *
88- * @param null|FetcherInterface $fetcher The interface to fetch the data from OpenWeatherMap. Defaults to
89- * CurlFetcher() if cURL is available. Otherwise defaults to
90- * FileGetContentsFetcher() using 'file_get_contents()'.
91- * @param bool|string $cacheClass If set to false, caching is disabled. Otherwise this must be a class
92- * extending AbstractCache. Defaults to false.
93- * @param int $seconds How long weather data shall be cached. Default 10 minutes.
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.
9499 *
95100 * @throws \Exception If $cache is neither false nor a valid callable extending Cmfcmf\OpenWeatherMap\Util\Cache.
96101 *
97102 * @api
98103 */
99- public function __construct ($ fetcher = null , $ cacheClass = false , $ seconds = 600 )
104+ public function __construct ($ apiKey = '' , $ fetcher = null , $ cache = false , $ seconds = 600 )
100105 {
101- if ($ cacheClass !== false && !($ cacheClass instanceof AbstractCache)) {
102- throw new \Exception ("The cache class must implement the FetcherInterface! " );
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! ' );
103117 }
104118 if (!is_numeric ($ seconds )) {
105- throw new \Exception ("\ $seconds must be numeric. " );
119+ throw new \Exception (' $seconds must be numeric. ' );
106120 }
107121 if (!isset ($ fetcher )) {
108122 $ fetcher = (function_exists ('curl_version ' )) ? new CurlFetcher () : new FileGetContentsFetcher ();
109123 }
110124 if ($ seconds == 0 ) {
111- $ cacheClass = false ;
125+ $ cache = false ;
112126 }
113127
114- $ this ->cacheClass = $ cacheClass ;
128+ $ this ->cache = $ cache ;
115129 $ this ->seconds = $ seconds ;
116130 $ this ->fetcher = $ fetcher ;
117131 }
118132
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+
119157 /**
120158 * Returns the current weather at the place you specified as an object.
121159 *
@@ -124,7 +162,7 @@ public function __construct($fetcher = null, $cacheClass = false, $seconds = 600
124162 * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi.
125163 * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
126164 *
127- * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
165+ * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
128166 * @throws \InvalidArgumentException If an argument error occurs.
129167 *
130168 * @return CurrentWeather The weather object.
@@ -177,15 +215,15 @@ public function getWeatherForecast($query, $units = 'imperial', $lang = 'en', $a
177215 /**
178216 * Returns the weather history for the place you specified as an object.
179217 *
180- * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather.
218+ * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather.
181219 * @param \DateTime $start
182220 * @param int $endOrCount
183- * @param string $type
184- * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
185- * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi.
186- * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
221+ * @param string $type Can either be 'tick', 'hour' or 'day'.
222+ * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
223+ * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi.
224+ * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
187225 *
188- * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
226+ * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
189227 * @throws \InvalidArgumentException If an argument error occurs.
190228 *
191229 * @return WeatherHistory
@@ -207,14 +245,6 @@ public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $ty
207245 return new WeatherHistory ($ xml , $ query );
208246 }
209247
210- /**
211- * @deprecated Use {@link self::getRawWeatherData()} instead.
212- */
213- public function getRawData ($ query , $ units = 'imperial ' , $ lang = 'en ' , $ appid = '' , $ mode = 'xml ' )
214- {
215- return $ this ->getRawWeatherData ($ query , $ units , $ lang , $ appid , $ mode );
216- }
217-
218248 /**
219249 * Directly returns the xml/json/html string returned by OpenWeatherMap for the current weather.
220250 *
@@ -270,6 +300,7 @@ public function getRawHourlyForecastData($query, $units = 'imperial', $lang = 'e
270300 * @param int $cnt How many days of forecast shall be returned? Maximum (and default): 16
271301 *
272302 * @throws \InvalidArgumentException If $cnt is higher than 16.
303+ *
273304 * @return string Returns false on failure and the fetched data in the format you specified on success.
274305 *
275306 * Warning: If an error occurs, OpenWeatherMap ALWAYS returns json data.
@@ -289,16 +320,16 @@ public function getRawDailyForecastData($query, $units = 'imperial', $lang = 'en
289320 /**
290321 * Directly returns the xml/json/html string returned by OpenWeatherMap for the weather history.
291322 *
292- * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather.
293- * @param \DateTime $start The \DateTime object of the date to get the first weather information from.
294- * @param \DateTime|int $endOrCount Can be either a \DateTime object representing the end of the period to
295- * receive weather history data for or an integer counting the number of
296- * reports requested.
297- * @param string $type The period of the weather history requested. Can be either be either "tick",
298- * "hour" or "day".
299- * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
300- * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi.
301- * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
323+ * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather.
324+ * @param \DateTime $start The \DateTime object of the date to get the first weather information from.
325+ * @param \DateTime|int $endOrCount Can be either a \DateTime object representing the end of the period to
326+ * receive weather history data for or an integer counting the number of
327+ * reports requested.
328+ * @param string $type The period of the weather history requested. Can be either be either "tick",
329+ * "hour" or "day".
330+ * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
331+ * @param string $lang The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi.
332+ * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
302333 *
303334 * @throws \InvalidArgumentException
304335 *
@@ -314,22 +345,17 @@ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1,
314345 throw new \InvalidArgumentException ('$type must be either "tick", "hour" or "day" ' );
315346 }
316347
317- $ queryUrl = $ this ->weatherHistoryUrl . $ this -> buildQueryUrlParameter ( $ query ) . " &start= { $ start -> format ( ' U ' )}" ;
318-
348+ $ url = $ this ->buildUrl ( $ query , $ units , $ lang , $ appid , ' json ' , $ this -> weatherHistoryUrl ) ;
349+ $ url .= " &type= $ type &start= { $ start -> format ( ' U ' )}" ;
319350 if ($ endOrCount instanceof \DateTime) {
320- $ queryUrl .= "&end= {$ endOrCount ->format ('U ' )}" ;
351+ $ url .= "&end= {$ endOrCount ->format ('U ' )}" ;
321352 } elseif (is_numeric ($ endOrCount ) && $ endOrCount > 0 ) {
322- $ queryUrl .= "&cnt= $ endOrCount " ;
353+ $ url .= "&cnt= $ endOrCount " ;
323354 } else {
324355 throw new \InvalidArgumentException ('$endOrCount must be either a \DateTime or a positive integer. ' );
325356 }
326- $ queryUrl .= "&type= $ type&units= $ units&lang= $ lang " ;
327357
328- if (!empty ($ appid )) {
329- $ queryUrl .= "&APPID= $ appid " ;
330- }
331-
332- return $ this ->cacheOrFetchResult ($ queryUrl );
358+ return $ this ->cacheOrFetchResult ($ url );
333359 }
334360
335361 /**
@@ -342,6 +368,14 @@ public function wasCached()
342368 return $ this ->wasCached ;
343369 }
344370
371+ /**
372+ * @deprecated Use {@link self::getRawWeatherData()} instead.
373+ */
374+ public function getRawData ($ query , $ units = 'imperial ' , $ lang = 'en ' , $ appid = '' , $ mode = 'xml ' )
375+ {
376+ return $ this ->getRawWeatherData ($ query , $ units , $ lang , $ appid , $ mode );
377+ }
378+
345379 /**
346380 * Fetches the result or delivers a cached version of the result.
347381 *
@@ -351,9 +385,9 @@ public function wasCached()
351385 */
352386 private function cacheOrFetchResult ($ url )
353387 {
354- if ($ this ->cacheClass !== false ) {
388+ if ($ this ->cache !== false ) {
355389 /** @var AbstractCache $cache */
356- $ cache = $ this ->cacheClass ;
390+ $ cache = $ this ->cache ;
357391 $ cache ->setSeconds ($ this ->seconds );
358392 if ($ cache ->isCached ($ url )) {
359393 $ this ->wasCached = true ;
@@ -377,18 +411,16 @@ private function cacheOrFetchResult($url)
377411 * @param $lang
378412 * @param $appid
379413 * @param $mode
380- * @param string $url The url to prepend.
414+ * @param string $url The url to prepend.
381415 *
382416 * @return bool|string The fetched url, false on failure.
383417 */
384418 private function buildUrl ($ query , $ units , $ lang , $ appid , $ mode , $ url )
385419 {
386420 $ queryUrl = $ this ->buildQueryUrlParameter ($ query );
387421
388- $ url = $ url . "$ queryUrl&units= $ units&lang= $ lang&mode= $ mode " ;
389- if (!empty ($ appid )) {
390- $ url .= "&APPID= $ appid " ;
391- }
422+ $ url = $ url ."$ queryUrl&units= $ units&lang= $ lang&mode= $ mode&APPID= " ;
423+ $ url .= empty ($ appid ) ? $ this ->apiKey : $ appid ;
392424
393425 return $ url ;
394426 }
@@ -399,6 +431,7 @@ private function buildUrl($query, $units, $lang, $appid, $mode, $url)
399431 * @param mixed $query
400432 *
401433 * @return string The built query string for the url.
434+ *
402435 * @throws \InvalidArgumentException If the query parameter is invalid.
403436 */
404437 private function buildQueryUrlParameter ($ query )
@@ -409,9 +442,9 @@ private function buildQueryUrlParameter($query)
409442 case is_numeric ($ query ):
410443 return "id= $ query " ;
411444 case is_string ($ query ):
412- return " q= " . urlencode ($ query );
445+ return ' q= ' . urlencode ($ query );
413446 default :
414- throw new \InvalidArgumentException ('Error: $query has the wrong format. See the documentation of OpenWeatherMap::getRawData () to read about valid formats. ' );
447+ throw new \InvalidArgumentException ('Error: $query has the wrong format. See the documentation of OpenWeatherMap::getWeather () to read about valid formats. ' );
415448 }
416449 }
417450
0 commit comments