Skip to content

Commit

Permalink
Merge pull request #44 from programmatordev/1.x
Browse files Browse the repository at this point in the history
1.x
  • Loading branch information
andrepimpao authored Aug 29, 2023
2 parents 0419fa7 + 225953b commit 0f1e8d8
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 107 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $openWeatherMap = new OpenWeatherMap(
);

// Get current weather by coordinate (latitude, longitude)
$currentWeather = $openWeatherMap->getWeather()->getCurrent(50, 50);
$currentWeather = $openWeatherMap->weather->getCurrent(50, 50);
// Show current temperature
echo $currentWeather->getTemperature();
```
Expand Down
2 changes: 1 addition & 1 deletion docs/01-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $openWeatherMap = new OpenWeatherMap(
);

// Get current weather by coordinate (latitude, longitude)
$currentWeather = $openWeatherMap->getWeather()->getCurrent(50, 50);
$currentWeather = $openWeatherMap->weather->getCurrent(50, 50);
// Show current temperature
echo $currentWeather->getTemperature();
```
14 changes: 7 additions & 7 deletions docs/02-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ It is possible to change the cache duration per request:

```php
// Response will be cached for 1 hour
$currentWeather = $openWeatherMap->getWeather()
$currentWeather = $openWeatherMap->weather
->withCacheTtl(3600)
->getCurrent(50, 50);
```
Expand Down Expand Up @@ -244,20 +244,20 @@ $openWeatherMap = new OpenWeatherMap(

// Using applicationKey as an example,
// but getters and setters are available for all options
$openWeatherMap->getConfig()->getApplicationKey();
$openWeatherMap->getConfig()->setApplicationKey('newappkey');
$openWeatherMap->config->getApplicationKey();
$openWeatherMap->config->setApplicationKey('newappkey');
```

Just take into account that any change will affect any subsequent request globally:

```php
// Using default 'metric' unit system
$openWeatherMap->getWeather()->getCurrent(50, 50);
$openWeatherMap->weather->getCurrent(50, 50);

// Set new unit system
$openWeatherMap->getConfig()->setUnitSystem(UnitSystem::IMPERIAL);
$openWeatherMap->config->setUnitSystem(UnitSystem::IMPERIAL);

// Using 'imperial' unit system
$openWeatherMap->getWeather()->getCurrent(50, 50);
$openWeatherMap->getWeather()->getForecast(50, 50);
$openWeatherMap->weather->getCurrent(50, 50);
$openWeatherMap->weather->getForecast(50, 50);
```
28 changes: 14 additions & 14 deletions docs/03-supported-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Get current and forecast (minutely, hourly and daily) weather data.
Returns a [`OneCall`](05-objects.md#onecall) object:

```php
$weather = $openWeatherMap->getOneCall()->getWeather(50, 50);
$weather = $openWeatherMap->oneCall->getWeather(50, 50);

echo $weather->getCurrent()->getTemperature();
```
Expand All @@ -52,7 +52,7 @@ Get weather data from a single moment in the past.
Returns a [`WeatherLocation`](05-objects.md#weatherlocation) object:

```php
$weather = $openWeatherMap->getOneCall()->getHistoryMoment(50, 50, new \DateTime('2023-01-01 12:00:00'));
$weather = $openWeatherMap->oneCall->getHistoryMoment(50, 50, new \DateTime('2023-01-01 12:00:00'));

echo $weather->getTemperature();
```
Expand All @@ -68,7 +68,7 @@ Get aggregated weather data from a single day in the past.
Returns a [`WeatherAggregate`](05-objects.md#weatheraggregate) object:

```php
$weather = $openWeatherMap->getOneCall()->getHistoryAggregate(50, 50, new \DateTime('1985-07-19'));
$weather = $openWeatherMap->oneCall->getHistoryAggregate(50, 50, new \DateTime('1985-07-19'));

echo $weather->getTemperature();
```
Expand All @@ -86,7 +86,7 @@ Get current weather data.
Returns a [`WeatherLocation`](05-objects.md#weatherlocation-1) object:

```php
$weather = $openWeatherMap->getWeather()->getCurrent(50, 50);
$weather = $openWeatherMap->weather->getCurrent(50, 50);

echo $weather->getTemperature();
```
Expand All @@ -104,7 +104,7 @@ Returns a [`WeatherLocationList`](05-objects.md#weatherlocationlist) object:
```php
// Since it returns 3-hour steps,
// passing 8 as the numResults means it will return results for the next 24 hours
$weatherForecast = $openWeatherMap->getWeather()->getForecast(50, 50, 8);
$weatherForecast = $openWeatherMap->weather->getForecast(50, 50, 8);

foreach ($weatherForecast->getList() as $weather) {
echo $weather->getDateTime()->format('Y-m-d H:i:s');
Expand All @@ -125,7 +125,7 @@ Get current air pollution data.
Returns a [`AirPollutionLocation`](05-objects.md#airpollutionlocation) object:

```php
$airPollution = $openWeatherMap->getAirPollution()->getCurrent(50, 50);
$airPollution = $openWeatherMap->airPollution->getCurrent(50, 50);

echo $airPollution->getAirQuality()->getQualitativeName();
echo $airPollution->getCarbonMonoxide();
Expand All @@ -142,7 +142,7 @@ Get air pollution forecast data per 1-hour for the next 24 hours.
Returns a [`AirPollutionLocationList`](05-objects.md#airpollutionlocationlist) object:

```php
$airPollutionForecast = $openWeatherMap->getAirPollution()->getForecast(50, 50);
$airPollutionForecast = $openWeatherMap->airPollution->getForecast(50, 50);

foreach ($airPollutionForecast->getList() as $airPollution) {
echo $airPollution->getDateTime()->format('Y-m-d H:i:s');
Expand All @@ -164,7 +164,7 @@ Returns a [`AirPollutionLocationList`](05-objects.md#airpollutionlocationlist) o
```php
$startDate = new \DateTime('-7 days'); // 7 days ago
$endDate = new \DateTime('-6 days'); // 6 days ago
$airPollutionHistory = $openWeatherMap->getAirPollution()->getHistory(50, 50, $startDate, $endDate);
$airPollutionHistory = $openWeatherMap->airPollution->getHistory(50, 50, $startDate, $endDate);

foreach ($airPollutionHistory->getList() as $airPollution) {
echo $airPollution->getDateTime()->format('Y-m-d H:i:s');
Expand All @@ -189,7 +189,7 @@ Get locations data by location name.
Returns an array of [`Location`](05-objects.md#location) objects:

```php
$locations = $openWeatherMap->getGeocoding()->getByLocationName('lisbon');
$locations = $openWeatherMap->geocoding->getByLocationName('lisbon');

foreach ($locations as $location) {
echo $location->getName();
Expand All @@ -208,7 +208,7 @@ Get location data by zip/post code.
Returns a [`ZipCodeLocation`](05-objects.md#zipcodelocation) object:

```php
$location = $openWeatherMap->getGeocoding()->getByZipCode('1000-001', 'pt');
$location = $openWeatherMap->geocoding->getByZipCode('1000-001', 'pt');

echo $location->getName();
```
Expand All @@ -227,7 +227,7 @@ Get locations data by coordinate.
Returns an array of [`Location`](05-objects.md#location) objects:

```php
$locations = $openWeatherMap->getGeocoding()->getByCoordinate(50, 50);
$locations = $openWeatherMap->geocoding->getByCoordinate(50, 50);

foreach ($locations as $location) {
echo $location->getName();
Expand All @@ -251,7 +251,7 @@ Only available for [`OneCall`](#one-call) and [`Weather`](#weather) APIs.
use ProgrammatorDev\OpenWeatherMap\UnitSystem\UnitSystem;

// Uses 'imperial' unit system for this request alone
$openWeatherMap->getWeather()
$openWeatherMap->weather
->withUnitSystem(UnitSystem::IMPERIAL)
->getCurrent(50, 50);
```
Expand All @@ -270,7 +270,7 @@ Only available for [`OneCall`](#one-call) and [`Weather`](#weather) APIs.
use ProgrammatorDev\OpenWeatherMap\Language\Language

// Uses 'pt' language for this request alone
$openWeatherMap->getWeather()
$openWeatherMap->weather
->withLanguage(Language::PORTUGUESE)
->getCurrent(50, 50);
```
Expand All @@ -296,7 +296,7 @@ Available for all APIs if `cache` is enabled in the [configuration](02-configura
use ProgrammatorDev\OpenWeatherMap\Language\Language

// Cache will be saved for 1 hour for this request alone
$openWeatherMap->getWeather()
$openWeatherMap->weather
->withCacheTtl(3600)
->getCurrent(50, 50);
```
10 changes: 5 additions & 5 deletions docs/04-error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;

try {
$location = $openWeatherMap->getGeocoding()->getByZipCode('1000-001', 'pt');
$location = $openWeatherMap->geocoding->getByZipCode('1000-001', 'pt');

$weather = $openWeatherMap->getOneCall()->getWeather(
$weather = $openWeatherMap->oneCall->getWeather(
$location->getCoordinate()->getLatitude(),
$location->getCoordinate()->getLongitude()
);
Expand Down Expand Up @@ -57,9 +57,9 @@ To catch all API errors with a single exception, `ApiErrorException` is availabl
use ProgrammatorDev\OpenWeatherMap\Exception\ApiErrorException;

try {
$location = $openWeatherMap->getGeocoding()->getByZipCode('1000-001', 'pt');
$location = $openWeatherMap->geocoding->getByZipCode('1000-001', 'pt');

$weather = $openWeatherMap->getOneCall()->getWeather(
$weather = $openWeatherMap->oneCall->getWeather(
$location->getCoordinate()->getLatitude(),
$location->getCoordinate()->getLongitude()
);
Expand All @@ -80,7 +80,7 @@ use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;

try {
// An invalid latitude value is given
$weather = $openWeatherMap->getWeather()->getCurrent(999, 50);
$weather = $openWeatherMap->weather->getCurrent(999, 50);
}
catch (ValidationException $exception) {
// Should print:
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoint/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AbstractEndpoint

public function __construct(protected OpenWeatherMap $api)
{
$this->config = $this->api->getConfig();
$this->config = $this->api->config;

$this->httpClientBuilder = $this->config->getHttpClientBuilder();
$this->cache = $this->config->getCache();
Expand Down
31 changes: 9 additions & 22 deletions src/OpenWeatherMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,19 @@

class OpenWeatherMap
{
public function __construct(
private readonly Config $config
) {}
public OneCallEndpoint $oneCall;

public function getConfig(): Config
{
return $this->config;
}
public WeatherEndpoint $weather;

public function getOneCall(): OneCallEndpoint
{
return new OneCallEndpoint($this);
}
public AirPollutionEndpoint $airPollution;

public function getWeather(): WeatherEndpoint
{
return new WeatherEndpoint($this);
}

public function getAirPollution(): AirPollutionEndpoint
{
return new AirPollutionEndpoint($this);
}
public GeocodingEndpoint $geocoding;

public function getGeocoding(): GeocodingEndpoint
public function __construct(public readonly Config $config)
{
return new GeocodingEndpoint($this);
$this->oneCall = new OneCallEndpoint($this);
$this->weather = new WeatherEndpoint($this);
$this->airPollution = new AirPollutionEndpoint($this);
$this->geocoding = new GeocodingEndpoint($this);
}
}
4 changes: 2 additions & 2 deletions tests/AbstractEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testAbstractEndpointWithCache()
$cache->expects($this->once())->method('save');

$api = $this->givenApi();
$api->getConfig()->setCache($cache);
$api->config->setCache($cache);

$this->mockSendRequest($api);
}
Expand All @@ -35,7 +35,7 @@ public function testAbstractEndpointWithLogger()
$logger->expects($this->atLeastOnce())->method('info');

$api = $this->givenApi();
$api->getConfig()->setLogger($logger);
$api->config->setLogger($logger);

$this->mockSendRequest($api);
}
Expand Down
18 changes: 9 additions & 9 deletions tests/AirPollutionEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public function testAirPollutionGetCurrent()
)
);

$response = $this->givenApi()->getAirPollution()->getCurrent(50, 50);
$response = $this->givenApi()->airPollution->getCurrent(50, 50);
$this->assertCurrentResponse($response);
}

#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
public function testAirPollutionGetCurrentWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
{
$this->expectException($expectedException);
$this->givenApi()->getAirPollution()->getCurrent($latitude, $longitude);
$this->givenApi()->airPollution->getCurrent($latitude, $longitude);
}

// --- FORECAST ---
Expand All @@ -47,15 +47,15 @@ public function testAirPollutionGetForecast()
)
);

$response = $this->givenApi()->getAirPollution()->getForecast(50, 50);
$response = $this->givenApi()->airPollution->getForecast(50, 50);
$this->assertForecastResponse($response);
}

#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
public function testAirPollutionGetForecastWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
{
$this->expectException($expectedException);
$this->givenApi()->getAirPollution()->getForecast($latitude, $longitude);
$this->givenApi()->airPollution->getForecast($latitude, $longitude);
}

// --- HISTORY ---
Expand All @@ -71,7 +71,7 @@ public function testAirPollutionGetHistory()

$utcTimezone = new \DateTimeZone('UTC');

$response = $this->givenApi()->getAirPollution()->getHistory(
$response = $this->givenApi()->airPollution->getHistory(
50,
50,
new \DateTimeImmutable('-5 days', $utcTimezone),
Expand All @@ -88,7 +88,7 @@ public function testAirPollutionGetHistoryWithInvalidCoordinate(float $latitude,
$startDate = new \DateTimeImmutable('-5 days');
$endDate = new \DateTimeImmutable('-4 days');

$this->givenApi()->getAirPollution()->getHistory($latitude, $longitude, $startDate, $endDate);
$this->givenApi()->airPollution->getHistory($latitude, $longitude, $startDate, $endDate);
}

#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidPastDateData')]
Expand All @@ -98,7 +98,7 @@ public function testAirPollutionGetHistoryWithInvalidPastStartDate(
)
{
$this->expectException($expectedException);
$this->givenApi()->getAirPollution()->getHistory(
$this->givenApi()->airPollution->getHistory(
50,
50,
$startDate,
Expand All @@ -113,7 +113,7 @@ public function testAirPollutionGetHistoryWithInvalidPastEndDate(
)
{
$this->expectException($expectedException);
$this->givenApi()->getAirPollution()->getHistory(
$this->givenApi()->airPollution->getHistory(
50,
50,
new \DateTimeImmutable('-5 days', new \DateTimeZone('UTC')),
Expand All @@ -129,7 +129,7 @@ public function testAirPollutionGetHistoryWithInvalidDateRange(
)
{
$this->expectException($expectedException);
$this->givenApi()->getAirPollution()->getHistory(50, 50, $startDate, $endDate);
$this->givenApi()->airPollution->getHistory(50, 50, $startDate, $endDate);
}

// --- ASSERT METHODS EXIST ---
Expand Down
2 changes: 1 addition & 1 deletion tests/ApiErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testApiError(int $statusCode, string $expectedException)
);

$this->expectException($expectedException);
$this->givenApi()->getWeather()->getCurrent(38.7077507, -9.1365919);
$this->givenApi()->weather->getCurrent(38.7077507, -9.1365919);
}

public static function provideApiErrorData(): \Generator
Expand Down
Loading

0 comments on commit 0f1e8d8

Please sign in to comment.