Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Apr 26, 2019
1 parent 13272a1 commit 9f3587e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 105 deletions.
65 changes: 3 additions & 62 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,7 @@
# Changelog

All notable changes to `geocoder` will be documented in this file.
All notable changes to `google-time-zone` will be documented in this file.

## 3.4.0 - 2018-01-24
## 1.0.0 - 2019-04-26

- include address components in the response

## 3.3.1 - 2018-05-15

- fix PHP 7.0 requirement in composer.json

## 3.3.0 - 2018-04-20

- add support for setting bounds

## 3.2.0 - 2018-03-30

- add viewport coordinates to `Geocoder::formatResponse()`

## 3.1.1 - 2018-02-21

- improved exception handling

## 3.1.0 - 2017-10-25

- add `getAddressForCoordinates`

## 3.0.1 - 2017-10-24

- fix typo

## 3.0.0 - 2017-10-24

- dropped support for PHP 5
- cleaned up internals
- some small API changes

## 2.3.2 - 2016-11-15

- require Guzzle 6 instead of 5

## 2.3.1 - 2016-09-04

- fixed the naming of variables in the `Geocoder` interface

### 2.3.0 - 2016-09-01

- added support for regions and languages
- added Laravel integration

### 2.2.0 - 2016-08-20

- add `formatted_address` to result

### 2.1.3 - 2016-08-07

- remove `sensor` parameter

### 2.1.2 - 2016-07-04

- upgrade Guzzle version

### 2.1.1 - 2016-03-10

