Skip to content

Commit 90e58e8

Browse files
authored
Merge pull request #37 from programmatordev/OPA-43-replace-validation-with-library
Replace validation with library
2 parents 637780e + 0fc6051 commit 90e58e8

20 files changed

+60
-360
lines changed

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/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

src/Endpoint/AirPollutionEndpoint.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,11 @@
1010
use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException;
1111
use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
1212
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
13-
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
14-
use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait;
15-
use ProgrammatorDev\OpenWeatherMap\Validator\LessThanValidatorTrait;
16-
use ProgrammatorDev\OpenWeatherMap\Validator\RangeValidatorTrait;
13+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
14+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
1715

1816
class AirPollutionEndpoint extends AbstractEndpoint
1917
{
20-
use CoordinateValidatorTrait;
21-
use LessThanValidatorTrait;
22-
use RangeValidatorTrait;
23-
2418
private string $urlAirPollution = 'https://api.openweathermap.org/data/2.5/air_pollution';
2519

2620
private string $urlAirPollutionForecast = 'https://api.openweathermap.org/data/2.5/air_pollution/forecast';
@@ -38,7 +32,8 @@ class AirPollutionEndpoint extends AbstractEndpoint
3832
*/
3933
public function getCurrent(float $latitude, float $longitude): AirPollutionLocation
4034
{
41-
$this->validateCoordinate($latitude, $longitude);
35+
Validator::range(-90, 90)->assert($latitude, 'latitude');
36+
Validator::range(-180, 180)->assert($longitude, 'longitude');
4237

4338
$data = $this->sendRequest(
4439
method: 'GET',
@@ -63,7 +58,8 @@ public function getCurrent(float $latitude, float $longitude): AirPollutionLocat
6358
*/
6459
public function getForecast(float $latitude, float $longitude): AirPollutionLocationList
6560
{
66-
$this->validateCoordinate($latitude, $longitude);
61+
Validator::range(-90, 90)->assert($latitude, 'latitude');
62+
Validator::range(-180, 180)->assert($longitude, 'longitude');
6763

6864
$data = $this->sendRequest(
6965
method: 'GET',
@@ -93,10 +89,10 @@ public function getHistory(
9389
\DateTimeInterface $endDate
9490
): AirPollutionLocationList
9591
{
96-
$this->validateCoordinate($latitude, $longitude);
97-
$this->validateLessThan('startDate', $startDate, new \DateTimeImmutable('now'));
98-
$this->validateLessThan('endDate', $endDate, new \DateTimeImmutable('now'));
99-
$this->validateRange('startDate', 'endDate', $startDate, $endDate);
92+
Validator::range(-90, 90)->assert($latitude, 'latitude');
93+
Validator::range(-180, 180)->assert($longitude, 'longitude');
94+
Validator::lessThan(new \DateTime('now'))->assert($endDate, 'endDate');
95+
Validator::greaterThan($startDate)->assert($endDate, 'endDate');
10096

10197
$timezoneUtc = new \DateTimeZone('UTC');
10298

src/Endpoint/GeocodingEndpoint.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,13 @@
1010
use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException;
1111
use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
1212
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
13-
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
1413
use ProgrammatorDev\OpenWeatherMap\Util\CreateEntityListTrait;
15-
use ProgrammatorDev\OpenWeatherMap\Validator\BlankValidatorTrait;
16-
use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait;
17-
use ProgrammatorDev\OpenWeatherMap\Validator\GreaterThanValidatorTrait;
14+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
15+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
1816

1917
class GeocodingEndpoint extends AbstractEndpoint
2018
{
2119
use CreateEntityListTrait;
22-
use BlankValidatorTrait;
23-
use CoordinateValidatorTrait;
24-
use GreaterThanValidatorTrait;
2520

2621
private const NUM_RESULTS = 5;
2722

@@ -45,8 +40,8 @@ class GeocodingEndpoint extends AbstractEndpoint
4540
*/
4641
public function getByLocationName(string $locationName, int $numResults = self::NUM_RESULTS): array
4742
{
48-
$this->validateBlank('locationName', $locationName);
49-
$this->validateGreaterThan('numResults', $numResults, 0);
43+
Validator::notBlank()->assert($locationName, 'locationName');
44+
Validator::greaterThan(0)->assert($numResults, 'numResults');
5045

5146
$data = $this->sendRequest(
5247
method: 'GET',
@@ -71,8 +66,8 @@ public function getByLocationName(string $locationName, int $numResults = self::
7166
*/
7267
public function getByZipCode(string $zipCode, string $countryCode): ZipCodeLocation
7368
{
74-
$this->validateBlank('zipCode', $zipCode);
75-
$this->validateBlank('countryCode', $countryCode);
69+
Validator::notBlank()->assert($zipCode, 'zipCode');
70+
Validator::notBlank()->assert($countryCode, 'countryCode');
7671

7772
$data = $this->sendRequest(
7873
method: 'GET',
@@ -97,8 +92,9 @@ public function getByZipCode(string $zipCode, string $countryCode): ZipCodeLocat
9792
*/
9893
public function getByCoordinate(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): array
9994
{
100-
$this->validateCoordinate($latitude, $longitude);
101-
$this->validateGreaterThan('numResults', $numResults, 0);
95+
Validator::range(-90, 90)->assert($latitude, 'latitude');
96+
Validator::range(-180, 180)->assert($longitude, 'longitude');
97+
Validator::greaterThan(0)->assert($numResults, 'numResults');
10298

10399
$data = $this->sendRequest(
104100
method: 'GET',

src/Endpoint/OneCallEndpoint.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313
use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException;
1414
use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
1515
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
16-
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
17-
use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait;
18-
use ProgrammatorDev\OpenWeatherMap\Validator\LessThanValidatorTrait;
16+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
17+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
1918

2019
class OneCallEndpoint extends AbstractEndpoint
2120
{
2221
use WithUnitSystemTrait;
2322
use WithLanguageTrait;
24-
use CoordinateValidatorTrait;
25-
use LessThanValidatorTrait;
2623

2724
private string $urlOneCall = 'https://api.openweathermap.org/data/3.0/onecall';
2825

@@ -41,7 +38,8 @@ class OneCallEndpoint extends AbstractEndpoint
4138
*/
4239
public function getWeather(float $latitude, float $longitude): OneCall
4340
{
44-
$this->validateCoordinate($latitude, $longitude);
41+
Validator::range(-90, 90)->assert($latitude, 'latitude');
42+
Validator::range(-180, 180)->assert($longitude, 'longitude');
4543

4644
$data = $this->sendRequest(
4745
method: 'GET',
@@ -68,8 +66,9 @@ public function getWeather(float $latitude, float $longitude): OneCall
6866
*/
6967
public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInterface $dateTime): WeatherLocation
7068
{
71-
$this->validateCoordinate($latitude, $longitude);
72-
$this->validateLessThan('dateTime', $dateTime, new \DateTimeImmutable('now'));
69+
Validator::range(-90, 90)->assert($latitude, 'latitude');
70+
Validator::range(-180, 180)->assert($longitude, 'longitude');
71+
Validator::lessThan(new \DateTime('now'))->assert($dateTime, 'dateTime');
7372

7473
$data = $this->sendRequest(
7574
method: 'GET',
@@ -97,8 +96,9 @@ public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInt
9796
*/
9897
public function getHistoryAggregate(float $latitude, float $longitude, \DateTimeInterface $date): WeatherAggregate
9998
{
100-
$this->validateCoordinate($latitude, $longitude);
101-
$this->validateLessThan('date', $date, new \DateTimeImmutable('now'));
99+
Validator::range(-90, 90)->assert($latitude, 'latitude');
100+
Validator::range(-180, 180)->assert($longitude, 'longitude');
101+
Validator::lessThan(new \DateTime('now'))->assert($date, 'date');
102102

103103
$data = $this->sendRequest(
104104
method: 'GET',

src/Endpoint/Util/WithLanguageTrait.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
namespace ProgrammatorDev\OpenWeatherMap\Endpoint\Util;
44

55
use ProgrammatorDev\OpenWeatherMap\Language\Language;
6-
use ProgrammatorDev\OpenWeatherMap\Validator\ChoiceValidatorTrait;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
78

89
trait WithLanguageTrait
910
{
10-
use ChoiceValidatorTrait;
11-
11+
/**
12+
* @throws ValidationException
13+
*/
1214
public function withLanguage(string $language): static
1315
{
14-
$this->validateChoice('language', $language, Language::getList());
16+
Validator::choice(Language::getList())->assert($language, 'language');
1517

1618
$clone = clone $this;
1719
$clone->language = $language;

src/Endpoint/Util/WithUnitSystemTrait.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
namespace ProgrammatorDev\OpenWeatherMap\Endpoint\Util;
44

55
use ProgrammatorDev\OpenWeatherMap\UnitSystem\UnitSystem;
6-
use ProgrammatorDev\OpenWeatherMap\Validator\ChoiceValidatorTrait;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
78

89
trait WithUnitSystemTrait
910
{
10-
use ChoiceValidatorTrait;
11-
11+
/**
12+
* @throws ValidationException
13+
*/
1214
public function withUnitSystem(string $unitSystem): static
1315
{
14-
$this->validateChoice('unitSystem', $unitSystem, UnitSystem::getList());
16+
Validator::choice(UnitSystem::getList())->assert($unitSystem, 'unitSystem');
1517

1618
$clone = clone $this;
1719
$clone->unitSystem = $unitSystem;

src/Endpoint/WeatherEndpoint.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@
1212
use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException;
1313
use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
1414
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
15-
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
16-
use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait;
17-
use ProgrammatorDev\OpenWeatherMap\Validator\GreaterThanValidatorTrait;
15+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
16+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
1817

1918
class WeatherEndpoint extends AbstractEndpoint
2019
{
2120
use WithUnitSystemTrait;
2221
use WithLanguageTrait;
23-
use CoordinateValidatorTrait;
24-
use GreaterThanValidatorTrait;
2522

2623
private const NUM_RESULTS = 40;
2724

@@ -40,7 +37,8 @@ class WeatherEndpoint extends AbstractEndpoint
4037
*/
4138
public function getCurrent(float $latitude, float $longitude): WeatherLocation
4239
{
43-
$this->validateCoordinate($latitude, $longitude);
40+
Validator::range(-90, 90)->assert($latitude, 'latitude');
41+
Validator::range(-180, 180)->assert($longitude, 'longitude');
4442

4543
$data = $this->sendRequest(
4644
method: 'GET',
@@ -67,8 +65,9 @@ public function getCurrent(float $latitude, float $longitude): WeatherLocation
6765
*/
6866
public function getForecast(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): WeatherLocationList
6967
{
70-
$this->validateCoordinate($latitude, $longitude);
71-
$this->validateGreaterThan('numResults', $numResults, 0);
68+
Validator::range(-90, 90)->assert($latitude, 'latitude');
69+
Validator::range(-180, 180)->assert($longitude, 'longitude');
70+
Validator::greaterThan(0)->assert($numResults, 'numResults');
7271

7372
$data = $this->sendRequest(
7473
method: 'GET',

src/Exception/ValidationException.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)