88
99namespace Attogram \Weatherbit ;
1010
11- use Exception ;
1211use function curl_close ;
1312use function curl_exec ;
1413use function curl_getinfo ;
2120
2221class Weatherbit
2322{
24- const VERSION = '2.0.1 ' ;
23+ const VERSION = '2.1.0 ' ;
2524
2625 /**
2726 * @var string - user agent for API requests
@@ -96,13 +95,13 @@ class Weatherbit
9695 * Set Weatherbit API access key
9796 *
9897 * @param string $key
99- * @throws Exception
98+ * @throws WeatherbitException
10099 * @return void
101100 */
102101 public function setKey (string $ key )
103102 {
104103 if (empty ($ key )) {
105- throw new Exception ('Missing API Key ' );
104+ throw new WeatherbitException ('Missing API Key ' );
106105 }
107106 $ this ->key = $ key ;
108107 }
@@ -112,11 +111,13 @@ public function setKey(string $key)
112111 * @see https://www.weatherbit.io/api/requests
113112 *
114113 * @param string $languageCode - 2 letter language code
114+ * @throws WeatherbitException
115+ * @return void
115116 */
116117 public function setLanguage (string $ languageCode )
117118 {
118119 if (empty ($ languageCode ) || strlen ($ languageCode ) != 2 ) {
119- throw new Exception ('Invalid Language Code ' );
120+ throw new WeatherbitException ('Invalid Language Code ' );
120121 }
121122 $ this ->language = $ languageCode ;
122123 }
@@ -126,11 +127,13 @@ public function setLanguage(string $languageCode)
126127 * @see https://www.weatherbit.io/api/requests
127128 *
128129 * @param string $unitsCode - 1 letter units code
130+ * @throws WeatherbitException
131+ * @return void
129132 */
130133 public function setUnits (string $ unitsCode )
131134 {
132135 if (empty ($ unitsCode ) || !in_array ($ unitsCode , ['M ' , 'S ' , 'I ' ])) {
133- throw new Exception ('Invalid Units value. Please use: M, S, or I ' );
136+ throw new WeatherbitException ('Invalid Units value. Please use: M, S, or I ' );
134137 }
135138 $ this ->units = $ unitsCode ;
136139 }
@@ -140,11 +143,13 @@ public function setUnits(string $unitsCode)
140143 *
141144 * @param string $latitude
142145 * @param string $longitude
146+ * @throws WeatherbitException
147+ * @return void
143148 */
144149 public function setLocationByLatitudeLongitude (string $ latitude , string $ longitude )
145150 {
146151 if (empty ($ latitude ) || empty ($ longitude )) {
147- throw new Exception ('Missing latitude and/or longitude ' );
152+ throw new WeatherbitException ('Missing latitude and/or longitude ' );
148153 }
149154
150155 $ this ->location = [
@@ -158,15 +163,17 @@ public function setLocationByLatitudeLongitude(string $latitude, string $longitu
158163 *
159164 * @param string $city
160165 * @param string $country (optional) 2 letter country code
166+ * @throws WeatherbitException
167+ * @return void
161168 */
162169 public function setLocationByCity (string $ city , string $ country = '' )
163170 {
164171 if (empty ($ city )) {
165- throw new Exception ('Invalid City ' );
172+ throw new WeatherbitException ('Invalid City ' );
166173 }
167174
168175 if (!empty ($ country ) && strlen ($ country ) != 2 ) {
169- throw new Exception ('Invalid Country Code ' );
176+ throw new WeatherbitException ('Invalid Country Code ' );
170177 }
171178
172179 $ this ->location ['city ' ] = $ city ;
@@ -252,13 +259,13 @@ public function setLocationByStations(array $weatherStations)
252259 * Get Daily Weather Forecast for 1-16 days in future
253260 *
254261 * @param int $days - Number of days to forecast (optional, default 10)
255- * @throws Exception
262+ * @throws WeatherbitException
256263 * @return array - array of weather forecast data
257264 */
258265 public function getDailyForecast ($ days = 10 ): array
259266 {
260267 if ($ days < 1 || $ days > 16 ) {
261- throw new Exception ('Forecast Days must between 1 and 16 ' );
268+ throw new WeatherbitException ('Forecast Days must between 1 and 16 ' );
262269 }
263270
264271 $ this ->setUrl (
@@ -309,12 +316,13 @@ public function getUrl(): string
309316 *
310317 * @param string $prefix - URL Prefix
311318 * @param array $additional - array of name/value pairs for additional URL values
312- * @throws Exception
319+ * @throws WeatherbitException
320+ * @return void
313321 */
314322 private function setUrl ($ prefix , $ additional = [])
315323 {
316324 if (empty ($ this ->key )) {
317- throw new Exception ('Missing API Key ' );
325+ throw new WeatherbitException ('Missing API Key ' );
318326 }
319327
320328 $ this ->url = self ::PREFIX_API . $ prefix . '?key= ' . urlencode ($ this ->key );
@@ -330,25 +338,27 @@ private function setUrl($prefix, $additional = [])
330338 $ this ->url .= '& ' . $ name . '= ' . urlencode ((string ) $ value );
331339 }
332340 }
333- if (!empty ($ additional )) {
334- foreach ($ additional as $ name => $ value ) {
335- if (!empty ($ value )) {
336- $ this ->url .= '& ' . $ name . '= ' . urlencode ((string ) $ value );
337- }
341+
342+ if (empty ($ additional )) {
343+ return ;
344+ }
345+ foreach ($ additional as $ name => $ value ) {
346+ if (!empty ($ value )) {
347+ $ this ->url .= '& ' . $ name . '= ' . urlencode ((string ) $ value );
338348 }
339349 }
340350 }
341351
342352 /**
343353 * Get Weather Data from the API
344354 *
345- * @throws Exception
355+ * @throws WeatherbitException
346356 * @return array - array of weather data
347357 */
348358 private function get ()
349359 {
350360 if (empty ($ this ->url )) {
351- throw new Exception ('Missing URL for API Call ' );
361+ throw new WeatherbitException ('Missing URL for API Call ' );
352362 }
353363
354364 $ curl = curl_init ($ this ->url );
@@ -360,16 +370,16 @@ private function get()
360370 curl_close ($ curl );
361371
362372 if ($ status != '200 ' ) {
363- throw new Exception ('API Failure - status code: ' . $ status . ' - data: ' . print_r ($ jsonData , true ));
373+ throw new WeatherbitException ('API Failure - status code: ' . $ status . ' - data: ' . print_r ($ jsonData , true ));
364374 }
365375
366376 if (empty ($ jsonData )) {
367- throw new Exception ('No data from API ' );
377+ throw new WeatherbitException ('No data from API ' );
368378 }
369379
370380 $ data = @json_decode ($ jsonData , true ); // @silently ignore decode errors
371381 if (!is_array ($ data )) {
372- throw new Exception ('Unable to decode response from API ' );
382+ throw new WeatherbitException ('Unable to decode response from API ' );
373383 }
374384
375385 return $ data ;
0 commit comments