Skip to content

Commit 02a9945

Browse files
committed
chore: throw ValidationException on validators
1 parent 5ecd1a2 commit 02a9945

15 files changed

+83
-24
lines changed

src/Endpoint/AirPollutionEndpoint.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException;
1111
use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
1212
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
13+
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
1314
use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait;
1415
use ProgrammatorDev\OpenWeatherMap\Validator\LessThanValidatorTrait;
1516
use ProgrammatorDev\OpenWeatherMap\Validator\RangeValidatorTrait;
@@ -35,6 +36,7 @@ class AirPollutionEndpoint extends AbstractEndpoint
3536
* @throws UnauthorizedException
3637
* @throws UnexpectedErrorException
3738
* @throws InvalidArgumentException
39+
* @throws ValidationException
3840
*/
3941
public function getCurrent(float $latitude, float $longitude): CurrentAirPollution
4042
{
@@ -60,6 +62,7 @@ public function getCurrent(float $latitude, float $longitude): CurrentAirPolluti
6062
* @throws UnauthorizedException
6163
* @throws UnexpectedErrorException
6264
* @throws InvalidArgumentException
65+
* @throws ValidationException
6366
*/
6467
public function getForecast(float $latitude, float $longitude): AirPollutionList
6568
{
@@ -85,6 +88,7 @@ public function getForecast(float $latitude, float $longitude): AirPollutionList
8588
* @throws UnauthorizedException
8689
* @throws UnexpectedErrorException
8790
* @throws InvalidArgumentException
91+
* @throws ValidationException
8892
*/
8993
public function getHistory(
9094
float $latitude,

src/Endpoint/GeocodingEndpoint.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException;
1111
use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
1212
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
13+
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
1314
use ProgrammatorDev\OpenWeatherMap\Util\CreateEntityListTrait;
1415
use ProgrammatorDev\OpenWeatherMap\Validator\BlankValidatorTrait;
1516
use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait;
@@ -42,6 +43,7 @@ class GeocodingEndpoint extends AbstractEndpoint
4243
* @throws UnauthorizedException
4344
* @throws UnexpectedErrorException
4445
* @throws InvalidArgumentException
46+
* @throws ValidationException
4547
*/
4648
public function getCoordinatesByLocationName(string $locationName, int $numResults = self::NUM_RESULTS): array
4749
{
@@ -68,6 +70,7 @@ public function getCoordinatesByLocationName(string $locationName, int $numResul
6870
* @throws UnauthorizedException
6971
* @throws UnexpectedErrorException
7072
* @throws InvalidArgumentException
73+
* @throws ValidationException
7174
*/
7275
public function getCoordinatesByZipCode(string $zipCode, string $countryCode): ZipCodeLocation
7376
{
@@ -94,6 +97,7 @@ public function getCoordinatesByZipCode(string $zipCode, string $countryCode): Z
9497
* @throws UnauthorizedException
9598
* @throws UnexpectedErrorException
9699
* @throws InvalidArgumentException
100+
* @throws ValidationException
97101
*/
98102
public function getLocationNameByCoordinates(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): array
99103
{

src/Endpoint/OneCallEndpoint.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException;
1414
use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
1515
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
16+
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
1617
use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait;
1718
use ProgrammatorDev\OpenWeatherMap\Validator\LessThanValidatorTrait;
1819
use Psr\Cache\InvalidArgumentException;
@@ -38,6 +39,7 @@ class OneCallEndpoint extends AbstractEndpoint
3839
* @throws UnauthorizedException
3940
* @throws UnexpectedErrorException
4041
* @throws InvalidArgumentException
42+
* @throws ValidationException
4143
*/
4244
public function getWeather(float $latitude, float $longitude): OneCall
4345
{
@@ -65,6 +67,7 @@ public function getWeather(float $latitude, float $longitude): OneCall
6567
* @throws UnauthorizedException
6668
* @throws UnexpectedErrorException
6769
* @throws InvalidArgumentException
70+
* @throws ValidationException
6871
*/
6972
public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInterface $dateTime): HistoryMoment
7073
{
@@ -94,6 +97,7 @@ public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInt
9497
* @throws UnauthorizedException
9598
* @throws UnexpectedErrorException
9699
* @throws InvalidArgumentException
100+
* @throws ValidationException
97101
*/
98102
public function getHistoryDaySummary(float $latitude, float $longitude, \DateTimeInterface $dateTime): HistoryDaySummary
99103
{

src/Endpoint/WeatherEndpoint.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException;
1313
use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
1414
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
15+
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
1516
use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait;
1617
use ProgrammatorDev\OpenWeatherMap\Validator\GreaterThanValidatorTrait;
1718
use Psr\Cache\InvalidArgumentException;
@@ -37,6 +38,7 @@ class WeatherEndpoint extends AbstractEndpoint
3738
* @throws UnauthorizedException
3839
* @throws UnexpectedErrorException
3940
* @throws InvalidArgumentException
41+
* @throws ValidationException
4042
*/
4143
public function getCurrent(float $latitude, float $longitude): CurrentWeather
4244
{
@@ -64,6 +66,7 @@ public function getCurrent(float $latitude, float $longitude): CurrentWeather
6466
* @throws UnauthorizedException
6567
* @throws UnexpectedErrorException
6668
* @throws InvalidArgumentException
69+
* @throws ValidationException
6770
*/
6871
public function getForecast(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): WeatherList
6972
{
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Exception;
4+
5+
class ValidationException extends \Exception {}

src/Validator/BetweenValidatorTrait.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Validator;
44

5+
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
6+
57
trait BetweenValidatorTrait
68
{
9+
/**
10+
* @throws ValidationException
11+
*/
712
private function ValidateBetween(
813
string $name,
914
\DateTimeInterface|int|float $value,
@@ -29,7 +34,7 @@ private function ValidateBetween(
2934
if ($value->getTimestamp() < $startConstraint->getTimestamp() || $value->getTimestamp() > $endConstraint->getTimestamp()) {
3035
$dateFormat = 'Y-m-d H:i:s';
3136

32-
throw new \UnexpectedValueException(
37+
throw new ValidationException(
3338
\sprintf(
3439
'The "%s" date "%s" is invalid. Must be between "%s" and "%s".',
3540
$name,
@@ -42,7 +47,7 @@ private function ValidateBetween(
4247
}
4348

4449
if ($value < $startConstraint || $value > $endConstraint) {
45-
throw new \UnexpectedValueException(
50+
throw new ValidationException(
4651
\sprintf(
4752
'The "%s" value "%d" is invalid. Must be between "%d" and "%d".',
4853
$name,

src/Validator/BlankValidatorTrait.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Validator;
44

5+
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
6+
57
trait BlankValidatorTrait
68
{
9+
/**
10+
* @throws ValidationException
11+
*/
712
private function validateBlank(string $name, string $value): void
813
{
914
if (empty($value)) {
10-
throw new \UnexpectedValueException(
15+
throw new ValidationException(
1116
\sprintf('The "%s" value should not be blank.', $name)
1217
);
1318
}

src/Validator/ChoiceValidatorTrait.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Validator;
44

5+
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
6+
57
trait ChoiceValidatorTrait
68
{
9+
/**
10+
* @throws ValidationException
11+
*/
712
private function validateChoice(string $name, mixed $value, array $options): void
813
{
914
if ( ! in_array($value, $options)) {
10-
throw new \UnexpectedValueException(
15+
throw new ValidationException(
1116
\sprintf(
1217
'The "%s" value "%s" is invalid. Accepted values are: "%s".',
1318
$name,

src/Validator/CoordinateValidatorTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Validator;
44

5+
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
6+
57
trait CoordinateValidatorTrait
68
{
79
use BetweenValidatorTrait;
810

11+
/**
12+
* @throws ValidationException
13+
*/
914
private function validateCoordinate(float $latitude, float $longitude): void
1015
{
1116
$this->validateBetween('latitude', $latitude, -90, 90);

src/Validator/GreaterThanValidatorTrait.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Validator;
44

5+
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
6+
57
trait GreaterThanValidatorTrait
68
{
9+
/**
10+
* @throws ValidationException
11+
*/
712
private function validateGreaterThan(
813
string $name,
914
\DateTimeInterface|int|float $value,
@@ -22,7 +27,7 @@ private function validateGreaterThan(
2227
if ($value->getTimestamp() <= $constraint->getTimestamp()) {
2328
$dateFormat = 'Y-m-d H:i:s';
2429

25-
throw new \UnexpectedValueException(
30+
throw new ValidationException(
2631
\sprintf(
2732
'The "%s" value "%s" is invalid. Must be greater than "%s".',
2833
$name,
@@ -34,7 +39,7 @@ private function validateGreaterThan(
3439
}
3540

3641
if ($value <= $constraint) {
37-
throw new \UnexpectedValueException(
42+
throw new ValidationException(
3843
\sprintf(
3944
'The "%s" value "%d" is invalid. Must be greater than "%d".',
4045
$name,

0 commit comments

Comments
 (0)