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 'google_maps'; |
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
|
|
|
|
80
|
2 |
|
$result = $this->getResult($lat, $lng, $timestamp); |
81
|
|
|
|
82
|
|
|
if (in_array($result->status, $this->errorStatus)) { |
83
|
1 |
|
$message = isset($result->errorMessage) ? $result->errorMessage : $result->status; |
84
|
1 |
|
throw new RuntimeException($message); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return new ZoneInfo($result->timeZoneId, $timestamp); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the URI for the specified location and timestamp. |
92
|
|
|
* |
93
|
|
|
* @param string|float $lat Coordinate latitude. |
94
|
|
|
* @param string|float $lng Coordinate longitude. |
95
|
|
|
* @param int|null $timestamp Seconds since Jan 1, 1970 UTC. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
protected function buildUri($lat, $lng, $timestamp = null) |
100
|
|
|
{ |
101
|
|
|
$params = [ |
102
|
|
|
'key' => $this->apiKey, |
103
|
|
|
'location' => implode(',', [$lat, $lng]), |
104
|
|
|
'timestamp' => $timestamp, |
105
|
2 |
|
]; |
106
|
|
|
|
107
|
|
|
// Remove null values. |
108
|
2 |
|
$params = array_filter($params); |
109
|
|
|
|
110
|
2 |
|
return static::ENDPOINT.'?'.http_build_query($params); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|