Skip to content

Commit 22e8adf

Browse files
authored
Merge pull request #10 from attogram/exceptions
WeatherbitException
2 parents e57313a + 8352610 commit 22e8adf

File tree

4 files changed

+67
-36
lines changed

4 files changed

+67
-36
lines changed

public/example.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
ini_set('display_startup_errors', '1');
1313
error_reporting(E_ALL);
1414

15-
require('../src/Weatherbit.php'); // or: require('path/to/vendor/autoload.php');
15+
require('../vendor/autoload.php');
1616

1717
$data = [];
1818

@@ -83,7 +83,7 @@
8383
break;
8484
}
8585
} catch (Exception $error) {
86-
$error = $error->getMessage();
86+
$error = get_class($error) . ': ' . $error->getMessage();
8787
}
8888

8989
printResults($response, $error);

src/Weatherbit.php

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace Attogram\Weatherbit;
1010

11-
use Exception;
1211
use function curl_close;
1312
use function curl_exec;
1413
use function curl_getinfo;
@@ -21,7 +20,7 @@
2120

2221
class 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;

src/WeatherbitException.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Custom Exception for Weatherbit
4+
*/
5+
declare(strict_types = 1);
6+
7+
namespace Attogram\Weatherbit;
8+
9+
class WeatherbitException extends \Exception
10+
{
11+
}

tests/WeatherbitTest.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use PHPUnit\Framework\TestCase;
1010
use Attogram\Weatherbit\Weatherbit;
11+
use Attogram\Weatherbit\WeatherbitException;
1112

1213
final class WeatherbitTest extends TestCase
1314
{
@@ -16,15 +17,17 @@ final class WeatherbitTest extends TestCase
1617
*/
1718
protected $weatherbit;
1819

20+
// @TODO test setUp() against phpunit 6
1921
protected function setWeatherbit()
2022
{
2123
if (!$this->weatherbit) {
2224
$this->weatherbit = new Weatherbit();
2325
}
2426
}
2527

26-
public function testClass()
28+
public function testWeatherbitClass()
2729
{
30+
$this->assertTrue(class_exists('Attogram\Weatherbit\Weatherbit'));
2831
$this->setWeatherbit();
2932
$this->assertInstanceOf(Weatherbit::class, $this->weatherbit);
3033
$this->assertTrue(is_string(Weatherbit::VERSION));
@@ -41,6 +44,13 @@ public function testClass()
4144
$this->assertClassHasAttribute('url', Weatherbit::class);
4245
}
4346

47+
public function testClassWeatherbitException()
48+
{
49+
$this->assertTrue(class_exists('\Attogram\Weatherbit\WeatherbitException'));
50+
$this->expectException(WeatherbitException::class);
51+
throw new WeatherbitException();
52+
}
53+
4454
public function testSetKey()
4555
{
4656
$this->setWeatherbit();
@@ -51,7 +61,7 @@ public function testSetKey()
5161
public function testSetKeyEmptyString()
5262
{
5363
$this->setWeatherbit();
54-
$this->expectException(Exception::class);
64+
$this->expectException(WeatherbitException::class);
5565
$this->weatherbit->setKey('');
5666
}
5767

@@ -72,7 +82,7 @@ public function testSetLanguage()
7282
public function testSetLanguageEmptyString()
7383
{
7484
$this->setWeatherbit();
75-
$this->expectException(Exception::class);
85+
$this->expectException(WeatherbitException::class);
7686
$this->weatherbit->setLanguage('');
7787
}
7888

@@ -86,14 +96,14 @@ public function testSetLanguageNotString()
8696
public function testSetLanguageTooShort()
8797
{
8898
$this->setWeatherbit();
89-
$this->expectException(Exception::class);
99+
$this->expectException(WeatherbitException::class);
90100
$this->weatherbit->setLanguage('a');
91101
}
92102

93103
public function testSetLanguageTooLong()
94104
{
95105
$this->setWeatherbit();
96-
$this->expectException(Exception::class);
106+
$this->expectException(WeatherbitException::class);
97107
$this->weatherbit->setLanguage('abc');
98108
}
99109

@@ -109,7 +119,7 @@ public function testSetUnits()
109119
public function testSetUnitsEmptyString()
110120
{
111121
$this->setWeatherbit();
112-
$this->expectException(Exception::class);
122+
$this->expectException(WeatherbitException::class);
113123
$this->weatherbit->setUnits('');
114124
}
115125

@@ -123,7 +133,7 @@ public function testSetUnitsNotString()
123133
public function testSetUnitsNonCode()
124134
{
125135
$this->setWeatherbit();
126-
$this->expectException(Exception::class);
136+
$this->expectException(WeatherbitException::class);
127137
$this->weatherbit->setUnits('X');
128138
}
129139

@@ -137,7 +147,7 @@ public function testSetLocationByLatitudeLongitude()
137147
public function testSetLocationByLatitudeLongitudeEmptyStrings()
138148
{
139149
$this->setWeatherbit();
140-
$this->expectException(Exception::class);
150+
$this->expectException(WeatherbitException::class);
141151
$this->weatherbit->setLocationByLatitudeLongitude('', '');
142152
}
143153

@@ -172,14 +182,14 @@ public function testSetLocationByCityStateCountry()
172182
public function testSetLocationByCityEmptyString()
173183
{
174184
$this->setWeatherbit();
175-
$this->expectException(Exception::class);
185+
$this->expectException(WeatherbitException::class);
176186
$this->weatherbit->setLocationByCity('');
177187
}
178188

179189
public function testSetLocationByCityEmptyStrings()
180190
{
181191
$this->setWeatherbit();
182-
$this->expectException(Exception::class);
192+
$this->expectException(WeatherbitException::class);
183193
$this->weatherbit->setLocationByCity('', '');
184194
}
185195

@@ -200,7 +210,7 @@ public function testSetLocationByCityNotStrings()
200210
public function testSetLocationByCityInvalidCountry()
201211
{
202212
$this->setWeatherbit();
203-
$this->expectException(Exception::class);
213+
$this->expectException(WeatherbitException::class);
204214
$this->weatherbit->setLocationByCity('Amsterdam', 'NLD');
205215
}
206216

0 commit comments

Comments
 (0)