1
1
<?php
2
2
3
- namespace ProgrammatorDev \OpenWeatherMap \Endpoint ;
3
+ namespace ProgrammatorDev \OpenWeatherMap \Resource ;
4
4
5
- use Http \Client \Exception ;
6
- use ProgrammatorDev \OpenWeatherMap \Endpoint \Util \ValidationTrait ;
7
- use ProgrammatorDev \OpenWeatherMap \Entity \Geocoding \ZipCodeLocation ;
5
+ use ProgrammatorDev \Api \Method ;
8
6
use ProgrammatorDev \OpenWeatherMap \Entity \Location ;
9
- use ProgrammatorDev \OpenWeatherMap \Exception \ApiErrorException ;
10
- use ProgrammatorDev \OpenWeatherMap \Util \EntityListTrait ;
11
- use ProgrammatorDev \YetAnotherPhpValidator \Exception \ValidationException ;
7
+ use ProgrammatorDev \OpenWeatherMap \OpenWeatherMap ;
8
+ use ProgrammatorDev \OpenWeatherMap \Resource \Util \ValidationTrait ;
9
+ use ProgrammatorDev \OpenWeatherMap \Util \EntityTrait ;
10
+ use ProgrammatorDev \Validator \Exception \ValidationException ;
11
+ use Psr \Http \Client \ClientExceptionInterface ;
12
12
13
- class GeocodingEndpoint extends AbstractEndpoint
13
+ class GeocodingResource
14
14
{
15
+ use EntityTrait;
15
16
use ValidationTrait;
16
- use EntityListTrait;
17
17
18
18
private const NUM_RESULTS = 5 ;
19
19
20
- protected int $ cacheTtl = 60 * 60 * 24 * 30 ; // 30 days
20
+ public function __construct ( private readonly OpenWeatherMap $ api ) {}
21
21
22
22
/**
23
23
* @return Location[]
24
- * @throws Exception
25
- * @throws ApiErrorException
24
+ * @throws ClientExceptionInterface
26
25
* @throws ValidationException
27
26
*/
28
27
public function getByLocationName (string $ locationName , int $ numResults = self ::NUM_RESULTS ): array
29
28
{
30
- $ this ->validateSearchQuery ($ locationName , 'locationName ' );
31
- $ this ->validateNumResults ($ numResults );
29
+ $ this ->validateQuery ($ locationName , 'locationName ' );
30
+ $ this ->validatePositive ($ numResults, ' numResults ' );
32
31
33
- $ data = $ this ->sendRequest (
34
- method: ' GET ' ,
32
+ $ data = $ this ->api -> request (
33
+ method: Method:: GET ,
35
34
path: '/geo/1.0/direct ' ,
36
35
query: [
37
36
'q ' => $ locationName ,
@@ -43,39 +42,37 @@ public function getByLocationName(string $locationName, int $numResults = self::
43
42
}
44
43
45
44
/**
46
- * @throws Exception
47
- * @throws ApiErrorException
45
+ * @throws ClientExceptionInterface
48
46
* @throws ValidationException
49
47
*/
50
- public function getByZipCode (string $ zipCode , string $ countryCode ): ZipCodeLocation
48
+ public function getByZipCode (string $ zipCode , string $ countryCode ): Location
51
49
{
52
- $ this ->validateSearchQuery ($ zipCode , 'zipCode ' );
53
- $ this ->validateCountryCode ($ countryCode );
50
+ $ this ->validateQuery ($ zipCode , 'zipCode ' );
51
+ $ this ->validateCountry ($ countryCode, ' countryCode ' );
54
52
55
- $ data = $ this ->sendRequest (
53
+ $ data = $ this ->api -> request (
56
54
method: 'GET ' ,
57
55
path: '/geo/1.0/zip ' ,
58
56
query: [
59
57
'zip ' => \sprintf ('%s,%s ' , $ zipCode , $ countryCode )
60
58
]
61
59
);
62
60
63
- return new ZipCodeLocation ($ data );
61
+ return new Location ($ data );
64
62
}
65
63
66
64
/**
67
65
* @return Location[]
68
- * @throws Exception
69
- * @throws ApiErrorException
66
+ * @throws ClientExceptionInterface
70
67
* @throws ValidationException
71
68
*/
72
69
public function getByCoordinate (float $ latitude , float $ longitude , int $ numResults = self ::NUM_RESULTS ): array
73
70
{
74
71
$ this ->validateCoordinate ($ latitude , $ longitude );
75
- $ this ->validateNumResults ($ numResults );
72
+ $ this ->validatePositive ($ numResults, ' numResults ' );
76
73
77
- $ data = $ this ->sendRequest (
78
- method: ' GET ' ,
74
+ $ data = $ this ->api -> request (
75
+ method: Method:: GET ,
79
76
path: '/geo/1.0/reverse ' ,
80
77
query: [
81
78
'lat ' => $ latitude ,
0 commit comments