Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Endpoint/AirPollutionEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException;
use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait;
use ProgrammatorDev\OpenWeatherMap\Validator\LessThanValidatorTrait;
use ProgrammatorDev\OpenWeatherMap\Validator\RangeValidatorTrait;
Expand All @@ -35,6 +36,7 @@ class AirPollutionEndpoint extends AbstractEndpoint
* @throws UnauthorizedException
* @throws UnexpectedErrorException
* @throws InvalidArgumentException
* @throws ValidationException
*/
public function getCurrent(float $latitude, float $longitude): CurrentAirPollution
{
Expand All @@ -60,6 +62,7 @@ public function getCurrent(float $latitude, float $longitude): CurrentAirPolluti
* @throws UnauthorizedException
* @throws UnexpectedErrorException
* @throws InvalidArgumentException
* @throws ValidationException
*/
public function getForecast(float $latitude, float $longitude): AirPollutionList
{
Expand All @@ -85,6 +88,7 @@ public function getForecast(float $latitude, float $longitude): AirPollutionList
* @throws UnauthorizedException
* @throws UnexpectedErrorException
* @throws InvalidArgumentException
* @throws ValidationException
*/
public function getHistory(
float $latitude,
Expand Down
4 changes: 4 additions & 0 deletions src/Endpoint/GeocodingEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException;
use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
use ProgrammatorDev\OpenWeatherMap\Util\CreateEntityListTrait;
use ProgrammatorDev\OpenWeatherMap\Validator\BlankValidatorTrait;
use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait;
Expand Down Expand Up @@ -42,6 +43,7 @@ class GeocodingEndpoint extends AbstractEndpoint
* @throws UnauthorizedException
* @throws UnexpectedErrorException
* @throws InvalidArgumentException
* @throws ValidationException
*/
public function getCoordinatesByLocationName(string $locationName, int $numResults = self::NUM_RESULTS): array
{
Expand All @@ -68,6 +70,7 @@ public function getCoordinatesByLocationName(string $locationName, int $numResul
* @throws UnauthorizedException
* @throws UnexpectedErrorException
* @throws InvalidArgumentException
* @throws ValidationException
*/
public function getCoordinatesByZipCode(string $zipCode, string $countryCode): ZipCodeLocation
{
Expand All @@ -94,6 +97,7 @@ public function getCoordinatesByZipCode(string $zipCode, string $countryCode): Z
* @throws UnauthorizedException
* @throws UnexpectedErrorException
* @throws InvalidArgumentException
* @throws ValidationException
*/
public function getLocationNameByCoordinates(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): array
{
Expand Down
4 changes: 4 additions & 0 deletions src/Endpoint/OneCallEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException;
use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait;
use ProgrammatorDev\OpenWeatherMap\Validator\LessThanValidatorTrait;
use Psr\Cache\InvalidArgumentException;
Expand All @@ -38,6 +39,7 @@ class OneCallEndpoint extends AbstractEndpoint
* @throws UnauthorizedException
* @throws UnexpectedErrorException
* @throws InvalidArgumentException
* @throws ValidationException
*/
public function getWeather(float $latitude, float $longitude): OneCall
{
Expand Down Expand Up @@ -65,6 +67,7 @@ public function getWeather(float $latitude, float $longitude): OneCall
* @throws UnauthorizedException
* @throws UnexpectedErrorException
* @throws InvalidArgumentException
* @throws ValidationException
*/
public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInterface $dateTime): HistoryMoment
{
Expand Down Expand Up @@ -94,6 +97,7 @@ public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInt
* @throws UnauthorizedException
* @throws UnexpectedErrorException
* @throws InvalidArgumentException
* @throws ValidationException
*/
public function getHistoryDaySummary(float $latitude, float $longitude, \DateTimeInterface $dateTime): HistoryDaySummary
{
Expand Down
3 changes: 3 additions & 0 deletions src/Endpoint/WeatherEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use ProgrammatorDev\OpenWeatherMap\Exception\TooManyRequestsException;
use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
use ProgrammatorDev\OpenWeatherMap\Validator\CoordinateValidatorTrait;
use ProgrammatorDev\OpenWeatherMap\Validator\GreaterThanValidatorTrait;
use Psr\Cache\InvalidArgumentException;
Expand All @@ -37,6 +38,7 @@ class WeatherEndpoint extends AbstractEndpoint
* @throws UnauthorizedException
* @throws UnexpectedErrorException
* @throws InvalidArgumentException
* @throws ValidationException
*/
public function getCurrent(float $latitude, float $longitude): CurrentWeather
{
Expand Down Expand Up @@ -64,6 +66,7 @@ public function getCurrent(float $latitude, float $longitude): CurrentWeather
* @throws UnauthorizedException
* @throws UnexpectedErrorException
* @throws InvalidArgumentException
* @throws ValidationException
*/
public function getForecast(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): WeatherList
{
Expand Down
5 changes: 5 additions & 0 deletions src/Exception/ValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Exception;

class ValidationException extends \Exception {}
9 changes: 7 additions & 2 deletions src/Validator/BetweenValidatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace ProgrammatorDev\OpenWeatherMap\Validator;

use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;

trait BetweenValidatorTrait
{
/**
* @throws ValidationException
*/
private function ValidateBetween(
string $name,
\DateTimeInterface|int|float $value,
Expand All @@ -29,7 +34,7 @@ private function ValidateBetween(
if ($value->getTimestamp() < $startConstraint->getTimestamp() || $value->getTimestamp() > $endConstraint->getTimestamp()) {
$dateFormat = 'Y-m-d H:i:s';

throw new \UnexpectedValueException(
throw new ValidationException(
\sprintf(
'The "%s" date "%s" is invalid. Must be between "%s" and "%s".',
$name,
Expand All @@ -42,7 +47,7 @@ private function ValidateBetween(
}

if ($value < $startConstraint || $value > $endConstraint) {
throw new \UnexpectedValueException(
throw new ValidationException(
\sprintf(
'The "%s" value "%d" is invalid. Must be between "%d" and "%d".',
$name,
Expand Down
7 changes: 6 additions & 1 deletion src/Validator/BlankValidatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

namespace ProgrammatorDev\OpenWeatherMap\Validator;

use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;

trait BlankValidatorTrait
{
/**
* @throws ValidationException
*/
private function validateBlank(string $name, string $value): void
{
if (empty($value)) {
throw new \UnexpectedValueException(
throw new ValidationException(
\sprintf('The "%s" value should not be blank.', $name)
);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Validator/ChoiceValidatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

namespace ProgrammatorDev\OpenWeatherMap\Validator;

use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;

trait ChoiceValidatorTrait
{
/**
* @throws ValidationException
*/
private function validateChoice(string $name, mixed $value, array $options): void
{
if ( ! in_array($value, $options)) {
throw new \UnexpectedValueException(
throw new ValidationException(
\sprintf(
'The "%s" value "%s" is invalid. Accepted values are: "%s".',
$name,
Expand Down
5 changes: 5 additions & 0 deletions src/Validator/CoordinateValidatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

namespace ProgrammatorDev\OpenWeatherMap\Validator;

use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;

trait CoordinateValidatorTrait
{
use BetweenValidatorTrait;

/**
* @throws ValidationException
*/
private function validateCoordinate(float $latitude, float $longitude): void
{
$this->validateBetween('latitude', $latitude, -90, 90);
Expand Down
9 changes: 7 additions & 2 deletions src/Validator/GreaterThanValidatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace ProgrammatorDev\OpenWeatherMap\Validator;

use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;

trait GreaterThanValidatorTrait
{
/**
* @throws ValidationException
*/
private function validateGreaterThan(
string $name,
\DateTimeInterface|int|float $value,
Expand All @@ -22,7 +27,7 @@ private function validateGreaterThan(
if ($value->getTimestamp() <= $constraint->getTimestamp()) {
$dateFormat = 'Y-m-d H:i:s';

throw new \UnexpectedValueException(
throw new ValidationException(
\sprintf(
'The "%s" value "%s" is invalid. Must be greater than "%s".',
$name,
Expand All @@ -34,7 +39,7 @@ private function validateGreaterThan(
}

if ($value <= $constraint) {
throw new \UnexpectedValueException(
throw new ValidationException(
\sprintf(
'The "%s" value "%d" is invalid. Must be greater than "%d".',
$name,
Expand Down
9 changes: 7 additions & 2 deletions src/Validator/LessThanValidatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace ProgrammatorDev\OpenWeatherMap\Validator;

use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;

trait LessThanValidatorTrait
{
/**
* @throws ValidationException
*/
private function validateLessThan(
string $name,
\DateTimeInterface|int|float $value,
Expand All @@ -22,7 +27,7 @@ private function validateLessThan(
if ($value->getTimestamp() >= $constraint->getTimestamp()) {
$dateFormat = 'Y-m-d H:i:s';

throw new \UnexpectedValueException(
throw new ValidationException(
\sprintf(
'The "%s" value "%s" is invalid. Must be less than "%s".',
$name,
Expand All @@ -34,7 +39,7 @@ private function validateLessThan(
}

if ($value >= $constraint) {
throw new \UnexpectedValueException(
throw new ValidationException(
\sprintf(
'The "%s" value "%d" is invalid. Must be less than "%d".',
$name,
Expand Down
11 changes: 8 additions & 3 deletions src/Validator/RangeValidatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

namespace ProgrammatorDev\OpenWeatherMap\Validator;

use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;

trait RangeValidatorTrait
{
use LessThanValidatorTrait;

/**
* @throws ValidationException
*/
public function validateRange(
string $startName,
string $endName,
Expand All @@ -23,11 +28,11 @@ public function validateRange(
try {
$this->validateLessThan('startValue', $startValue, $endValue);
}
catch (\UnexpectedValueException) {
catch (ValidationException) {
if ($startValue instanceof \DateTimeInterface) {
$dateFormat = 'Y-m-d H:i:s';

throw new \UnexpectedValueException(
throw new ValidationException(
\sprintf(
'The "%s" value "%s" should be less than the "%s" value "%s".',
$startName,
Expand All @@ -38,7 +43,7 @@ public function validateRange(
);
}

throw new \UnexpectedValueException(
throw new ValidationException(
\sprintf(
'The "%s" value "%d" should be less than the "%s" value "%d".',
$startName,
Expand Down
3 changes: 2 additions & 1 deletion tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use ProgrammatorDev\OpenWeatherMap\Config;
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
use ProgrammatorDev\OpenWeatherMap\HttpClient\HttpClientBuilder;
use ProgrammatorDev\OpenWeatherMap\Test\DataProvider\InvalidParamDataProvider;
use Psr\Cache\CacheItemPoolInterface;
Expand Down Expand Up @@ -100,7 +101,7 @@ public function testConfigSetApplicationKey()

public function testConfigSetApplicationKeyWithBlankValue()
{
$this->expectException(\UnexpectedValueException::class);
$this->expectException(ValidationException::class);
$this->config->setApplicationKey('');
}

Expand Down
22 changes: 12 additions & 10 deletions tests/DataProvider/InvalidParamDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

namespace ProgrammatorDev\OpenWeatherMap\Test\DataProvider;

use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;

class InvalidParamDataProvider
{
public static function provideInvalidCoordinateData(): \Generator
{
yield 'latitude lower than -90' => [-91, -9.1365919, \UnexpectedValueException::class];
yield 'latitude greater than 90' => [91, -9.1365919, \UnexpectedValueException::class];
yield 'longitude lower than -180' => [38.7077507, -181, \UnexpectedValueException::class];
yield 'longitude greater than 180' => [38.7077507, 181, \UnexpectedValueException::class];
yield 'latitude lower than -90' => [-91, -9.1365919, ValidationException::class];
yield 'latitude greater than 90' => [91, -9.1365919, ValidationException::class];
yield 'longitude lower than -180' => [38.7077507, -181, ValidationException::class];
yield 'longitude greater than 180' => [38.7077507, 181, ValidationException::class];
}

public static function provideInvalidPastDateData(): \Generator
{
yield 'invalid past date' => [
new \DateTimeImmutable('1 days'),
\UnexpectedValueException::class
ValidationException::class
];
}

Expand All @@ -25,23 +27,23 @@ public static function provideInvalidDateRangeData(): \Generator
yield 'start date greater than end date' => [
new \DateTimeImmutable('-4 days'),
new \DateTimeImmutable('-5 days'),
\UnexpectedValueException::class
ValidationException::class
];
}

public static function provideInvalidNumResultsData(): \Generator
{
yield 'equal to zero num results' => [0, \UnexpectedValueException::class];
yield 'negative num results' => [-1, \UnexpectedValueException::class];
yield 'equal to zero num results' => [0, ValidationException::class];
yield 'negative num results' => [-1, ValidationException::class];
}

public static function provideInvalidMeasurementSystemData(): \Generator
{
yield 'not allowed measurement system' => ['invalid', \UnexpectedValueException::class];
yield 'not allowed measurement system' => ['invalid', ValidationException::class];
}

public static function provideInvalidLanguageData(): \Generator
{
yield 'not allowed language' => ['invalid', \UnexpectedValueException::class];
yield 'not allowed language' => ['invalid', ValidationException::class];
}
}
Loading