1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Teazee package. |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @license MIT License |
9
|
|
|
*/ |
10
|
|
|
namespace Teazee\Provider; |
11
|
|
|
|
12
|
|
|
use Http\Client\HttpClient; |
13
|
|
|
use Http\Message\MessageFactory; |
14
|
|
|
use RuntimeException; |
15
|
|
|
use Teazee\ZoneInfo; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Michael Crumm <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class GoogleMaps extends AbstractHttpProvider |
21
|
|
|
{ |
22
|
|
|
const ENDPOINT = 'https://maps.googleapis.com/maps/api/timezone/json'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $errorStatus = [ |
28
|
|
|
'INVALID_REQUEST', |
29
|
|
|
'OVER_QUERY_LIMIT', |
30
|
|
|
'REQUEST_DENIED', |
31
|
|
|
'UNKNOWN_ERROR', |
32
|
|
|
'ZERO_RESULTS', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $apiKey; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Google constructor. |
42
|
|
|
* |
43
|
|
|
* @param string $apiKey |
44
|
|
|
* @param HttpClient $client |
45
|
|
|
* @param MessageFactory $messageFactory |
46
|
|
|
*/ |
47
|
|
|
public function __construct($apiKey = null, HttpClient $client = null, MessageFactory $messageFactory = null) |
48
|
|
|
{ |
49
|
13 |
|
$this->apiKey = $apiKey; |
50
|
|
|
|
51
|
13 |
|
parent::__construct($client, $messageFactory); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns the name of this provider. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getName() |
60
|
|
|
{ |
61
|
1 |
|
return 'googlemaps'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns ZoneInfo for the specified location and timestamp. |
66
|
|
|
* |
67
|
|
|
* @param string|float $lat Coordinate latitude. |
68
|
|
|
* @param string|float $lng Coordinate longitude. |
69
|
|
|
* @param int $timestamp UNIX timestamp used to determine Daylight Savings Time. |
70
|
|
|
* |
71
|
|
|
* @throws RuntimeException |
72
|
|
|
* |
73
|
|
|
* @return ZoneInfo |
74
|
|
|
*/ |
75
|
|
|
public function find($lat, $lng, $timestamp = null) |
76
|
|
|
{ |
77
|
|
|
// Google Maps TimeZone API requires a timestamp. |
78
|
2 |
|
$timestamp = $timestamp ?: $this->getCurrentTimestamp(); |
79
|
2 |
|
$query = $this->buildQuery($lat, $lng, $timestamp); |
80
|
2 |
|
$response = $this->getResponse($query); |
81
|
2 |
|
$data = json_decode($response->getBody()->getContents()); |
82
|
|
|
|
83
|
|
|
if (in_array($data->status, $this->errorStatus)) { |
84
|
1 |
|
$message = isset($data->errorMessage) ? $data->errorMessage : $data->status; |
85
|
1 |
|
throw new RuntimeException($message); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return new ZoneInfo($data->timeZoneId, $timestamp); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns the URI for the specified location and timestamp. |
93
|
|
|
* |
94
|
|
|
* @param string|float $lat Coordinate latitude. |
95
|
|
|
* @param string|float $lng Coordinate longitude. |
96
|
|
|
* @param int|null $timestamp Seconds since Jan 1, 1970 UTC. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
private function buildQuery($lat, $lng, $timestamp) |
101
|
|
|
{ |
102
|
|
|
$params = [ |
103
|
|
|
'key' => $this->apiKey, |
104
|
|
|
'location' => implode(',', [$lat, $lng]), |
105
|
|
|
'timestamp' => $timestamp, |
106
|
2 |
|
]; |
107
|
|
|
|
108
|
|
|
// Remove null values. |
109
|
2 |
|
$params = array_filter($params); |
110
|
|
|
|
111
|
2 |
|
return static::ENDPOINT.'?'.http_build_query($params); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|