Skip to content

Commit

Permalink
arrays are now returned as Illiuminate Collections; new exception add…
Browse files Browse the repository at this point in the history
…ed for Invalid API request
  • Loading branch information
benjaminhansen committed May 3, 2024
1 parent 0a5557c commit a0add9c
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 30 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>Carbon</code> object
* Any timezone fields are automatically returned as a PHP <code>DateTimeZone</code> object
Expand Down
3 changes: 3 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use NWS\Features\Point;
use NWS\Features\ForecastOffice;
use NWS\Features\ObservationStation;
use NWS\Exceptions\InvalidRequestException;
use DateTimeZone;

class Api
Expand Down Expand Up @@ -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!");
}
}
10 changes: 10 additions & 0 deletions src/Exceptions/InvalidRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace NWS\Exceptions;

use Exception;

class InvalidRequestException extends Exception
{
//
}
7 changes: 4 additions & 3 deletions src/Features/Alerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use NWS\Traits\IsCallable;
use NWS\Support\Carbon;
use Illuminate\Support\Collection;

class Alerts
{
Expand All @@ -30,22 +31,22 @@ public function updatedAt(): Carbon

public function hasAlerts(): bool
{
return (boolean)count($this->data->features);
return (boolean) $this->count();
}

public function count(): int
{
return count($this->data->features);
}

public function get(): array
public function get(): Collection
{
$return = [];

foreach($this->data->features as $alert) {
$return[] = new Alert($alert, $this->api);
}

return $return;
return collect($return);
}
}
9 changes: 5 additions & 4 deletions src/Features/FireZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTimeZone;
use NWS\Traits\IsCallable;
use Illuminate\Support\Collection;

class FireZone
{
Expand All @@ -23,31 +24,31 @@ public function timezone(): DateTimeZone
return new DateTimeZone($this->data->properties->timeZone);
}

public function forecastOffices(): array
public function forecastOffices(): Collection
{
$return = [];

foreach($this->data->properties->forecastOffices as $office) {
$return[] = new ForecastOffice($this->api->get($office), $this->api);
}

return $return;
return collect($return);
}

public function forecastOffice(int $i = 0): ForecastOffice
{
return $this->forecastOffices()[$i];
}

public function observationStations(): array
public function observationStations(): Collection
{
$return = [];

foreach($this->data->properties->observationStations as $station) {
$return[] = new ObservationStation($this->api->get($station), $this->api);
}

return $return;
return collect($return);
}

public function observationStation(int $i = 0): ObservationStation
Expand Down
5 changes: 3 additions & 2 deletions src/Features/ForecastGridData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use NWS\Traits\IsCallable;
use NWS\Support\Helpers;
use NWS\Support\Carbon;
use Illuminate\Support\Collection;

class ForecastGridData
{
Expand Down Expand Up @@ -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 = [];

Expand All @@ -66,6 +67,6 @@ public function temeratures(string $unit = 'f', int $decimal_points = 0): array
];
}

return $return;
return collect($return);
}
}
5 changes: 3 additions & 2 deletions src/Features/ForecastHourly.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use NWS\Support\Coordinate;
use NWS\Traits\IsCallable;
use NWS\Support\Carbon;
use Illuminate\Support\Collection;

class ForecastHourly
{
Expand Down Expand Up @@ -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 = [];

Expand All @@ -56,6 +57,6 @@ public function coordinates(): array
}
}

return $return;
return collect($return);
}
}
17 changes: 9 additions & 8 deletions src/Features/ForecastOffice.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NWS\Features;

use NWS\Traits\IsCallable;
use Illuminate\Support\Collection;

class ForecastOffice
{
Expand Down Expand Up @@ -47,47 +48,47 @@ public function address(): string
return $this->data->address;
}

public function counties(): array
public function counties(): Collection
{
$return = [];

foreach($this->data->responsibleCounties as $county) {
$return[] = new County($this->api->get($county), $this->api);
}

return $return;
return collect($return);
}

public function forecastZones(): array
public function forecastZones(): Collection
{
$return = [];

foreach($this->data->responsibleForecastZones as $zone) {
$return[] = new ForecastZone($this->api->get($zone), $this->api);
}

return $return;
return collect($return);
}

public function observationStations(): array
public function observationStations(): Collection
{
$return = [];

foreach($this->data->approvedObservationStations as $station) {
$return[] = new ObservationStation($this->api->get($station), $this->api);
}

return $return;
return collect($return);
}

public function fireZones(): array
public function fireZones(): Collection
{
$return = [];

foreach($this->data->responsibleFireZones as $zone) {
$return[] = new FireZone($this->api->get($zone), $this->api);
}

return $return;
return collect($return);
}
}
5 changes: 3 additions & 2 deletions src/Features/ForecastPeriods.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NWS\Features;

use NWS\Traits\IsCallable;
use Illuminate\Support\Collection;

class ForecastPeriods
{
Expand All @@ -17,15 +18,15 @@ public function __construct($data, $api)
$this->api = $api;
}

public function get(): array
public function get(): Collection
{
$return = [];

foreach($this->data as $period) {
$return[] = new ForecastPeriod($period, $this->api);
}

return $return;
return collect($return);
}

public function period(int $i): ForecastPeriod
Expand Down
9 changes: 5 additions & 4 deletions src/Features/ForecastZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTimeZone;
use NWS\Traits\IsCallable;
use Illuminate\Support\Collection;

class ForecastZone
{
Expand All @@ -23,15 +24,15 @@ public function timezone(int $i = 0): DateTimeZone
return $this->timezones()[$i];
}

public function timezones(): array
public function timezones(): Collection
{
$return = [];

foreach($this->data->properties->timeZone as $timezone) {
$return[] = new DateTimeZone($timezone);
}

return $return;
return collect($return);
}

public function id(): string
Expand Down Expand Up @@ -65,15 +66,15 @@ public function observationStation(int $i = 0): ObservationStation
return $this->observationStations()[$i];
}

public function forecastOffices(): array
public function forecastOffices(): Collection
{
$return = [];

foreach($this->data->properties->forecastOffices as $office) {
$return[] = new ForecastOffice($this->api->get($office), $this->api);
}

return $return;
return collect($return);
}

public function forecastOffice(int $i = 0): ForecastOffice
Expand Down
5 changes: 3 additions & 2 deletions src/Features/ObservationStations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NWS\Features;

use NWS\Traits\IsCallable;
use Illuminate\Support\Collection;

class ObservationStations
{
Expand All @@ -17,15 +18,15 @@ public function __construct($data, $api)
$this->api = $api;
}

public function get(): array
public function get(): Collection
{
$return = [];

foreach($this->data->features as $station) {
$return[] = new ObservationStation($station, $this->api);
}

return $return;
return collect($return);
}

public function station(int $i = 0): ObservationStation
Expand Down

0 comments on commit a0add9c

Please sign in to comment.