Skip to content

Commit 2419859

Browse files
committed
chore: refactored endpoints arguments validation
1 parent 0a20615 commit 2419859

File tree

5 files changed

+86
-28
lines changed

5 files changed

+86
-28
lines changed

src/Endpoint/AirPollutionEndpoint.php

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

55
use Http\Client\Exception;
6+
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\ValidationTrait;
67
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollutionLocationList;
78
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollutionLocation;
89
use ProgrammatorDev\OpenWeatherMap\Exception\ApiErrorException;
@@ -11,15 +12,16 @@
1112

1213
class AirPollutionEndpoint extends AbstractEndpoint
1314
{
15+
use ValidationTrait;
16+
1417
/**
1518
* @throws Exception
1619
* @throws ApiErrorException
1720
* @throws ValidationException
1821
*/
1922
public function getCurrent(float $latitude, float $longitude): AirPollutionLocation
2023
{
21-
Validator::range(-90, 90)->assert($latitude, 'latitude');
22-
Validator::range(-180, 180)->assert($longitude, 'longitude');
24+
$this->validateCoordinate($latitude, $longitude);
2325

2426
$data = $this->sendRequest(
2527
method: 'GET',
@@ -40,8 +42,7 @@ public function getCurrent(float $latitude, float $longitude): AirPollutionLocat
4042
*/
4143
public function getForecast(float $latitude, float $longitude): AirPollutionLocationList
4244
{
43-
Validator::range(-90, 90)->assert($latitude, 'latitude');
44-
Validator::range(-180, 180)->assert($longitude, 'longitude');
45+
$this->validateCoordinate($latitude, $longitude);
4546

4647
$data = $this->sendRequest(
4748
method: 'GET',
@@ -67,10 +68,8 @@ public function getHistory(
6768
\DateTimeInterface $endDate
6869
): AirPollutionLocationList
6970
{
70-
Validator::range(-90, 90)->assert($latitude, 'latitude');
71-
Validator::range(-180, 180)->assert($longitude, 'longitude');
72-
Validator::lessThan(new \DateTime('now'))->assert($endDate, 'endDate');
73-
Validator::greaterThan($startDate)->assert($endDate, 'endDate');
71+
$this->validateCoordinate($latitude, $longitude);
72+
$this->validateDateRange($startDate, $endDate);
7473

7574
$timezoneUtc = new \DateTimeZone('UTC');
7675

src/Endpoint/GeocodingEndpoint.php

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

55
use Http\Client\Exception;
6+
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\ValidationTrait;
67
use ProgrammatorDev\OpenWeatherMap\Entity\Geocoding\ZipCodeLocation;
78
use ProgrammatorDev\OpenWeatherMap\Entity\Location;
89
use ProgrammatorDev\OpenWeatherMap\Exception\ApiErrorException;
@@ -12,6 +13,7 @@
1213

1314
class GeocodingEndpoint extends AbstractEndpoint
1415
{
16+
use ValidationTrait;
1517
use CreateEntityListTrait;
1618

1719
private const NUM_RESULTS = 5;
@@ -26,8 +28,8 @@ class GeocodingEndpoint extends AbstractEndpoint
2628
*/
2729
public function getByLocationName(string $locationName, int $numResults = self::NUM_RESULTS): array
2830
{
29-
Validator::notBlank()->assert($locationName, 'locationName');
30-
Validator::greaterThan(0)->assert($numResults, 'numResults');
31+
$this->validateSearchQuery($locationName, 'locationName');
32+
$this->validateNumResults($numResults);
3133

3234
$data = $this->sendRequest(
3335
method: 'GET',
@@ -48,8 +50,8 @@ public function getByLocationName(string $locationName, int $numResults = self::
4850
*/
4951
public function getByZipCode(string $zipCode, string $countryCode): ZipCodeLocation
5052
{
51-
Validator::notBlank()->assert($zipCode, 'zipCode');
52-
Validator::country()->assert($countryCode, 'countryCode');
53+
$this->validateSearchQuery($zipCode, 'zipCode');
54+
$this->validateCountryCode($countryCode);
5355

5456
$data = $this->sendRequest(
5557
method: 'GET',
@@ -70,9 +72,8 @@ public function getByZipCode(string $zipCode, string $countryCode): ZipCodeLocat
7072
*/
7173
public function getByCoordinate(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): array
7274
{
73-
Validator::range(-90, 90)->assert($latitude, 'latitude');
74-
Validator::range(-180, 180)->assert($longitude, 'longitude');
75-
Validator::greaterThan(0)->assert($numResults, 'numResults');
75+
$this->validateCoordinate($latitude, $longitude);
76+
$this->validateNumResults($numResults);
7677

7778
$data = $this->sendRequest(
7879
method: 'GET',

src/Endpoint/OneCallEndpoint.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\Client\Exception;
66
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\LanguageTrait;
77
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\UnitSystemTrait;
8+
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\ValidationTrait;
89
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherAggregate;
910
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherLocation;
1011
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\OneCall;
@@ -16,6 +17,7 @@ class OneCallEndpoint extends AbstractEndpoint
1617
{
1718
use UnitSystemTrait;
1819
use LanguageTrait;
20+
use ValidationTrait;
1921

2022
/**
2123
* @throws Exception
@@ -24,8 +26,7 @@ class OneCallEndpoint extends AbstractEndpoint
2426
*/
2527
public function getWeather(float $latitude, float $longitude): OneCall
2628
{
27-
Validator::range(-90, 90)->assert($latitude, 'latitude');
28-
Validator::range(-180, 180)->assert($longitude, 'longitude');
29+
$this->validateCoordinate($latitude, $longitude);
2930

3031
$data = $this->sendRequest(
3132
method: 'GET',
@@ -48,9 +49,8 @@ public function getWeather(float $latitude, float $longitude): OneCall
4849
*/
4950
public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInterface $dateTime): WeatherLocation
5051
{
51-
Validator::range(-90, 90)->assert($latitude, 'latitude');
52-
Validator::range(-180, 180)->assert($longitude, 'longitude');
53-
Validator::lessThan(new \DateTime('now'))->assert($dateTime, 'dateTime');
52+
$this->validateCoordinate($latitude, $longitude);
53+
$this->validatePastDate($dateTime, 'dateTime');
5454

5555
$data = $this->sendRequest(
5656
method: 'GET',
@@ -74,9 +74,8 @@ public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInt
7474
*/
7575
public function getHistoryAggregate(float $latitude, float $longitude, \DateTimeInterface $date): WeatherAggregate
7676
{
77-
Validator::range(-90, 90)->assert($latitude, 'latitude');
78-
Validator::range(-180, 180)->assert($longitude, 'longitude');
79-
Validator::lessThan(new \DateTime('now'))->assert($date, 'date');
77+
$this->validateCoordinate($latitude, $longitude);
78+
$this->validatePastDate($date, 'date');
8079

8180
$data = $this->sendRequest(
8281
method: 'GET',
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Endpoint\Util;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
7+
8+
trait ValidationTrait
9+
{
10+
/**
11+
* @throws ValidationException
12+
*/
13+
private function validateCoordinate(float $latitude, float $longitude): void
14+
{
15+
Validator::range(-90, 90)->assert($latitude, 'latitude');
16+
Validator::range(-180, 180)->assert($longitude, 'longitude');
17+
}
18+
19+
/**
20+
* @throws ValidationException
21+
*/
22+
private function validateDateRange(\DateTimeInterface $startDate, \DateTimeInterface $endDate): void
23+
{
24+
Validator::lessThan(new \DateTime('now'))->assert($endDate, 'endDate');
25+
Validator::greaterThan($startDate)->assert($endDate, 'endDate');
26+
}
27+
28+
/**
29+
* @throws ValidationException
30+
*/
31+
private function validatePastDate(\DateTimeInterface $date, string $name): void
32+
{
33+
Validator::lessThan(new \DateTime('now'))->assert($date, $name);
34+
}
35+
36+
/**
37+
* @throws ValidationException
38+
*/
39+
private function validateSearchQuery(string $searchQuery, string $name): void
40+
{
41+
Validator::notBlank()->assert($searchQuery, $name);
42+
}
43+
44+
/**
45+
* @throws ValidationException
46+
*/
47+
private function validateNumResults(int $numResults): void
48+
{
49+
Validator::greaterThan(0)->assert($numResults, 'numResults');
50+
}
51+
52+
/**
53+
* @throws ValidationException
54+
*/
55+
private function validateCountryCode(string $countryCode): void
56+
{
57+
Validator::country()->assert($countryCode, 'countryCode');
58+
}
59+
}

src/Endpoint/WeatherEndpoint.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\Client\Exception;
66
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\LanguageTrait;
77
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\UnitSystemTrait;
8+
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\ValidationTrait;
89
use ProgrammatorDev\OpenWeatherMap\Entity\Weather\WeatherLocation;
910
use ProgrammatorDev\OpenWeatherMap\Entity\Weather\WeatherLocationList;
1011
use ProgrammatorDev\OpenWeatherMap\Exception\ApiErrorException;
@@ -15,6 +16,7 @@ class WeatherEndpoint extends AbstractEndpoint
1516
{
1617
use UnitSystemTrait;
1718
use LanguageTrait;
19+
use ValidationTrait;
1820

1921
private const NUM_RESULTS = 40;
2022

@@ -25,8 +27,7 @@ class WeatherEndpoint extends AbstractEndpoint
2527
*/
2628
public function getCurrent(float $latitude, float $longitude): WeatherLocation
2729
{
28-
Validator::range(-90, 90)->assert($latitude, 'latitude');
29-
Validator::range(-180, 180)->assert($longitude, 'longitude');
30+
$this->validateCoordinate($latitude, $longitude);
3031

3132
$data = $this->sendRequest(
3233
method: 'GET',
@@ -49,9 +50,8 @@ public function getCurrent(float $latitude, float $longitude): WeatherLocation
4950
*/
5051
public function getForecast(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): WeatherLocationList
5152
{
52-
Validator::range(-90, 90)->assert($latitude, 'latitude');
53-
Validator::range(-180, 180)->assert($longitude, 'longitude');
54-
Validator::greaterThan(0)->assert($numResults, 'numResults');
53+
$this->validateCoordinate($latitude, $longitude);
54+
$this->validateNumResults($numResults);
5555

5656
$data = $this->sendRequest(
5757
method: 'GET',

0 commit comments

Comments
 (0)