| 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\Model\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, HttpClient $client = null, MessageFactory $messageFactory = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 6 |  |         $this->apiKey = $apiKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 6 |  |         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 | 3 |  |         $timestamp = $timestamp ?: $this->getCurrentTimestamp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |         $query = $this->buildQuery($lat, $lng, $timestamp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 3 |  |         $response = $this->getResponse($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 3 |  |         $data = json_decode($response->getBody()->getContents()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if (in_array($data->status, $this->errorStatus)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |             $message = isset($data->errorMessage) ? $data->errorMessage : $data->status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |             throw new RuntimeException($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return $this->returnResult(array_merge($this->getDefaults(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             'id'        => $data->timeZoneId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             'dst'       => $data->dstOffset !== 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             'timestamp' => $timestamp, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             'utcOffset' => $data->rawOffset, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * Returns the URI for the specified location and timestamp. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param string|float $lat       Coordinate latitude. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param string|float $lng       Coordinate longitude. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param int          $timestamp UNIX timestamp used to determine Daylight Savings Time. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     private function buildQuery($lat, $lng, $timestamp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $params = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             'key'       => $this->apiKey, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             'location'  => implode(',', [$lat, $lng]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             'timestamp' => $timestamp, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 3 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         // Remove null values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 3 |  |         $params = array_filter($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 3 |  |         return static::ENDPOINT.'?'.http_build_query($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  |