Skip to content

Commit 253b5c8

Browse files
authored
Merge pull request #7 from Ziptastic/2.0
2.0 [WIP]
2 parents 6227a4c + 20e4e00 commit 253b5c8

19 files changed

+379
-557
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
vendor
22
composer.lock
33
.DS_Store
4+
clover.xml
45
build

.travis.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
language: php
22
php:
3-
- '5.4'
4-
- '5.5'
5-
- '5.6'
6-
- '7.0'
7-
install:
8-
- mkdir -p build/logs
3+
- 7.1
4+
- 7.2
95
before_script:
106
- composer self-update
117
- composer install --prefer-source --no-interaction --dev

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The MIT License (MIT)
2-
Copyright (c) 2015 John Pedrie
2+
Copyright (c) 2018 Ziptastic
33

44
Permission is hereby granted, free of charge, to any person obtaining a copy of
55
this software and associated documentation files (the "Software"), to deal in

README.md

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Ziptastic PHP
22

3-
[![Build Status](https://travis-ci.org/Ziptastic/ziptastic-php.svg)](https://travis-ci.org/Ziptastic/ziptastic-php) [![Code Climate](https://codeclimate.com/github/Ziptastic/ziptastic-php/badges/gpa.svg)](https://codeclimate.com/github/Ziptastic/ziptastic-php) [![Test Coverage](https://codeclimate.com/github/Ziptastic/ziptastic-php/badges/coverage.svg)](https://codeclimate.com/github/Ziptastic/ziptastic-php/coverage)
3+
[![Latest Stable Version](https://poser.pugx.org/ziptastic/ziptastic/v/stable)](https://packagist.org/packages/ziptastic/ziptastic) [![Build Status](https://travis-ci.org/Ziptastic/ziptastic-php.svg)](https://travis-ci.org/Ziptastic/ziptastic-php) [![Test Coverage](https://codeclimate.com/github/Ziptastic/ziptastic-php/badges/coverage.svg)](https://codeclimate.com/github/Ziptastic/ziptastic-php/coverage)
44

55
This library is a simple interface for the [Ziptastic API](https://www.getziptastic.com/).
66

@@ -14,56 +14,61 @@ Ziptastic PHP can be installed via composer:
1414
composer require ziptastic/ziptastic
1515
````
1616

17-
## Usage
18-
19-
The simplest usage defaults to the provided Curl service:
17+
Ziptastic PHP relies on [HTTPlug](http://httplug.io/) to make API requests.
18+
HTTPlug is an abstraction which allows you to choose from any one of a large
19+
number of HTTP clients, including the client you might already have installed.
2020

21-
````php
22-
<?php
21+
For more information on getting started with HTTPlug, please refer to the
22+
[HTTPlug for library users](http://docs.php-http.org/en/latest/httplug/users.html)
23+
documentation.
2324

24-
include "vendor/autoload.php";
25+
To just get moving right now, install a couple common dependencies:
2526

26-
$z = Ziptastic\Ziptastic\Lookup::create(getenv('ZIPTASTIC_API_KEY'));
27-
28-
$result = $z->lookup(48038);
29-
30-
?>
31-
````
27+
```
28+
composer require php-http/curl-client guzzlehttp/psr7 php-http/message
29+
```
3230

33-
You can also use a normal instantiation technique:
31+
## Usage
3432

35-
````php
33+
```php
3634
<?php
3735

3836
include "vendor/autoload.php";
3937

40-
$service = new Ziptastic\Ziptastic\Service\CurlService;
41-
$z = new Ziptastic\Ziptastic\Lookup($service, getenv('ZIPTASTIC_API_KEY'));
38+
use Ziptastic\Client;
4239

43-
$result = $z->lookup(48038);
40+
$z = Client::create(getenv('ZIPTASTIC_API_KEY'));
41+
```
4442

45-
?>
46-
````
43+
Ziptastic provides two API methods: Lookup by a postal code (forward lookup),
44+
and lookup by latitude and longitude (reverse lookup).
4745

48-
Results are returned as a collection of class LookupModel:
46+
```php
47+
$result = $z->forward(48038);
48+
$result = $z->reverse(42.331427, -83.0457538, 1000);
49+
```
4950

50-
````php
51+
Results are returned as a list of arrays:
52+
53+
```php
5154
<?php
5255

5356
$lookup = current($result);
54-
echo $lookup->county(); // Macomb
55-
echo $lookup->city(); // Clinton Township
56-
echo $lookup->state(); // Michigan
57-
echo $lookup->stateShort(); // MI
58-
echo $lookup->postalCode(); // 48038
59-
echo $lookup->latitude(); // 42.5868882
60-
echo $lookup->longitude(); // -82.9195514
61-
62-
// timezone() returns an instance of \DateTimeZone
63-
echo $lookup->timezone()->getName(); // America/Detroit
64-
65-
?>
66-
````
57+
echo $lookup['county']; // Macomb
58+
echo $lookup['city']; // Clinton Township
59+
echo $lookup['state']; // Michigan
60+
echo $lookup['state_short']; // MI
61+
echo $lookup['postal_code']; // 48038
62+
echo $lookup['latitude']; // 42.5868882
63+
echo $lookup['longitude']; // -82.9195514
64+
65+
// Timezones are represented by an instance of \DateTimeZone
66+
echo $lookup['timezone']->getName(); // America/Detroit
67+
```
68+
69+
### PHP 5
70+
71+
If you require PHP 5 compatibility, please use Ziptastic-PHP version 1.
6772

6873
## License
6974

composer.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
"name": "ziptastic/ziptastic",
33
"description": "PHP SDK for the Ziptastic Lookup API",
44
"require": {
5-
"php": ">=5.4"
5+
"php": ">=7.1",
6+
"php-http/client-implementation": "^1.0"
67
},
78
"require-dev": {
8-
"phpunit/phpunit": "~4.0",
9-
"codeclimate/php-test-reporter": "dev-master"
9+
"phpunit/phpunit": "^6.0",
10+
"codeclimate/php-test-reporter": "dev-master",
11+
"php-http/mock-client": "^1.0",
12+
"php-http/message": "^1.0",
13+
"php-http/curl-client": "^1.7",
14+
"guzzlehttp/psr7": "^1.4"
1015
},
1116
"license": "MIT",
1217
"authors": [
@@ -17,7 +22,7 @@
1722
],
1823
"autoload": {
1924
"psr-4": {
20-
"Ziptastic\\Ziptastic\\": "src"
25+
"Ziptastic\\": "src"
2126
}
2227
},
2328
"minimum-stability": "stable"

demo.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

phpunit.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@
77
</testsuite>
88
</testsuites>
99

10+
<filter>
11+
<whitelist processUncoveredFilesFromWhitelist="true">
12+
<directory suffix=".php">./src</directory>
13+
<exclude>./vendor</exclude>
14+
<exclude>./tests</exclude>
15+
</whitelist>
16+
</filter>
17+
1018
<logging>
11-
<log type="coverage-html" target="build/coverage" lowUpperBound="35"
12-
highLowerBound="70"/>
13-
<log type="coverage-clover" target="build/logs/clover.xml"/>
19+
<log type="coverage-clover" target="clover.xml"/>
1420
</logging>
1521

1622
</phpunit>

src/Client.php

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ziptastic;
6+
7+
use Http\Client\HttpClient;
8+
use Http\Discovery\HttpClientDiscovery;
9+
use Http\Discovery\MessageFactoryDiscovery;
10+
use Http\Message\MessageFactory;
11+
12+
/**
13+
* The Ziptastic API class.
14+
*
15+
* Example:
16+
* ```
17+
* use Ziptastic\Client;
18+
*
19+
* $ziptastic = Client::create($myApiKey);
20+
* ```
21+
*/
22+
class Client
23+
{
24+
const ZIPTASTIC_LOOKUP_URL = 'https://zip.getziptastic.com/v3/%s/%s';
25+
const ZIPTASTIC_REVERSE_URL = 'https://zip.getziptastic.com/v3/reverse/%s/%s/%s';
26+
27+
/**
28+
* @var HttpClient;
29+
*/
30+
private $http;
31+
32+
/**
33+
* @var MessageFactory
34+
*/
35+
private $messageFactory;
36+
37+
/**
38+
* @var string
39+
*/
40+
private $apiKey;
41+
42+
/**
43+
* @var string
44+
*/
45+
private $countryCode;
46+
47+
/**
48+
* @param HttpClient $http An HTTP Client implementation.
49+
* @param MessageFactory $messageFactory An HTTP message factory implementation.
50+
* @param string $apiKey Ziptastic API Key.
51+
* @param string|null $countryCode 2-character country code. Currently only
52+
* supports "US".
53+
*/
54+
public function __construct(
55+
HttpClient $http,
56+
MessageFactory $messageFactory,
57+
string $apiKey,
58+
string $countryCode = 'US'
59+
) {
60+
$this->http = $http;
61+
$this->messageFactory = $messageFactory;
62+
$this->apiKey = $apiKey;
63+
$this->countryCode = $countryCode;
64+
}
65+
66+
/**
67+
* Create a client instance with the default HTTP handler.
68+
*
69+
* Example:
70+
* ```
71+
* $ziptastic = Client::create($myApiKey);
72+
* ```
73+
*
74+
* @param string $apiKey Ziptastic API Key.
75+
* @param string|null $countryCode 2-character country code. Currently only
76+
* supports "US".
77+
* @return Client
78+
*/
79+
public static function create(
80+
string $apiKey,
81+
string $countryCode = 'US'
82+
): self {
83+
$http = HttpClientDiscovery::find();
84+
$messageFactory = MessageFactoryDiscovery::find();
85+
86+
return new self($http, $messageFactory, $apiKey, $countryCode);
87+
}
88+
89+
/**
90+
* Lookup locale information by a postal code.
91+
*
92+
* Example:
93+
* ```
94+
* $result = $ziptastic->forward('48226');
95+
* foreach ($result as $item) {
96+
* echo $item->postalCode() . PHP_EOL;
97+
* }
98+
* ```
99+
*
100+
* @param string $zipCode The lookup postal code.
101+
* @return array[] A list of arrays containing locale data.
102+
*/
103+
public function forward(string $postalCode): array
104+
{
105+
$url = sprintf(
106+
self::ZIPTASTIC_LOOKUP_URL,
107+
$this->countryCode,
108+
$postalCode
109+
);
110+
111+
return $this->request($url);
112+
}
113+
114+
/**
115+
* Lookup locale information by a set of coordinates.
116+
*
117+
* Example:
118+
* ```
119+
* $result = $ziptastic->reverse(42.331427, -83.0457538);
120+
* foreach ($result as $item) {
121+
* echo $item->postalCode() . PHP_EOL;
122+
* }
123+
* ```
124+
*
125+
* @param float $latitude The lookup centerpoint latitude.
126+
* @param float $longitude The lookup centerpoint longitude.
127+
* @param integer $radius The search radius, in meters. Defaults to `1000`.
128+
* @return array[] A list of arrays containing locale data.
129+
*/
130+
public function reverse(float $latitude, float $longitude, int $radius = 1000): array
131+
{
132+
$url = sprintf(
133+
self::ZIPTASTIC_REVERSE_URL,
134+
$latitude,
135+
$longitude,
136+
$radius
137+
);
138+
139+
return $this->request($url);
140+
}
141+
142+
/**
143+
* Make a request to a given URI with the ziptastic API key.
144+
*
145+
* @param string $url
146+
* @return array[] A list of arrays containing locale data.
147+
*/
148+
private function request(string $url): array
149+
{
150+
try {
151+
$res = $this->http->sendRequest(
152+
$this->messageFactory->createRequest('GET', $url, [
153+
'x-key' => $this->apiKey
154+
])
155+
);
156+
} catch (\Exception $e) {
157+
throw new Exception('An error occurred: '. $e->getMessage(), $e->getCode(), $e);
158+
}
159+
160+
$body = json_decode($res->getBody()->getContents(), true);
161+
162+
if ($res->getStatusCode() !== 200) {
163+
$message = isset($body['message'])
164+
? $body['message']
165+
: 'An error occurred';
166+
167+
throw new Exception($message, $res->getStatusCode());
168+
}
169+
170+
$collection = [];
171+
foreach ($body as $result) {
172+
// If the timezone is not valid, keep null.
173+
try {
174+
$result['timezone'] = new \DateTimeZone($result['timezone']);
175+
} catch (\Exception $e) {
176+
$result['timezone'] = null;
177+
}
178+
179+
$collection[] = $result;
180+
181+
}
182+
183+
return $collection;
184+
}
185+
}

src/Exception.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
<?php namespace Ziptastic\Ziptastic;
1+
<?php
22

3-
class Exception extends \Exception
4-
{}
3+
namespace Ziptastic;
4+
5+
/**
6+
* Represents an error case in the Ziptastic API.
7+
*/
8+
class Exception extends \RuntimeException
9+
{}

0 commit comments

Comments
 (0)