Skip to content

Commit 3e032b0

Browse files
authored
Merge pull request #49 from programmatordev/1.x
1.x
2 parents 0f1e8d8 + b400396 commit 3e032b0

28 files changed

+188
-212
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/Config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
class Config
1515
{
16+
private string $baseUrl = 'https://api.openweathermap.org';
17+
1618
private array $options;
1719

1820
public function __construct(array $options = [])
@@ -50,6 +52,11 @@ private function resolveOptions(array $options): array
5052
return $resolver->resolve($options);
5153
}
5254

55+
public function getBaseUrl(): string
56+
{
57+
return $this->baseUrl;
58+
}
59+
5360
public function getApplicationKey(): string
5461
{
5562
return $this->options['applicationKey'];

src/Endpoint/AbstractEndpoint.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Http\Client\Common\Plugin\LoggerPlugin;
77
use Http\Client\Exception;
88
use ProgrammatorDev\OpenWeatherMap\Config;
9-
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\WithCacheTtlTrait;
9+
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\CacheTtlTrait;
1010
use ProgrammatorDev\OpenWeatherMap\Entity\Error;
1111
use ProgrammatorDev\OpenWeatherMap\Exception\BadRequestException;
1212
use ProgrammatorDev\OpenWeatherMap\Exception\NotFoundException;
@@ -20,12 +20,11 @@
2020
use Psr\Cache\CacheItemPoolInterface;
2121
use Psr\Http\Message\ResponseInterface;
2222
use Psr\Http\Message\StreamInterface;
23-
use Psr\Http\Message\UriInterface;
2423
use Psr\Log\LoggerInterface;
2524

2625
class AbstractEndpoint
2726
{
28-
use WithCacheTtlTrait;
27+
use CacheTtlTrait;
2928

3029
private Config $config;
3130

@@ -43,7 +42,7 @@ class AbstractEndpoint
4342

4443
public function __construct(protected OpenWeatherMap $api)
4544
{
46-
$this->config = $this->api->config;
45+
$this->config = $this->api->config();
4746

4847
$this->httpClientBuilder = $this->config->getHttpClientBuilder();
4948
$this->cache = $this->config->getCache();
@@ -62,7 +61,7 @@ public function __construct(protected OpenWeatherMap $api)
6261
*/
6362
protected function sendRequest(
6463
string $method,
65-
UriInterface|string $baseUrl,
64+
string $path,
6665
array $query = [],
6766
array $headers = [],
6867
StreamInterface|string $body = null
@@ -72,13 +71,13 @@ protected function sendRequest(
7271

7372
$response = $this->httpClientBuilder->getHttpClient()->send(
7473
$method,
75-
$this->buildUrl($baseUrl, $query),
74+
$this->buildUrl($path, $query),
7675
$headers,
7776
$body
7877
);
7978

80-
if (($statusCode = $response->getStatusCode()) >= 400) {
81-
$this->handleResponseErrors($response, $statusCode);
79+
if ($response->getStatusCode() >= 400) {
80+
$this->handleResponseErrors($response);
8281
}
8382

8483
return ResponseMediator::toArray($response);
@@ -113,13 +112,13 @@ private function configurePlugins(): void
113112
* @throws UnauthorizedException
114113
* @throws BadRequestException
115114
*/
116-
private function handleResponseErrors(ResponseInterface $response, int $statusCode): void
115+
private function handleResponseErrors(ResponseInterface $response): void
117116
{
118117
$error = new Error(
119118
ResponseMediator::toArray($response)
120119
);
121120

122-
match ($statusCode) {
121+
match ($response->getStatusCode()) {
123122
400 => throw new BadRequestException($error),
124123
401 => throw new UnauthorizedException($error),
125124
404 => throw new NotFoundException($error),
@@ -128,17 +127,13 @@ private function handleResponseErrors(ResponseInterface $response, int $statusCo
128127
};
129128
}
130129

131-
private function buildUrl(UriInterface|string $baseUrl, array $query): string
130+
private function buildUrl(string $path, array $query): string
132131
{
133-
if ($baseUrl instanceof UriInterface) {
134-
$baseUrl = (string) $baseUrl;
135-
}
136-
137132
// Add application key to all requests
138133
$query = $query + [
139134
'appid' => $this->config->getApplicationKey()
140135
];
141136

142-
return \sprintf('%s?%s', $baseUrl, http_build_query($query));
137+
return \sprintf('%s%s?%s', $this->config->getBaseUrl(), $path, http_build_query($query));
143138
}
144139
}

src/Endpoint/AirPollutionEndpoint.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515

1616
class AirPollutionEndpoint extends AbstractEndpoint
1717
{
18-
private string $urlAirPollution = 'https://api.openweathermap.org/data/2.5/air_pollution';
19-
20-
private string $urlAirPollutionForecast = 'https://api.openweathermap.org/data/2.5/air_pollution/forecast';
21-
22-
private string $urlAirPollutionHistory = 'https://api.openweathermap.org/data/2.5/air_pollution/history';
23-
2418
/**
2519
* @throws Exception
2620
* @throws BadRequestException
@@ -37,7 +31,7 @@ public function getCurrent(float $latitude, float $longitude): AirPollutionLocat
3731

3832
$data = $this->sendRequest(
3933
method: 'GET',
40-
baseUrl: $this->urlAirPollution,
34+
path: '/data/2.5/air_pollution',
4135
query: [
4236
'lat' => $latitude,
4337
'lon' => $longitude
@@ -63,7 +57,7 @@ public function getForecast(float $latitude, float $longitude): AirPollutionLoca
6357

6458
$data = $this->sendRequest(
6559
method: 'GET',
66-
baseUrl: $this->urlAirPollutionForecast,
60+
path: '/data/2.5/air_pollution/forecast',
6761
query: [
6862
'lat' => $latitude,
6963
'lon' => $longitude
@@ -98,7 +92,7 @@ public function getHistory(
9892

9993
$data = $this->sendRequest(
10094
method: 'GET',
101-
baseUrl: $this->urlAirPollutionHistory,
95+
path: '/data/2.5/air_pollution/history',
10296
query: [
10397
'lat' => $latitude,
10498
'lon' => $longitude,

0 commit comments

Comments
 (0)