-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGeocodingResource.php
More file actions
75 lines (65 loc) · 2.14 KB
/
GeocodingResource.php
File metadata and controls
75 lines (65 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace ProgrammatorDev\OpenWeatherMap\Resource;
use ProgrammatorDev\Api\Method;
use ProgrammatorDev\OpenWeatherMap\Entity\Geocoding\ZipLocation;
use ProgrammatorDev\OpenWeatherMap\Entity\Location;
use ProgrammatorDev\OpenWeatherMap\Util\EntityTrait;
use Psr\Http\Client\ClientExceptionInterface;
class GeocodingResource extends Resource
{
use EntityTrait;
private const NUM_RESULTS = 5;
/**
* Get geographical coordinates (latitude, longitude) by using the name of the location (city name or area name)
*
* @return Location[]
* @throws ClientExceptionInterface
*/
public function getByLocationName(string $locationName, int $numResults = self::NUM_RESULTS): array
{
$data = $this->api->request(
method: Method::GET,
path: '/geo/1.0/direct',
query: [
'q' => $locationName,
'limit' => $numResults
]
);
return $this->createEntityList(Location::class, $data);
}
/**
* Get geographical coordinates (latitude, longitude) by using the zip/postal code
*
* @throws ClientExceptionInterface
*/
public function getByZipCode(string $zipCode, string $countryCode): ZipLocation
{
$data = $this->api->request(
method: Method::GET,
path: '/geo/1.0/zip',
query: [
'zip' => \sprintf('%s,%s', $zipCode, $countryCode)
]
);
return new ZipLocation($data);
}
/**
* Get the name of the location (city name or area name) by using geographical coordinates (latitude, longitude)
*
* @return Location[]
* @throws ClientExceptionInterface
*/
public function getByCoordinate(float $latitude, float $longitude, int $numResults = self::NUM_RESULTS): array
{
$data = $this->api->request(
method: Method::GET,
path: '/geo/1.0/reverse',
query: [
'lat' => $latitude,
'lon' => $longitude,
'limit' => $numResults
]
);
return $this->createEntityList(Location::class, $data);
}
}