Skip to content

Commit b2ef43e

Browse files
authored
Merge pull request #62 from programmatordev/OPA-57-remove-validation
Remove parameters validation
2 parents a51437f + 3d52826 commit b2ef43e

21 files changed

+16
-259
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"php": ">=8.1",
1616
"myclabs/deep-copy": "^1.13",
1717
"programmatordev/php-api-sdk": "^2.0",
18-
"programmatordev/yet-another-php-validator": "^1.3",
1918
"symfony/options-resolver": "^6.4|^7.3"
2019
},
2120
"require-dev": {

docs/04-error-handling.md

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# Error Handling
22

3-
- [API Errors](#api-errors)
4-
- [Validation Errors](#validation-errors)
5-
63
## API Errors
74

85
To handle API response errors, multiple exceptions are provided. You can see all available in the following example:
@@ -15,13 +12,9 @@ use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
1512
use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
1613

1714
try {
18-
$location = $api->geocoding()->getByZipCode('1000-001', 'pt');
19-
$coordinate = $location->getCoordinate();
15+
// ...
2016

21-
$weather = $api->oneCall()->getWeather(
22-
$coordinate->getLatitude(),
23-
$coordinate->getLongitude()
24-
);
17+
$weather = $api->oneCall()->getWeather($latitude, $longitude);
2518
}
2619
// bad request to the API
2720
catch (BadRequestException $exception) {
@@ -33,7 +26,7 @@ catch (UnauthorizedException $exception) {
3326
echo $exception->getCode(); // 401
3427
echo $exception->getMessage();
3528
}
36-
// resource not found
29+
// resource not found,
3730
// for example, when trying to get a location with a zip code that does not exist
3831
catch (NotFoundException $exception) {
3932
echo $exception->getCode(); // 404
@@ -57,35 +50,13 @@ To catch all API errors with a single exception, `ApiErrorException` is availabl
5750
use ProgrammatorDev\OpenWeatherMap\Exception\ApiErrorException;
5851

5952
try {
60-
$location = $api->geocoding()->getByZipCode('1000-001', 'pt');
61-
$coordinate = $location->getCoordinate();
62-
63-
$weather = $api->oneCall()->getWeather(
64-
$coordinate->getLatitude(),
65-
$coordinate->getLongitude()
66-
);
53+
// ...
54+
55+
$weather = $api->oneCall()->getWeather($latitude, $longitude);
6756
}
6857
// catches all API response errors
6958
catch (ApiErrorException $exception) {
7059
echo $exception->getCode();
7160
echo $exception->getMessage();
7261
}
73-
```
74-
75-
## Validation Errors
76-
77-
To catch invalid input data (like an out of range coordinate, blank location name, etc.), the `ValidationException` is available:
78-
79-
```php
80-
use ProgrammatorDev\Validator\Exception\ValidationException;
81-
82-
try {
83-
// an invalid latitude value is given
84-
$weather = $api->weather()->getCurrent(999, 50);
85-
}
86-
catch (ValidationException $exception) {
87-
// should print:
88-
// The latitude value should be between -90 and 90, 999 given.
89-
echo $exception->getMessage();
90-
}
9162
```

src/Entity/Icon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Icon
1111
public function __construct(array $data)
1212
{
1313
$this->id = $data['icon'];
14-
$this->url = \sprintf('https://openweathermap.org/img/wn/%s@4x.png', $this->id);
14+
$this->url = sprintf('https://openweathermap.org/img/wn/%s@4x.png', $this->id);
1515
}
1616

1717
public function getId(): string

src/OpenWeatherMap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private function configureApi(): void
8686

8787
// if there was a response with an error status code
8888
if ($statusCode >= 400) {
89-
$error = \json_decode($response->getBody()->getContents(), true);
89+
$error = json_decode($response->getBody()->getContents(), true);
9090

9191
match ($statusCode) {
9292
400 => throw new BadRequestException($error),
@@ -101,7 +101,7 @@ private function configureApi(): void
101101
$this->addResponseContentsListener(function(ResponseContentsEvent $event) {
102102
// decode json string response into an array
103103
$contents = $event->getContents();
104-
$contents = \json_decode($contents, true);
104+
$contents = json_decode($contents, true);
105105

106106
$event->setContents($contents);
107107
});

src/Resource/AirPollutionResource.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@
55
use ProgrammatorDev\Api\Method;
66
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollution;
77
use ProgrammatorDev\OpenWeatherMap\Entity\AirPollution\AirPollutionCollection;
8-
use ProgrammatorDev\Validator\Exception\ValidationException;
98
use Psr\Http\Client\ClientExceptionInterface;
109

1110
class AirPollutionResource extends Resource
1211
{
1312
/**
1413
* Get access to current air pollution data
1514
*
16-
* @throws ValidationException
1715
* @throws ClientExceptionInterface
1816
*/
1917
public function getCurrent(float $latitude, float $longitude): AirPollution
2018
{
21-
$this->validateCoordinate($latitude, $longitude);
22-
2319
$data = $this->api->request(
2420
method: Method::GET,
2521
path: '/data/2.5/air_pollution',
@@ -35,13 +31,10 @@ public function getCurrent(float $latitude, float $longitude): AirPollution
3531
/**
3632
* Get access to air pollution forecast data per hour
3733
*
38-
* @throws ValidationException
3934
* @throws ClientExceptionInterface
4035
*/
4136
public function getForecast(float $latitude, float $longitude): AirPollutionCollection
4237
{
43-
$this->validateCoordinate($latitude, $longitude);
44-
4538
$data = $this->api->request(
4639
method: Method::GET,
4740
path: '/data/2.5/air_pollution/forecast',
@@ -57,7 +50,6 @@ public function getForecast(float $latitude, float $longitude): AirPollutionColl
5750
/**
5851
* Get access to historical air pollution data per hour between two dates
5952
*
60-
* @throws ValidationException
6153
* @throws ClientExceptionInterface
6254
*/
6355
public function getHistory(
@@ -67,9 +59,6 @@ public function getHistory(
6759
\DateTimeInterface $endDate
6860
): AirPollutionCollection
6961
{
70-
$this->validateCoordinate($latitude, $longitude);
71-
$this->validateDateOrder($startDate, $endDate);
72-
7362
$utcTimezone = new \DateTimeZone('UTC');
7463

7564
$data = $this->api->request(

src/Resource/GeocodingResource.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use ProgrammatorDev\OpenWeatherMap\Entity\Geocoding\ZipLocation;
77
use ProgrammatorDev\OpenWeatherMap\Entity\Location;
88
use ProgrammatorDev\OpenWeatherMap\Util\EntityTrait;
9-
use ProgrammatorDev\Validator\Exception\ValidationException;
109
use Psr\Http\Client\ClientExceptionInterface;
1110

1211
class GeocodingResource extends Resource
@@ -19,14 +18,10 @@ class GeocodingResource extends Resource
1918
* Get geographical coordinates (latitude, longitude) by using the name of the location (city name or area name)
2019
*
2120
* @return Location[]
22-
* @throws ValidationException
2321
* @throws ClientExceptionInterface
2422
*/
2523
public function getByLocationName(string $locationName, int $numResults = self::NUM_RESULTS): array
2624
{
27-
$this->validateQuery($locationName, 'locationName');
28-
$this->validatePositive($numResults, 'numResults');
29-
3025
$data = $this->api->request(
3126
method: Method::GET,
3227
path: '/geo/1.0/direct',
@@ -42,16 +37,12 @@ public function getByLocationName(string $locationName, int $numResults = self::
4237
/**
4338
* Get geographical coordinates (latitude, longitude) by using the zip/postal code
4439
*
45-
* @throws ValidationException
4640
* @throws ClientExceptionInterface
4741
*/
4842
public function getByZipCode(string $zipCode, string $countryCode): ZipLocation
4943
{
50-
$this->validateQuery($zipCode, 'zipCode');
51-
$this->validateCountryCode($countryCode);
52-
5344
$data = $this->api->request(
54-
method: 'GET',
45+
method: Method::GET,
5546
path: '/geo/1.0/zip',
5647
query: [
5748
'zip' => \sprintf('%s,%s', $zipCode, $countryCode)
@@ -62,17 +53,13 @@ public function getByZipCode(string $zipCode, string $countryCode): ZipLocation
6253
}
6354

6455
/**
65-
* Get name of the location (city name or area name) by using geographical coordinates (latitude, longitude)
56+
* Get the name of the location (city name or area name) by using geographical coordinates (latitude, longitude)
6657
*
6758
* @return Location[]
68-
* @throws ValidationException
6959
* @throws ClientExceptionInterface
7060
*/
7161
public function getByCoordinate(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): array
7262
{
73-
$this->validateCoordinate($latitude, $longitude);
74-
$this->validatePositive($numResults, 'numResults');
75-
7663
$data = $this->api->request(
7764
method: Method::GET,
7865
path: '/geo/1.0/reverse',

src/Resource/OneCallResource.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\WeatherSummary;
99
use ProgrammatorDev\OpenWeatherMap\Resource\Util\LanguageTrait;
1010
use ProgrammatorDev\OpenWeatherMap\Resource\Util\UnitSystemTrait;
11-
use ProgrammatorDev\Validator\Exception\ValidationException;
1211
use Psr\Http\Client\ClientExceptionInterface;
1312

1413
class OneCallResource extends Resource
@@ -20,13 +19,10 @@ class OneCallResource extends Resource
2019
* Get access to current weather, minute forecast for 1 hour, hourly forecast for 48 hours,
2120
* daily forecast for 8 days and government weather alerts
2221
*
23-
* @throws ValidationException
2422
* @throws ClientExceptionInterface
2523
*/
2624
public function getWeather(float $latitude, float $longitude): Weather
2725
{
28-
$this->validateCoordinate($latitude, $longitude);
29-
3026
$data = $this->api->request(
3127
method: Method::GET,
3228
path: '/data/3.0/onecall',
@@ -42,13 +38,10 @@ public function getWeather(float $latitude, float $longitude): Weather
4238
/**
4339
* Get access to weather data for any datetime
4440
*
45-
* @throws ValidationException
4641
* @throws ClientExceptionInterface
4742
*/
4843
public function getWeatherByDate(float $latitude, float $longitude, \DateTimeInterface $dateTime): WeatherMoment
4944
{
50-
$this->validateCoordinate($latitude, $longitude);
51-
5245
$utcTimezone = new \DateTimeZone('UTC');
5346

5447
$data = $this->api->request(
@@ -67,13 +60,10 @@ public function getWeatherByDate(float $latitude, float $longitude, \DateTimeInt
6760
/**
6861
* Get access to aggregated weather data for a particular date
6962
*
70-
* @throws ValidationException
7163
* @throws ClientExceptionInterface
7264
*/
7365
public function getWeatherSummaryByDate(float $latitude, float $longitude, \DateTimeInterface $date): WeatherSummary
7466
{
75-
$this->validateCoordinate($latitude, $longitude);
76-
7767
$data = $this->api->request(
7868
method: Method::GET,
7969
path: '/data/3.0/onecall/day_summary',

src/Resource/Resource.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
use ProgrammatorDev\OpenWeatherMap\OpenWeatherMap;
66
use ProgrammatorDev\OpenWeatherMap\Resource\Util\CacheTrait;
7-
use ProgrammatorDev\OpenWeatherMap\Resource\Util\ValidationTrait;
87

98
class Resource
109
{
1110
use CacheTrait;
12-
use ValidationTrait;
1311

1412
public function __construct(protected OpenWeatherMap $api) {}
1513
}

src/Resource/Util/UnitSystemTrait.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Resource\Util;
44

5-
use ProgrammatorDev\Validator\Exception\ValidationException;
65
use function DeepCopy\deep_copy;
76

87
trait UnitSystemTrait
98
{
10-
use ValidationTrait;
11-
12-
/**
13-
* @throws ValidationException
14-
*/
159
public function withUnitSystem(string $unitSystem): static
1610
{
17-
$this->validateUnitSystem($unitSystem);
18-
1911
$clone = deep_copy($this, true);
2012
$clone->api->addQueryDefault('units', $unitSystem);
2113

src/Resource/Util/ValidationTrait.php

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

0 commit comments

Comments
 (0)