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 */
@@ -62,9 +62,9 @@ class OpenWeatherMap
6262 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
@@ -89,20 +89,30 @@ class OpenWeatherMap
8989 /**
9090 * Constructs the OpenWeatherMap object.
9191 *
92- * @param null|FetcherInterface $fetcher The interface to fetch the data from OpenWeatherMap. Defaults to
93- * CurlFetcher() if cURL is available. Otherwise defaults to
94- * FileGetContentsFetcher() using 'file_get_contents()'.
95- * @param bool|string $cacheClass If set to false, caching is disabled. Otherwise this must be a class
96- * extending AbstractCache. Defaults to false.
97- * @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.
9899 *
99100 * @throws \Exception If $cache is neither false nor a valid callable extending Cmfcmf\OpenWeatherMap\Util\Cache.
100101 *
101102 * @api
102103 */
103- public function __construct ($ fetcher = null , $ cacheClass = false , $ seconds = 600 )
104+ public function __construct ($ apiKey = '' , $ fetcher = null , $ cache = false , $ seconds = 600 )
104105 {
105- if ($ cacheClass !== false && !($ cacheClass instanceof AbstractCache)) {
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)) {
106116 throw new \Exception ('The cache class must implement the FetcherInterface! ' );
107117 }
108118 if (!is_numeric ($ seconds )) {
@@ -112,37 +122,37 @@ public function __construct($fetcher = null, $cacheClass = false, $seconds = 600
112122 $ fetcher = (function_exists ('curl_version ' )) ? new CurlFetcher () : new FileGetContentsFetcher ();
113123 }
114124 if ($ seconds == 0 ) {
115- $ cacheClass = false ;
125+ $ cache = false ;
116126 }
117127
118- $ this ->cacheClass = $ cacheClass ;
128+ $ this ->cache = $ cache ;
119129 $ this ->seconds = $ seconds ;
120130 $ this ->fetcher = $ fetcher ;
121131 }
122132
123- /**
124- * Sets the API Key.
125- *
126- * @param string API key for the OpenWeatherMap account making the connection .
127- *
128- * @api
129- */
130- public function setApiKey ($ appid )
131- {
132- $ this ->apiKey = $ appid ;
133- }
134-
135- /**
136- * Returns the API Key.
137- *
138- * @return string
139- *
140- * @api
141- */
142- public function getApiKey ()
143- {
144- return $ this ->apiKey ;
145- }
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+ }
146156
147157 /**
148158 * Returns the current weather at the place you specified as an object.
@@ -162,25 +172,6 @@ public function getApiKey()
162172 * - Use the city id: $query must be an integer containing the city id.
163173 * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
164174 *
165- * Available languages are (as of 17. July 2013):
166- * - English - en
167- * - Russian - ru
168- * - Italian - it
169- * - Spanish - sp
170- * - Ukrainian - ua
171- * - German - de
172- * - Portuguese - pt
173- * - Romanian - ro
174- * - Polish - pl
175- * - Finnish - fi
176- * - Dutch - nl
177- * - French - fr
178- * - Bulgarian - bg
179- * - Swedish - se
180- * - Chinese Traditional - zh_tw
181- * - Chinese Simplified - zh_cn
182- * - Turkish - tr
183- *
184175 * @api
185176 */
186177 public function getWeather ($ query , $ units = 'imperial ' , $ lang = 'en ' , $ appid = '' )
@@ -245,7 +236,7 @@ public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $ty
245236 throw new \InvalidArgumentException ('$type must be either "tick", "hour" or "day" ' );
246237 }
247238
248- $ xml = json_decode ($ this ->getRawWeatherHistory ($ query , $ start , $ endOrCount , $ type , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid ), true );
239+ $ xml = json_decode ($ this ->getRawWeatherHistory ($ query , $ start , $ endOrCount , $ type , $ units , $ lang , $ appid ), true );
249240
250241 if ($ xml ['cod ' ] != 200 ) {
251242 throw new OWMException ($ xml ['message ' ], $ xml ['cod ' ]);
@@ -254,14 +245,6 @@ public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $ty
254245 return new WeatherHistory ($ xml , $ query );
255246 }
256247
257- /**
258- * @deprecated Use {@link self::getRawWeatherData()} instead.
259- */
260- public function getRawData ($ query , $ units = 'imperial ' , $ lang = 'en ' , $ appid = '' , $ mode = 'xml ' )
261- {
262- return $ this ->getRawWeatherData ($ query , $ units , $ lang , $ appid , $ mode );
263- }
264-
265248 /**
266249 * Directly returns the xml/json/html string returned by OpenWeatherMap for the current weather.
267250 *
@@ -362,20 +345,17 @@ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1,
362345 throw new \InvalidArgumentException ('$type must be either "tick", "hour" or "day" ' );
363346 }
364347
365- $ queryUrl = $ this ->weatherHistoryUrl . $ this -> buildQueryUrlParameter ($ query). " &start= { $ start -> format ( ' U ' )}" ;
366-
348+ $ url = $ this ->buildUrl ($ query, $ units , $ lang , $ appid , ' json ' , $ this -> weatherHistoryUrl ) ;
349+ $ url .= " &type= $ type &start= { $ start -> format ( ' U ' )}" ;
367350 if ($ endOrCount instanceof \DateTime) {
368- $ queryUrl .= "&end= {$ endOrCount ->format ('U ' )}" ;
351+ $ url .= "&end= {$ endOrCount ->format ('U ' )}" ;
369352 } elseif (is_numeric ($ endOrCount ) && $ endOrCount > 0 ) {
370- $ queryUrl .= "&cnt= $ endOrCount " ;
353+ $ url .= "&cnt= $ endOrCount " ;
371354 } else {
372355 throw new \InvalidArgumentException ('$endOrCount must be either a \DateTime or a positive integer. ' );
373356 }
374- $ queryUrl .= "&type= $ type&units= $ units&lang= $ lang&APPID= " ;
375-
376- $ queryUrl .= empty ($ appid ) ? $ this ->apiKey : $ appid ;
377357
378- return $ this ->cacheOrFetchResult ($ queryUrl );
358+ return $ this ->cacheOrFetchResult ($ url );
379359 }
380360
381361 /**
@@ -388,6 +368,14 @@ public function wasCached()
388368 return $ this ->wasCached ;
389369 }
390370
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+
391379 /**
392380 * Fetches the result or delivers a cached version of the result.
393381 *
@@ -397,9 +385,9 @@ public function wasCached()
397385 */
398386 private function cacheOrFetchResult ($ url )
399387 {
400- if ($ this ->cacheClass !== false ) {
388+ if ($ this ->cache !== false ) {
401389 /** @var AbstractCache $cache */
402- $ cache = $ this ->cacheClass ;
390+ $ cache = $ this ->cache ;
403391 $ cache ->setSeconds ($ this ->seconds );
404392 if ($ cache ->isCached ($ url )) {
405393 $ this ->wasCached = true ;
0 commit comments