Skip to content

Commit d99abc0

Browse files
authored
Merge pull request #48 from programmatordev/OPA-48-improve-endpoints-methods-call
Improve endpoints methods call
2 parents 8851367 + 872564b commit d99abc0

17 files changed

+91
-80
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ $openWeatherMap = new OpenWeatherMap(
4747
);
4848

4949
// Get current weather by coordinate (latitude, longitude)
50-
$currentWeather = $openWeatherMap->weather->getCurrent(50, 50);
50+
$currentWeather = $openWeatherMap->weather()->getCurrent(50, 50);
5151
// Show current temperature
5252
echo $currentWeather->getTemperature();
5353
```

docs/01-usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ $openWeatherMap = new OpenWeatherMap(
4444
);
4545

4646
// Get current weather by coordinate (latitude, longitude)
47-
$currentWeather = $openWeatherMap->weather->getCurrent(50, 50);
47+
$currentWeather = $openWeatherMap->weather()->getCurrent(50, 50);
4848
// Show current temperature
4949
echo $currentWeather->getTemperature();
5050
```

docs/02-configuration.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ It is possible to change the cache duration per request:
187187

188188
```php
189189
// Response will be cached for 1 hour
190-
$currentWeather = $openWeatherMap->weather
190+
$currentWeather = $openWeatherMap->weather()
191191
->withCacheTtl(3600)
192192
->getCurrent(50, 50);
193193
```
@@ -244,20 +244,20 @@ $openWeatherMap = new OpenWeatherMap(
244244

245245
// Using applicationKey as an example,
246246
// but getters and setters are available for all options
247-
$openWeatherMap->config->getApplicationKey();
248-
$openWeatherMap->config->setApplicationKey('newappkey');
247+
$openWeatherMap->config()->getApplicationKey();
248+
$openWeatherMap->config()->setApplicationKey('newappkey');
249249
```
250250

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

253253
```php
254254
// Using default 'metric' unit system
255-
$openWeatherMap->weather->getCurrent(50, 50);
255+
$openWeatherMap->weather()->getCurrent(50, 50);
256256

257257
// Set new unit system
258-
$openWeatherMap->config->setUnitSystem(UnitSystem::IMPERIAL);
258+
$openWeatherMap->config()->setUnitSystem(UnitSystem::IMPERIAL);
259259

260260
// Using 'imperial' unit system
261-
$openWeatherMap->weather->getCurrent(50, 50);
262-
$openWeatherMap->weather->getForecast(50, 50);
261+
$openWeatherMap->weather()->getCurrent(50, 50);
262+
$openWeatherMap->weather()->getForecast(50, 50);
263263
```

docs/03-supported-apis.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Get current and forecast (minutely, hourly and daily) weather data.
3636
Returns a [`OneCall`](05-objects.md#onecall) object:
3737

3838
```php
39-
$weather = $openWeatherMap->oneCall->getWeather(50, 50);
39+
$weather = $openWeatherMap->oneCall()->getWeather(50, 50);
4040

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

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

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

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

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

8888
```php
89-
$weather = $openWeatherMap->weather->getCurrent(50, 50);
89+
$weather = $openWeatherMap->weather()->getCurrent(50, 50);
9090

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

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

127127
```php
128-
$airPollution = $openWeatherMap->airPollution->getCurrent(50, 50);
128+
$airPollution = $openWeatherMap->airPollution()->getCurrent(50, 50);
129129

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

144144
```php
145-
$airPollutionForecast = $openWeatherMap->airPollution->getForecast(50, 50);
145+
$airPollutionForecast = $openWeatherMap->airPollution()->getForecast(50, 50);
146146

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

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

191191
```php
192-
$locations = $openWeatherMap->geocoding->getByLocationName('lisbon');
192+
$locations = $openWeatherMap->geocoding()->getByLocationName('lisbon');
193193

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

210210
```php
211-
$location = $openWeatherMap->geocoding->getByZipCode('1000-001', 'pt');
211+
$location = $openWeatherMap->geocoding()->getByZipCode('1000-001', 'pt');
212212

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

229229
```php
230-
$locations = $openWeatherMap->geocoding->getByCoordinate(50, 50);
230+
$locations = $openWeatherMap->geocoding()->getByCoordinate(50, 50);
231231

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

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

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

298298
// Cache will be saved for 1 hour for this request alone
299-
$openWeatherMap->weather
299+
$openWeatherMap->weather()
300300
->withCacheTtl(3600)
301301
->getCurrent(50, 50);
302302
```

docs/04-error-handling.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
1515
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
1616

1717
try {
18-
$location = $openWeatherMap->geocoding->getByZipCode('1000-001', 'pt');
18+
$location = $openWeatherMap->geocoding()->getByZipCode('1000-001', 'pt');
1919

20-
$weather = $openWeatherMap->oneCall->getWeather(
20+
$weather = $openWeatherMap->oneCall()->getWeather(
2121
$location->getCoordinate()->getLatitude(),
2222
$location->getCoordinate()->getLongitude()
2323
);
@@ -57,9 +57,9 @@ To catch all API errors with a single exception, `ApiErrorException` is availabl
5757
use ProgrammatorDev\OpenWeatherMap\Exception\ApiErrorException;
5858

5959
try {
60-
$location = $openWeatherMap->geocoding->getByZipCode('1000-001', 'pt');
60+
$location = $openWeatherMap->geocoding()->getByZipCode('1000-001', 'pt');
6161

62-
$weather = $openWeatherMap->oneCall->getWeather(
62+
$weather = $openWeatherMap->oneCall()->getWeather(
6363
$location->getCoordinate()->getLatitude(),
6464
$location->getCoordinate()->getLongitude()
6565
);
@@ -80,7 +80,7 @@ use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
8080

8181
try {
8282
// An invalid latitude value is given
83-
$weather = $openWeatherMap->weather->getCurrent(999, 50);
83+
$weather = $openWeatherMap->weather()->getCurrent(999, 50);
8484
}
8585
catch (ValidationException $exception) {
8686
// Should print:

src/Endpoint/AbstractEndpoint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class AbstractEndpoint
4242

4343
public function __construct(protected OpenWeatherMap $api)
4444
{
45-
$this->config = $this->api->config;
45+
$this->config = $this->api->config();
4646

4747
$this->httpClientBuilder = $this->config->getHttpClientBuilder();
4848
$this->cache = $this->config->getCache();

src/OpenWeatherMap.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,30 @@
99

1010
class OpenWeatherMap
1111
{
12-
public OneCallEndpoint $oneCall;
12+
public function __construct(private readonly Config $config) {}
1313

14-
public WeatherEndpoint $weather;
14+
public function config(): Config
15+
{
16+
return $this->config;
17+
}
1518

16-
public AirPollutionEndpoint $airPollution;
19+
public function oneCall(): OneCallEndpoint
20+
{
21+
return new OneCallEndpoint($this);
22+
}
1723

18-
public GeocodingEndpoint $geocoding;
24+
public function weather(): WeatherEndpoint
25+
{
26+
return new WeatherEndpoint($this);
27+
}
28+
29+
public function airPollution(): AirPollutionEndpoint
30+
{
31+
return new AirPollutionEndpoint($this);
32+
}
1933

20-
public function __construct(public readonly Config $config)
34+
public function geocoding(): GeocodingEndpoint
2135
{
22-
$this->oneCall = new OneCallEndpoint($this);
23-
$this->weather = new WeatherEndpoint($this);
24-
$this->airPollution = new AirPollutionEndpoint($this);
25-
$this->geocoding = new GeocodingEndpoint($this);
36+
return new GeocodingEndpoint($this);
2637
}
2738
}

tests/AbstractEndpointTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testAbstractEndpointWithCache()
2020
$cache->expects($this->once())->method('save');
2121

2222
$api = $this->givenApi();
23-
$api->config->setCache($cache);
23+
$api->config()->setCache($cache);
2424

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

3737
$api = $this->givenApi();
38-
$api->config->setLogger($logger);
38+
$api->config()->setLogger($logger);
3939

4040
$this->mockSendRequest($api);
4141
}

tests/AirPollutionEndpointTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ public function testAirPollutionGetCurrent()
2525
)
2626
);
2727

28-
$response = $this->givenApi()->airPollution->getCurrent(50, 50);
28+
$response = $this->givenApi()->airPollution()->getCurrent(50, 50);
2929
$this->assertCurrentResponse($response);
3030
}
3131

3232
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
3333
public function testAirPollutionGetCurrentWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
3434
{
3535
$this->expectException($expectedException);
36-
$this->givenApi()->airPollution->getCurrent($latitude, $longitude);
36+
$this->givenApi()->airPollution()->getCurrent($latitude, $longitude);
3737
}
3838

3939
// --- FORECAST ---
@@ -47,15 +47,15 @@ public function testAirPollutionGetForecast()
4747
)
4848
);
4949

50-
$response = $this->givenApi()->airPollution->getForecast(50, 50);
50+
$response = $this->givenApi()->airPollution()->getForecast(50, 50);
5151
$this->assertForecastResponse($response);
5252
}
5353

5454
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidCoordinateData')]
5555
public function testAirPollutionGetForecastWithInvalidCoordinate(float $latitude, float $longitude, string $expectedException)
5656
{
5757
$this->expectException($expectedException);
58-
$this->givenApi()->airPollution->getForecast($latitude, $longitude);
58+
$this->givenApi()->airPollution()->getForecast($latitude, $longitude);
5959
}
6060

6161
// --- HISTORY ---
@@ -71,7 +71,7 @@ public function testAirPollutionGetHistory()
7171

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

74-
$response = $this->givenApi()->airPollution->getHistory(
74+
$response = $this->givenApi()->airPollution()->getHistory(
7575
50,
7676
50,
7777
new \DateTimeImmutable('-5 days', $utcTimezone),
@@ -88,7 +88,7 @@ public function testAirPollutionGetHistoryWithInvalidCoordinate(float $latitude,
8888
$startDate = new \DateTimeImmutable('-5 days');
8989
$endDate = new \DateTimeImmutable('-4 days');
9090

91-
$this->givenApi()->airPollution->getHistory($latitude, $longitude, $startDate, $endDate);
91+
$this->givenApi()->airPollution()->getHistory($latitude, $longitude, $startDate, $endDate);
9292
}
9393

9494
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidPastDateData')]
@@ -98,7 +98,7 @@ public function testAirPollutionGetHistoryWithInvalidPastStartDate(
9898
)
9999
{
100100
$this->expectException($expectedException);
101-
$this->givenApi()->airPollution->getHistory(
101+
$this->givenApi()->airPollution()->getHistory(
102102
50,
103103
50,
104104
$startDate,
@@ -113,7 +113,7 @@ public function testAirPollutionGetHistoryWithInvalidPastEndDate(
113113
)
114114
{
115115
$this->expectException($expectedException);
116-
$this->givenApi()->airPollution->getHistory(
116+
$this->givenApi()->airPollution()->getHistory(
117117
50,
118118
50,
119119
new \DateTimeImmutable('-5 days', new \DateTimeZone('UTC')),
@@ -129,7 +129,7 @@ public function testAirPollutionGetHistoryWithInvalidDateRange(
129129
)
130130
{
131131
$this->expectException($expectedException);
132-
$this->givenApi()->airPollution->getHistory(50, 50, $startDate, $endDate);
132+
$this->givenApi()->airPollution()->getHistory(50, 50, $startDate, $endDate);
133133
}
134134

135135
// --- ASSERT METHODS EXIST ---

tests/ApiErrorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testApiError(int $statusCode, string $expectedException)
2424
);
2525

2626
$this->expectException($expectedException);
27-
$this->givenApi()->weather->getCurrent(38.7077507, -9.1365919);
27+
$this->givenApi()->weather()->getCurrent(38.7077507, -9.1365919);
2828
}
2929

3030
public static function provideApiErrorData(): \Generator

0 commit comments

Comments
 (0)