Issues (9)

src/Services/Geocoding.php (1 issue)

Severity
1
<?php
2
3
namespace kalanis\google_maps\Services;
4
5
6
use kalanis\google_maps\ServiceException;
7
use Psr\Http\Message\RequestInterface;
8
9
10
/**
11
 * Geocoding Service
12
 *
13
 * @see     https://developers.google.com/maps/documentation/geocoding/
14
 * @see     https://developers.google.com/maps/documentation/geocoding/requests-geocoding
15
 */
16
class Geocoding extends AbstractService
17
{
18
    /**
19
     * Reverse Geocode
20
     *
21
     * @param string|null $address
22
     * @param array<string, string|int|float> $params Query parameters
23
     * @return RequestInterface
24
     */
25 1
    public function geocode(?string $address = null, array $params = []): RequestInterface
26
    {
27 1
        if (is_string($address)) {
28 1
            $params['address'] = $address;
29
        }
30
31 1
        return $this->getWithDefaults(
32 1
            static::API_HOST . '/maps/api/geocode/json',
33 1
            $this->queryParamsLang($params)
34 1
        );
35
    }
36
37
    /**
38
     * Reverse Geocode
39
     *
40
     * @param array<string|float>|string $latlng ['lat', 'lng'] or place_id string
41
     * @param array<string, string|int|float> $params Query parameters
42
     * @throws ServiceException
43
     * @return RequestInterface
44
     */
45 4
    public function reverseGeocode(array|string $latlng, array $params = []): RequestInterface
46
    {
47
        // Check if latlng param is a place_id string.
48
        // place_id strings do not contain commas; latlng strings do.
49 4
        if (is_string($latlng)) {
0 ignored issues
show
The condition is_string($latlng) is always false.
Loading history...
50 1
            $params['place_id'] = $latlng;
51
52 3
        } elseif (isset($latlng['lat']) && isset($latlng['lng'])) {
53 1
            $params['latlng'] = sprintf('%1.08F,%1.08F', $latlng['lat'], $latlng['lng']);
54
55 2
        } elseif (isset($latlng[0]) && isset($latlng[1])) {
56 1
            $params['latlng'] = sprintf('%1.08F,%1.08F', $latlng[0], $latlng[1]);
57
58
        } else {
59 1
            throw new ServiceException('Passed invalid values into coordinates! You must use either array with lat and lng or 0 and 1 keys.');
60
61
        }
62
63 3
        return $this->getWithDefaults(
64 3
            static::API_HOST . '/maps/api/geocode/json',
65 3
            $this->queryParamsLang($params)
66 3
        );
67
    }
68
}
69