- use https to connect to google
- Initial release
61 changes: 30 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
This package can convert GPS coordinates to timzones using [Google's Time Zone service](https://developers.google.com/maps/documentation/timezone/intro). Here's a quick example:

```php
GoogleTimeZone::getTimeZoneForCoordinates('39.6034810', '-119.6822510');
GoogleTimeZone::getTimeZoneForCoordinates('51.2194475', '4.4024643');

// Will return this array
[
'dstOffset' : 0,
'rawOffset' : -28800,
'timeZoneId' : 'America/Los_Angeles',
'timeZoneName' : 'Pacific Standard Time'
"dstOffset" => 0
"rawOffset" => 3600
"timeZoneId" => "Europe/Brussels"
"timeZoneName" => "Central European Standard Time"
]
```

Expand Down Expand Up @@ -61,40 +61,37 @@ return [
Here's how you can get the timezone for coordinates.

```php
$client = new \GuzzleHttp\Client();

$googleTimeZone = new GoogleTimeZone($client);
$googleTimeZone = new GoogleTimeZone();

$googleTimeZone->setApiKey(config('google-time-zone.key'));

$googleTimeZone->getTimeZoneForCoordinates('39.6034810', '-119.6822510');
$googleTimeZone->getTimeZoneForCoordinates('51.2194475', '4.4024643');

/*
// Will return this array
[
'dstOffset' : 0,
'rawOffset' : -28800,
'timeZoneId' : 'America/Los_Angeles',
'timeZoneName' : 'Pacific Standard Time'
"dstOffset" => 0
"rawOffset" => 3600
"timeZoneId" => "Europe/Brussels"
"timeZoneName" => "Central European Standard Time"
]
*/
```


You can get the result back in a specific language.

```php
$googleTimeZone
->setLanguage('es')
->getTimeZoneForCoordinates('39.6034810', '-119.6822510');
->setLanguage('nl')
->getTimeZoneForCoordinates('51.2194475', '4.4024643');

/*
// Will return this array
[
'dstOffset' : 0,
'rawOffset' : -28800,
'timeZoneId' : 'America/Los_Angeles',
'timeZoneName' : 'Hora de verano del Pacífico'
"dstOffset" => 0
"rawOffset" => 3600
"timeZoneId" => "Europe/Brussels"
"timeZoneName" => "Midden-Europese standaardtijd"
]
*/
```
Expand All @@ -103,16 +100,16 @@ It is possible to specify a timestamp for the location so that daylight savings

```php
$googleTimeZone
->setTimestamp(new DateTime('03/15/2016 12:00'))
->getTimeZoneForCoordinates('39.6034810', '-119.6822510');
->setTimestamp(new DateTime('13 august 2018'))
->getTimeZoneForCoordinates('51.2194475', '4.4024643');

/*
// Will return this array
[
'dstOffset' : 3600,
'rawOffset' : -28800,
'timeZoneId' : 'America/Los_Angeles',
'timeZoneName' : 'Pacific Daylight Time'
"dstOffset" => 3600
"rawOffset" => 3600
"timeZoneId" => "Europe/Brussels"
"timeZoneName" => "Central European Summer Time"
]
*/
```
Expand All @@ -121,19 +118,21 @@ $googleTimeZone
If you are using the package with Laravel, you can simply call `getTimeZoneForCoordinates `.

```php
GoogleTimeZone::getTimeZoneForCoordinates('39.6034810', '-119.6822510');
GoogleTimeZone::getTimeZoneForCoordinates('51.2194475', '4.4024643');

/*
// Will return this array
[
'dstOffset' : 0,
'rawOffset' : -28800,
'timeZoneId' : 'America/Los_Angeles',
'timeZoneName' : 'Pacific Standard Time'
"dstOffset" => 0
"rawOffset" => 3600
"timeZoneId" => "Europe/Brussels"
"timeZoneName" => "Central European Standard Time"
]
*/
```

When no time zone was found a `TimeZoneNotFound` exception will be thrown.

## Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Expand Down
13 changes: 13 additions & 0 deletions src/Exceptions/TimeZoneNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\GoogleTimeZone\Exceptions;

use Exception;

final class TimeZoneNotFound extends Exception
{
public static function forCoordinates(string $latitude, string $longitude): TimeZoneNotFound
{
return new self("No time zone was found for coordinates (`{$latitude}`, `{$longitude}`)");
}
}
25 changes: 15 additions & 10 deletions src/GoogleTimeZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,31 @@
use DateTimeInterface;
use GuzzleHttp\Client;
use Spatie\GoogleTimeZone\Exceptions\GoogleTimeZoneException;
use Spatie\GoogleTimeZone\Exceptions\TimeZoneNotFound;

class GoogleTimeZone
final class GoogleTimeZone
{
/** @var \GuzzleHttp\Client */
protected $client;
private $client;

/** @var string */
protected $endpoint = 'https://maps.googleapis.com/maps/api/timezone/json';
private $endpoint = 'https://maps.googleapis.com/maps/api/timezone/json';

/** @var string */
protected $apiKey;
private $apiKey;

/** @var string */
protected $language;
private $language;

/** @var DateTimeInterface|null */
protected $timestamp;
private $timestamp;

public function __construct(Client $client)
public function __construct(Client $client = null)
{
$this->client = $client;
$this->client = $client ?? new Client();
}

public function setApiKey(string $apiKey) : GoogleTimeZone
public function setApiKey(string $apiKey): GoogleTimeZone
{
$this->apiKey = $apiKey;

Expand Down Expand Up @@ -62,6 +63,10 @@ public function getTimeZoneForCoordinates(string $latitude, string $longitude):

$timezoneResponse = json_decode($response->getBody());

if ($timezoneResponse->status === 'ZERO_RESULTS') {
throw TimeZoneNotFound::forCoordinates($latitude, $longitude);
}

if (! empty($timezoneResponse->errorMessage)) {
throw GoogleTimeZoneException::serviceReturnedError($timezoneResponse->errorMessage);
}
Expand All @@ -85,7 +90,7 @@ private function getPayload(string $latitude, string $longitude): array
];
}

private function formatResponse($timezoneResponse): array
private function formatResponse(object $timezoneResponse): array
{
return [
'dstOffset' => $timezoneResponse->dstOffset,
Expand Down
22 changes: 20 additions & 2 deletions tests/GoogleTimeZoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Spatie\GoogleTimeZone\Exceptions\TimeZoneNotFound;
use Spatie\GoogleTimeZone\GoogleTimeZone;

class GoogleTimeZoneTest extends TestCase
final class GoogleTimeZoneTest extends TestCase
{
/** @var \Spatie\GoogleTimeZone\GoogleTimeZone */
protected $googleTimeZone;
private $googleTimeZone;

/** @var array */
private $historyContainer;
Expand Down Expand Up @@ -154,6 +155,23 @@ public function it_creates_a_valid_request_with_language_and_timestamp()

$this->assertEquals('key=fake_api_key&language=es&location=38.908133%2C-77.047119&timestamp=1458043200', $requestUri->getQuery());
}

/** @test */
public function it_will_throw_an_exception_when_no_results_are_found()
{
$this->expectException(TimeZoneNotFound::class);

$client = $this->createFakeClient([
new Response(200, [], json_encode([
'status' => 'ZERO_RESULTS',
])),
]);

$googleTimezone = new GoogleTimeZone($client);

$googleTimezone->setApiKey('fake_api_key')
->getTimeZoneForCoordinates('38.908133', '-77.047119');
}


private function createFakeClient(array $responses)
Expand Down

0 comments on commit 9f3587e

Please sign in to comment.