diff --git a/src/Api.php b/src/Api.php index 1f4ba03..559d799 100644 --- a/src/Api.php +++ b/src/Api.php @@ -4,18 +4,11 @@ use Phpfastcache\Helper\Psr16Adapter; use GuzzleHttp\Client as HttpClient; -use BenjaminHansen\NWS\Features\Point; -use BenjaminHansen\NWS\Features\ForecastOffice; -use BenjaminHansen\NWS\Features\ObservationStation; -use BenjaminHansen\NWS\Features\ObservationStations; -use BenjaminHansen\NWS\Features\Glossary; -use BenjaminHansen\NWS\Features\ForecastZone; -use BenjaminHansen\NWS\Exceptions\ApiNotOkException; -use BenjaminHansen\NWS\Exceptions\CacheException; +use BenjaminHansen\NWS\Features\{Point, ForecastOffice, ObservationStation, ObservationStations, Glossary, ForecastZone}; +use BenjaminHansen\NWS\Exceptions\{ApiNotOkException, CacheException}; use BenjaminHansen\NWS\Support\Carbon; use Illuminate\Support\Collection; -use DateInterval; -use DateTimeZone; +use DateInterval, DateTimeZone; class Api { @@ -31,16 +24,8 @@ class Api public function __construct(string $domain, string $email, string|DateTimeZone $timezone = null) { - if($timezone) { - // set the timezone that was passed into the constructor - $this->timezone($timezone); - } else { - // default the api requests to a reasonable timezone - $this->timezone('UTC'); - } - - // set our user agent for the API requests - $this->userAgent($domain, $email); + $this->timezone($timezone ?? 'UTC') // set the timezone that was provided, or default to a reasonable value + ->userAgent($domain, $email); // set our user agent for the API requests // build up our HTTP client for making requests to the API $this->client = new HttpClient([ @@ -239,12 +224,7 @@ public function ok(): bool public function assertOk(string $message = null): self { if(!$this->ok()) { - if($message) { - throw new ApiNotOkException($message); - } - - $status = $this->status(); - throw new ApiNotOkException("NWS API is not OK: {$status}"); + throw new ApiNotOkException($message ?? "NWS API is not OK: {$this->status()}"); } return $this; @@ -304,8 +284,7 @@ public function zone(string $zone_id): ForecastZone { $zone_id = strtoupper($zone_id); $url = "{$this->baseUrl()}/zones?id={$zone_id}"; - $data = $this->get($url)->features[0]; - return new ForecastZone($data, $this); + return new ForecastZone($this->get($url)->features[0], $this); } /* diff --git a/src/Features/Alert.php b/src/Features/Alert.php index 1daf2c7..258213f 100644 --- a/src/Features/Alert.php +++ b/src/Features/Alert.php @@ -3,13 +3,7 @@ namespace BenjaminHansen\NWS\Features; use BenjaminHansen\NWS\Api; -use BenjaminHansen\NWS\Enums\AlertResponse; -use BenjaminHansen\NWS\Enums\AlertUrgency; -use BenjaminHansen\NWS\Enums\AlertCertainty; -use BenjaminHansen\NWS\Enums\AlertSeverity; -use BenjaminHansen\NWS\Enums\AlertCategory; -use BenjaminHansen\NWS\Enums\AlertStatus; -use BenjaminHansen\NWS\Enums\AlertMessageType; +use BenjaminHansen\NWS\Enums\{AlertResponse, AlertUrgency, AlertCertainty, AlertSeverity, AlertCategory, AlertStatus, AlertMessageType}; use BenjaminHansen\NWS\Support\Carbon; use Illuminate\Support\Collection; diff --git a/src/Features/Forecast.php b/src/Features/Forecast.php index 8afceb6..cdeea3a 100644 --- a/src/Features/Forecast.php +++ b/src/Features/Forecast.php @@ -3,8 +3,7 @@ namespace BenjaminHansen\NWS\Features; use BenjaminHansen\NWS\Api; -use BenjaminHansen\NWS\Support\Helpers; -use BenjaminHansen\NWS\Support\Carbon; +use BenjaminHansen\NWS\Support\{Helpers, Carbon}; class Forecast extends BaseFeature { @@ -25,8 +24,8 @@ public function createdAt(): Carbon public function elevation(string $unit = "FT", int $decimal_places = 0, bool $show_unit = false): int|float|string { - $value = match($unit) { - "ft", "FT" => round(Helpers::meters_to_feet($this->properties_elevation()->value), $decimal_places), + $value = match(strtoupper($unit)) { + "FT" => round(Helpers::meters_to_feet($this->properties_elevation()->value), $decimal_places), default => round($this->properties_elevation()->value, $decimal_places) }; diff --git a/src/Features/ForecastGridData.php b/src/Features/ForecastGridData.php index 3f1a178..0f995c3 100644 --- a/src/Features/ForecastGridData.php +++ b/src/Features/ForecastGridData.php @@ -3,8 +3,7 @@ namespace BenjaminHansen\NWS\Features; use BenjaminHansen\NWS\Api; -use BenjaminHansen\NWS\Support\Helpers; -use BenjaminHansen\NWS\Support\Carbon; +use BenjaminHansen\NWS\Support\{Helpers, Carbon}; use Illuminate\Support\Collection; class ForecastGridData extends BaseFeature diff --git a/src/Features/ForecastHourly.php b/src/Features/ForecastHourly.php index 8aa5363..8086851 100644 --- a/src/Features/ForecastHourly.php +++ b/src/Features/ForecastHourly.php @@ -3,8 +3,7 @@ namespace BenjaminHansen\NWS\Features; use BenjaminHansen\NWS\Api; -use BenjaminHansen\NWS\Support\Coordinate; -use BenjaminHansen\NWS\Support\Carbon; +use BenjaminHansen\NWS\Support\{Coordinate, Carbon}; use Illuminate\Support\Collection; class ForecastHourly extends BaseFeature diff --git a/src/Features/ForecastPeriod.php b/src/Features/ForecastPeriod.php index 9ec4f5b..769facf 100644 --- a/src/Features/ForecastPeriod.php +++ b/src/Features/ForecastPeriod.php @@ -3,8 +3,7 @@ namespace BenjaminHansen\NWS\Features; use BenjaminHansen\NWS\Api; -use BenjaminHansen\NWS\Support\Helpers; -use BenjaminHansen\NWS\Support\Carbon; +use BenjaminHansen\NWS\Support\{Helpers, Carbon}; class ForecastPeriod extends BaseFeature { @@ -48,16 +47,11 @@ public function windSpeed() return $this->data->windSpeed; } - public function windDirectionDegrees() + public function windDirection() { return $this->data->windDirection; } - public function windDirectionCardinal(): string - { - return Helpers::degrees_to_cardinals($this->data->windDirection); - } - public function shortForecast(): string { return $this->data->shortForecast; diff --git a/src/Features/LatestObservations.php b/src/Features/LatestObservations.php index f8214e7..540e0a3 100644 --- a/src/Features/LatestObservations.php +++ b/src/Features/LatestObservations.php @@ -3,8 +3,7 @@ namespace BenjaminHansen\NWS\Features; use BenjaminHansen\NWS\Api; -use BenjaminHansen\NWS\Support\Helpers; -use BenjaminHansen\NWS\Support\Carbon; +use BenjaminHansen\NWS\Support\{Helpers, Carbon}; class LatestObservations extends BaseFeature { diff --git a/src/Features/Point.php b/src/Features/Point.php index b8dfbb0..50b939a 100644 --- a/src/Features/Point.php +++ b/src/Features/Point.php @@ -80,9 +80,8 @@ public function radarStationId(): string public function radarStation(): RadarStation { - $id = $this->radarStationId(); $base_url = $this->api->baseUrl(); - $url = "{$base_url}/radar/stations/{$id}"; + $url = "{$base_url}/radar/stations/{$this->radarStationId()}"; return new RadarStation($this->api->get($url), $this->api); } @@ -120,9 +119,7 @@ public function latestObservations(int $observation_station_index = 0): LatestOb public function activeAlerts(): Alerts { $base_url = $this->api->baseUrl(); - $lat = $this->latitude(); - $lon = $this->longitude(); - $request_url = "{$base_url}/alerts/active?point={$lat},{$lon}"; + $request_url = "{$base_url}/alerts/active?point={$this->latitude()},{$this->longitude()}"; return new Alerts($this->api->get($request_url), $this->api); } diff --git a/src/Traits/CanGetDescription.php b/src/Traits/CanGetDescription.php index 84977ba..6a420f6 100644 --- a/src/Traits/CanGetDescription.php +++ b/src/Traits/CanGetDescription.php @@ -2,13 +2,7 @@ namespace BenjaminHansen\NWS\Traits; -use BenjaminHansen\NWS\Enums\AlertResponse; -use BenjaminHansen\NWS\Enums\AlertUrgency; -use BenjaminHansen\NWS\Enums\AlertCertainty; -use BenjaminHansen\NWS\Enums\AlertSeverity; -use BenjaminHansen\NWS\Enums\AlertCategory; -use BenjaminHansen\NWS\Enums\AlertStatus; -use BenjaminHansen\NWS\Enums\AlertMessageType; +use BenjaminHansen\NWS\Enums\{AlertResponse, AlertUrgency, AlertCertainty, AlertSeverity, AlertCategory, AlertStatus, AlertMessageType}; trait CanGetDescription {