Skip to content

Commit 38b3137

Browse files
authored
Merge pull request #38 from programmatordev/1.x
1.x
2 parents 29bf718 + 90e58e8 commit 38b3137

23 files changed

+163
-401
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 programmatordev
3+
Copyright (c) 2023 Programmator
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ echo $currentWeather->getTemperature();
6262

6363
## Contributing
6464

65-
Any form of contribution to improve this library will be welcome and appreciated.
65+
Any form of contribution to improve this library (including requests) will be welcome and appreciated.
6666
Make sure to open a pull request or issue.
6767

6868
## License

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"php-http/client-common": "^2.7",
1818
"php-http/discovery": "^1.18",
1919
"php-http/logger-plugin": "^1.3",
20+
"programmatordev/yet-another-php-validator": "^0.1.1",
2021
"psr/cache": "^2.0 || ^3.0",
2122
"psr/http-client": "^1.0",
2223
"psr/http-factory": "^1.0",

docs/03-supported-apis.md

Lines changed: 101 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,70 @@
22

33
- [APIs](#apis)
44
- [One Call](#one-call)
5-
- [getWeather](#getweatherfloat-latitude-float-longitude)
6-
- [getHistoryMoment](#gethistorymomentfloat-latitude-float-longitude-datetimeinterface-datetime)
7-
- [getHistoryAggregate](#gethistoryaggregatefloat-latitude-float-longitude-datetimeinterface-date)
5+
- [getWeather](#getweather)
6+
- [getHistoryMoment](#gethistorymoment)
7+
- [getHistoryAggregate](#gethistoryaggregate)
88
- [Weather](#weather)
9-
- [getCurrent](#getcurrentfloat-latitude-float-longitude)
10-
- [getForecast](#getforecastfloat-latitude-float-longitude-int-numresults--40)
9+
- [getCurrent](#getcurrent)
10+
- [getForecast](#getforecast)
1111
- [Air Pollution](#air-pollution)
12-
- [getCurrent](#getcurrentfloat-latitude-float-longitude-1)
13-
- [getForecast](#getforecastfloat-latitude-float-longitude)
14-
- [getHistory](#gethistoryfloat-latitude-float-longitude-datetimeinterface-startdate-datetimeinterface-enddate)
12+
- [getCurrent](#getcurrent-1)
13+
- [getForecast](#getforecast-1)
14+
- [getHistory](#gethistory)
1515
- [Geocoding](#geocoding)
16-
- [getByLocationName](#getbylocationnamestring-locationname-int-numresults--5)
17-
- [getByZipCode](#getbyzipcodestring-zipcode-string-countrycode)
18-
- [getByCoordinate](#getbycoordinatefloat-latitude-float-longitude-int-numresults--5)
16+
- [getByLocationName](#getbylocationname)
17+
- [getByZipCode](#getbyzipcode)
18+
- [getByCoordinate](#getbycoordinate)
1919
- [Common Methods](#common-methods)
20-
- [withUnitSystem](#withunitsystemstring-unitsystem)
21-
- [withLanguage](#withlanguagestring-language)
22-
- [withCacheTtl](#withcachettlint-time)
20+
- [withUnitSystem](#withunitsystem)
21+
- [withLanguage](#withlanguage)
22+
- [withCacheTtl](#withcachettl)
2323

2424
## APIs
2525

2626
### One Call
2727

28-
#### `getWeather(float $latitude, float $longitude)`
28+
#### `getWeather`
29+
30+
```php
31+
getWeather(float $latitude, float $longitude): OneCall
32+
```
2933

3034
Get current and forecast (minutely, hourly and daily) weather data.
3135

32-
Returns a [`OneCall`](05-objects.md#onecall) object.
36+
Returns a [`OneCall`](05-objects.md#onecall) object:
3337

3438
```php
3539
$weather = $openWeatherMap->getOneCall()->getWeather(50, 50);
3640

3741
echo $weather->getCurrent()->getTemperature();
3842
```
3943

40-
#### `getHistoryMoment(float $latitude, float $longitude, \DateTimeInterface $dateTime)`
44+
#### `getHistoryMoment`
45+
46+
```php
47+
getHistoryMoment(float $latitude, float $longitude, \DateTimeInterface $dateTime): WeatherLocation
48+
```
4149

4250
Get weather data from a single moment in the past.
4351

44-
Returns a [`WeatherLocation`](05-objects.md#weatherlocation) object.
52+
Returns a [`WeatherLocation`](05-objects.md#weatherlocation) object:
4553

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

4957
echo $weather->getTemperature();
5058
```
5159

52-
#### `getHistoryAggregate(float $latitude, float $longitude, \DateTimeInterface $date)`
60+
#### `getHistoryAggregate`
61+
62+
```php
63+
getHistoryAggregate(float $latitude, float $longitude, \DateTimeInterface $date): WeatherAggregate
64+
```
5365

5466
Get aggregated weather data from a single day in the past.
5567

56-
Returns a [`WeatherAggregate`](05-objects.md#weatheraggregate) object.
68+
Returns a [`WeatherAggregate`](05-objects.md#weatheraggregate) object:
5769

5870
```php
5971
$weather = $openWeatherMap->getOneCall()->getHistoryAggregate(50, 50, new \DateTime('1985-07-19'));
@@ -63,23 +75,31 @@ echo $weather->getTemperature();
6375

6476
### Weather
6577

66-
#### `getCurrent(float $latitude, float $longitude)`
78+
#### `getCurrent`
79+
80+
```php
81+
getCurrent(float $latitude, float $longitude): WeatherLocation
82+
```
6783

6884
Get current weather data.
6985

70-
Returns a [`WeatherLocation`](05-objects.md#weatherlocation-1) object.
86+
Returns a [`WeatherLocation`](05-objects.md#weatherlocation-1) object:
7187

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

7591
echo $weather->getTemperature();
7692
```
7793

78-
#### `getForecast(float $latitude, float $longitude, int $numResults = 40)`
94+
#### `getForecast`
95+
96+
```php
97+
getForecast(float $latitude, float $longitude, int $numResults = 40): WeatherLocationList
98+
```
7999

80100
Get weather forecast data per 3-hour steps for the next 5 days.
81101

82-
Returns a [`WeatherLocationList`](05-objects.md#weatherlocationlist) object.
102+
Returns a [`WeatherLocationList`](05-objects.md#weatherlocationlist) object:
83103

84104
```php
85105
// Since it returns 3-hour steps,
@@ -94,11 +114,15 @@ foreach ($weatherForecast->getList() as $weather) {
94114

95115
### Air Pollution
96116

97-
#### `getCurrent(float $latitude, float $longitude)`
117+
#### `getCurrent`
118+
119+
```php
120+
getCurrent(float $latitude, float $longitude): AirPollutionLocation
121+
```
98122

99123
Get current air pollution data.
100124

101-
Returns a [`AirPollutionLocation`](05-objects.md#airpollutionlocation) object.
125+
Returns a [`AirPollutionLocation`](05-objects.md#airpollutionlocation) object:
102126

103127
```php
104128
$airPollution = $openWeatherMap->getAirPollution()->getCurrent(50, 50);
@@ -107,11 +131,15 @@ echo $airPollution->getAirQuality()->getQualitativeName();
107131
echo $airPollution->getCarbonMonoxide();
108132
```
109133

110-
#### `getForecast(float $latitude, float $longitude)`
134+
#### `getForecast`
135+
136+
```php
137+
getForecast(float $latitude, float $longitude): AirPollutionLocationList
138+
```
111139

112140
Get air pollution forecast data per 1-hour for the next 24 hours.
113141

114-
Returns a [`AirPollutionLocationList`](05-objects.md#airpollutionlocationlist) object.
142+
Returns a [`AirPollutionLocationList`](05-objects.md#airpollutionlocationlist) object:
115143

116144
```php
117145
$airPollutionForecast = $openWeatherMap->getAirPollution()->getForecast(50, 50);
@@ -123,11 +151,15 @@ foreach ($airPollutionForecast->getList() as $airPollution) {
123151
}
124152
```
125153

126-
#### `getHistory(float $latitude, float $longitude, \DateTimeInterface $startDate, \DateTimeInterface $endDate)`
154+
#### `getHistory`
155+
156+
```php
157+
getHistory(float $latitude, float $longitude, \DateTimeInterface $startDate, \DateTimeInterface $endDate): AirPollutionLocationList
158+
```
127159

128160
Get air pollution history data between two dates.
129161

130-
Returns a [`AirPollutionLocationList`](05-objects.md#airpollutionlocationlist) object.
162+
Returns a [`AirPollutionLocationList`](05-objects.md#airpollutionlocationlist) object:
131163

132164
```php
133165
$startDate = new \DateTime('-7 days'); // 7 days ago
@@ -143,11 +175,18 @@ foreach ($airPollutionHistory->getList() as $airPollution) {
143175

144176
### Geocoding
145177

146-
#### `getByLocationName(string $locationName, int $numResults = 5)`
178+
#### `getByLocationName`
179+
180+
```php
181+
/**
182+
* @return Location[]
183+
*/
184+
getByLocationName(string $locationName, int $numResults = 5): array
185+
```
147186

148187
Get locations data by location name.
149188

150-
Returns an array of [`Location`](05-objects.md#location) objects.
189+
Returns an array of [`Location`](05-objects.md#location) objects:
151190

152191
```php
153192
$locations = $openWeatherMap->getGeocoding()->getByLocationName('lisbon');
@@ -158,23 +197,34 @@ foreach ($locations as $location) {
158197
}
159198
```
160199

161-
#### `getByZipCode(string $zipCode, string $countryCode)`
200+
#### `getByZipCode`
201+
202+
```php
203+
getByZipCode(string $zipCode, string $countryCode): ZipCodeLocation
204+
```
162205

163206
Get location data by zip/post code.
164207

165-
Returns a [`ZipCodeLocation`](05-objects.md#zipcodelocation) object.
208+
Returns a [`ZipCodeLocation`](05-objects.md#zipcodelocation) object:
166209

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

170213
echo $location->getName();
171214
```
172215

173-
#### `getByCoordinate(float $latitude, float $longitude, int $numResults = 5)`
216+
#### `getByCoordinate`
217+
218+
```php
219+
/**
220+
* @return Location[]
221+
*/
222+
getByCoordinate(float $latitude, float $longitude, int $numResults = 5): array
223+
```
174224

175225
Get locations data by coordinate.
176226

177-
Returns an array of [`Location`](05-objects.md#location) objects.
227+
Returns an array of [`Location`](05-objects.md#location) objects:
178228

179229
```php
180230
$locations = $openWeatherMap->getGeocoding()->getByCoordinate(50, 50);
@@ -187,7 +237,11 @@ foreach ($locations as $location) {
187237

188238
## Common Methods
189239

190-
#### `withUnitSystem(string $unitSystem)`
240+
#### `withUnitSystem`
241+
242+
```php
243+
withUnitSystem(string $unitSystem): self
244+
```
191245

192246
Makes a request with a different unit system from the one globally defined in the [configuration](02-configuration.md#unitsystem).
193247

@@ -202,7 +256,11 @@ $openWeatherMap->getWeather()
202256
->getCurrent(50, 50);
203257
```
204258

205-
#### `withLanguage(string $language)`
259+
#### `withLanguage`
260+
261+
```php
262+
withLanguage(string $language): self
263+
```
206264

207265
Makes a request with a different language from the one globally defined in the [configuration](02-configuration.md#language).
208266

@@ -217,7 +275,11 @@ $openWeatherMap->getWeather()
217275
->getCurrent(50, 50);
218276
```
219277

220-
#### `withCacheTtl(?int $time)`
278+
#### `withCacheTtl`
279+
280+
```php
281+
withCacheTtl(?int $time): self
282+
```
221283

222284
Makes a request and saves into cache with the provided time duration value (in seconds).
223285
Check the [Cache TTL](02-configuration.md#cache-ttl) section for more information regarding default values.

docs/04-error-handling.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,18 @@ catch (ApiErrorException $exception) {
7373

7474
## Validation Errors
7575

76-
To catch invalid input data (like an out of range coordinate, blank location name, etc.),
77-
the `ValidationException` is available:
76+
To catch invalid input data (like an out of range coordinate, blank location name, etc.), the `ValidationException` is available:
7877

7978
```php
80-
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
79+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
8180

8281
try {
8382
// An invalid latitude value is given
8483
$weather = $openWeatherMap->getWeather()->getCurrent(999, 50);
8584
}
8685
catch (ValidationException $exception) {
8786
// Should print:
88-
// The "latitude" value "999" is invalid. Must be between "-90" and "90".
87+
// The "latitude" value should be between "-90" and "90", "999" given.
8988
echo $exception->getMessage();
9089
}
9190
```

src/Config.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap;
44

5-
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
65
use ProgrammatorDev\OpenWeatherMap\HttpClient\HttpClientBuilder;
76
use ProgrammatorDev\OpenWeatherMap\Language\Language;
87
use ProgrammatorDev\OpenWeatherMap\UnitSystem\UnitSystem;
9-
use ProgrammatorDev\OpenWeatherMap\Validator\BlankValidatorTrait;
10-
use ProgrammatorDev\OpenWeatherMap\Validator\ChoiceValidatorTrait;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
9+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
1110
use Psr\Cache\CacheItemPoolInterface;
1211
use Psr\Log\LoggerInterface;
1312
use Symfony\Component\OptionsResolver\OptionsResolver;
1413

1514
class Config
1615
{
17-
use BlankValidatorTrait;
18-
use ChoiceValidatorTrait;
19-
2016
private array $options;
2117

2218
public function __construct(array $options = [])
@@ -62,7 +58,7 @@ public function getApplicationKey(): string
6258
*/
6359
public function setApplicationKey(string $applicationKey): self
6460
{
65-
$this->validateBlank('applicationKey', $applicationKey);
61+
Validator::notBlank()->assert($applicationKey, 'applicationKey');
6662

6763
$this->options['applicationKey'] = $applicationKey;
6864

@@ -79,7 +75,7 @@ public function getUnitSystem(): string
7975
*/
8076
public function setUnitSystem(string $unitSystem): self
8177
{
82-
$this->validateChoice('unitSystem', $unitSystem, UnitSystem::getList());
78+
Validator::choice(UnitSystem::getList())->assert($unitSystem, 'unitSystem');
8379

8480
$this->options['unitSystem'] = $unitSystem;
8581

@@ -96,7 +92,7 @@ public function getLanguage(): string
9692
*/
9793
public function setLanguage(string $language): self
9894
{
99-
$this->validateChoice('language', $language, Language::getList());
95+
Validator::choice(Language::getList())->assert($language, 'language');
10096

10197
$this->options['language'] = $language;
10298

0 commit comments

Comments
 (0)