From a0add9c130c3d5fc01660608dd0a9f4ab614c3ec Mon Sep 17 00:00:00 2001 From: BJ Hansen Date: Fri, 3 May 2024 16:17:06 -0500 Subject: [PATCH] arrays are now returned as Illiuminate Collections; new exception added for Invalid API request --- composer.json | 5 +++-- readme.md | 2 +- src/Api.php | 3 +++ src/Exceptions/InvalidRequestException.php | 10 ++++++++++ src/Features/Alerts.php | 7 ++++--- src/Features/FireZone.php | 9 +++++---- src/Features/ForecastGridData.php | 5 +++-- src/Features/ForecastHourly.php | 5 +++-- src/Features/ForecastOffice.php | 17 +++++++++-------- src/Features/ForecastPeriods.php | 5 +++-- src/Features/ForecastZone.php | 9 +++++---- src/Features/ObservationStations.php | 5 +++-- 12 files changed, 52 insertions(+), 30 deletions(-) create mode 100644 src/Exceptions/InvalidRequestException.php diff --git a/composer.json b/composer.json index 03fbcde..3f449a9 100644 --- a/composer.json +++ b/composer.json @@ -21,9 +21,10 @@ ] }, "require": { - "php": "^8.1", + "php": "^8.2", "guzzlehttp/guzzle": "^7.8", "nesbot/carbon": "^3.2", - "phpfastcache/phpfastcache": "^9.2" + "phpfastcache/phpfastcache": "^9.2", + "illuminate/collections": "^11.6" } } diff --git a/readme.md b/readme.md index 6879570..fdbb0fe 100644 --- a/readme.md +++ b/readme.md @@ -9,7 +9,7 @@ composer require benjaminhansen/nws-php ## Usage Notes -* Requires PHP 8.1 or greater +* Requires PHP 8.2 or greater * All requests can be cached locally to reduce outbound requests to the API endpoint * All datetime fields are automatically returned as a Carbon object * Any timezone fields are automatically returned as a PHP DateTimeZone object diff --git a/src/Api.php b/src/Api.php index fc3f6db..78ec50c 100644 --- a/src/Api.php +++ b/src/Api.php @@ -7,6 +7,7 @@ use NWS\Features\Point; use NWS\Features\ForecastOffice; use NWS\Features\ObservationStation; +use NWS\Exceptions\InvalidRequestException; use DateTimeZone; class Api @@ -164,5 +165,7 @@ public function getLocation(float $lat = null, float $lon = null, string $observ $url = "{$this->base_url}/offices/{$forecast_office}"; return new ForecastOffice($this->get($url), $this); } + + throw new InvalidRequestException("Invalid API request!"); } } diff --git a/src/Exceptions/InvalidRequestException.php b/src/Exceptions/InvalidRequestException.php new file mode 100644 index 0000000..df3f424 --- /dev/null +++ b/src/Exceptions/InvalidRequestException.php @@ -0,0 +1,10 @@ +data->features); + return (boolean) $this->count(); } public function count(): int @@ -38,7 +39,7 @@ public function count(): int return count($this->data->features); } - public function get(): array + public function get(): Collection { $return = []; @@ -46,6 +47,6 @@ public function get(): array $return[] = new Alert($alert, $this->api); } - return $return; + return collect($return); } } diff --git a/src/Features/FireZone.php b/src/Features/FireZone.php index c16c5dd..e265d1a 100644 --- a/src/Features/FireZone.php +++ b/src/Features/FireZone.php @@ -4,6 +4,7 @@ use DateTimeZone; use NWS\Traits\IsCallable; +use Illuminate\Support\Collection; class FireZone { @@ -23,7 +24,7 @@ public function timezone(): DateTimeZone return new DateTimeZone($this->data->properties->timeZone); } - public function forecastOffices(): array + public function forecastOffices(): Collection { $return = []; @@ -31,7 +32,7 @@ public function forecastOffices(): array $return[] = new ForecastOffice($this->api->get($office), $this->api); } - return $return; + return collect($return); } public function forecastOffice(int $i = 0): ForecastOffice @@ -39,7 +40,7 @@ public function forecastOffice(int $i = 0): ForecastOffice return $this->forecastOffices()[$i]; } - public function observationStations(): array + public function observationStations(): Collection { $return = []; @@ -47,7 +48,7 @@ public function observationStations(): array $return[] = new ObservationStation($this->api->get($station), $this->api); } - return $return; + return collect($return); } public function observationStation(int $i = 0): ObservationStation diff --git a/src/Features/ForecastGridData.php b/src/Features/ForecastGridData.php index 708f3e3..f9bf775 100644 --- a/src/Features/ForecastGridData.php +++ b/src/Features/ForecastGridData.php @@ -5,6 +5,7 @@ use NWS\Traits\IsCallable; use NWS\Support\Helpers; use NWS\Support\Carbon; +use Illuminate\Support\Collection; class ForecastGridData { @@ -49,7 +50,7 @@ public function gridY(): int return $this->data->properties->gridY; } - public function temeratures(string $unit = 'f', int $decimal_points = 0): array + public function temeratures(string $unit = 'f', int $decimal_points = 0): Collection { $return = []; @@ -66,6 +67,6 @@ public function temeratures(string $unit = 'f', int $decimal_points = 0): array ]; } - return $return; + return collect($return); } } diff --git a/src/Features/ForecastHourly.php b/src/Features/ForecastHourly.php index 9c23f8f..5d3419c 100644 --- a/src/Features/ForecastHourly.php +++ b/src/Features/ForecastHourly.php @@ -5,6 +5,7 @@ use NWS\Support\Coordinate; use NWS\Traits\IsCallable; use NWS\Support\Carbon; +use Illuminate\Support\Collection; class ForecastHourly { @@ -44,7 +45,7 @@ public function period($i): ForecastPeriod return (new ForecastPeriods($this->data->properties->periods, $this->api))->period($i); } - public function coordinates(): array + public function coordinates(): Collection { $return = []; @@ -56,6 +57,6 @@ public function coordinates(): array } } - return $return; + return collect($return); } } diff --git a/src/Features/ForecastOffice.php b/src/Features/ForecastOffice.php index 08711ff..28a339f 100644 --- a/src/Features/ForecastOffice.php +++ b/src/Features/ForecastOffice.php @@ -3,6 +3,7 @@ namespace NWS\Features; use NWS\Traits\IsCallable; +use Illuminate\Support\Collection; class ForecastOffice { @@ -47,7 +48,7 @@ public function address(): string return $this->data->address; } - public function counties(): array + public function counties(): Collection { $return = []; @@ -55,10 +56,10 @@ public function counties(): array $return[] = new County($this->api->get($county), $this->api); } - return $return; + return collect($return); } - public function forecastZones(): array + public function forecastZones(): Collection { $return = []; @@ -66,10 +67,10 @@ public function forecastZones(): array $return[] = new ForecastZone($this->api->get($zone), $this->api); } - return $return; + return collect($return); } - public function observationStations(): array + public function observationStations(): Collection { $return = []; @@ -77,10 +78,10 @@ public function observationStations(): array $return[] = new ObservationStation($this->api->get($station), $this->api); } - return $return; + return collect($return); } - public function fireZones(): array + public function fireZones(): Collection { $return = []; @@ -88,6 +89,6 @@ public function fireZones(): array $return[] = new FireZone($this->api->get($zone), $this->api); } - return $return; + return collect($return); } } diff --git a/src/Features/ForecastPeriods.php b/src/Features/ForecastPeriods.php index 53e80e9..e8a35f9 100644 --- a/src/Features/ForecastPeriods.php +++ b/src/Features/ForecastPeriods.php @@ -3,6 +3,7 @@ namespace NWS\Features; use NWS\Traits\IsCallable; +use Illuminate\Support\Collection; class ForecastPeriods { @@ -17,7 +18,7 @@ public function __construct($data, $api) $this->api = $api; } - public function get(): array + public function get(): Collection { $return = []; @@ -25,7 +26,7 @@ public function get(): array $return[] = new ForecastPeriod($period, $this->api); } - return $return; + return collect($return); } public function period(int $i): ForecastPeriod diff --git a/src/Features/ForecastZone.php b/src/Features/ForecastZone.php index 2499c2d..2b6f277 100644 --- a/src/Features/ForecastZone.php +++ b/src/Features/ForecastZone.php @@ -4,6 +4,7 @@ use DateTimeZone; use NWS\Traits\IsCallable; +use Illuminate\Support\Collection; class ForecastZone { @@ -23,7 +24,7 @@ public function timezone(int $i = 0): DateTimeZone return $this->timezones()[$i]; } - public function timezones(): array + public function timezones(): Collection { $return = []; @@ -31,7 +32,7 @@ public function timezones(): array $return[] = new DateTimeZone($timezone); } - return $return; + return collect($return); } public function id(): string @@ -65,7 +66,7 @@ public function observationStation(int $i = 0): ObservationStation return $this->observationStations()[$i]; } - public function forecastOffices(): array + public function forecastOffices(): Collection { $return = []; @@ -73,7 +74,7 @@ public function forecastOffices(): array $return[] = new ForecastOffice($this->api->get($office), $this->api); } - return $return; + return collect($return); } public function forecastOffice(int $i = 0): ForecastOffice diff --git a/src/Features/ObservationStations.php b/src/Features/ObservationStations.php index d448209..2cbee97 100644 --- a/src/Features/ObservationStations.php +++ b/src/Features/ObservationStations.php @@ -3,6 +3,7 @@ namespace NWS\Features; use NWS\Traits\IsCallable; +use Illuminate\Support\Collection; class ObservationStations { @@ -17,7 +18,7 @@ public function __construct($data, $api) $this->api = $api; } - public function get(): array + public function get(): Collection { $return = []; @@ -25,7 +26,7 @@ public function get(): array $return[] = new ObservationStation($station, $this->api); } - return $return; + return collect($return); } public function station(int $i = 0): ObservationStation