dev-master
9999999-dev
The Requires
- php >=5.3.0
- league/geotools ~0.3
- guzzle/guzzle 3.*
The Development Requires
by Lukas White
Wallogit.com
2017 © Pedro Peláez
This package has been effectively replaced by a newer version. However, it does not include the web service; it's simply a single class., (*1)
This package serves two purposes:, (*3)
Via Composer:, (*4)
"lukaswhite/uk-postcodes" : "dev-master"
A simple class to encapsulate a UK postcode. While it's primarily used as the return format for the web service, it does have the odd use in itself., (*5)
$postcode = new Lukaswhite\UkPostcodes\UkPostcode('sw1a2aa');
if ($postcode->isValid()) {
// do something...
}
Alternatively, use the static method:, (*6)
if (Lukaswhite\UkPostcodes\UkPostcode::validate('sw1a2aa')) {
// do something...
}
$postcode = new Lukaswhite\UkPostcodes\UkPostcode('sw1a2aa');
print $postcode->formatted();
// > "SW1A 2AA"
The outcode is the first part of a UK postcode. To illustrate:, (*7)
$postcode = new Lukaswhite\UkPostcodes\UkPostcode('sw1a2aa');
print $postcode->getOutcode();
// SW1A
$postcode = new Lukaswhite\UkPostcodes\UkPostcode('GL9 1AH');
print $postcode->getOutcode();
// GL9
$postcode = new Lukaswhite\UkPostcodes\UkPostcode('gl91ah');
print $postcode->getOutcode();
// GL9
Provides wrapper for this web service for geocoding postcodes., (*8)
It's probably easiest to demonstrate the usage through an example:, (*9)
// Create the client
$client = new Lukaswhite\UkPostcodes\UkPostcodesClient();
// Call the web service
$postcode = $client->postcode('sw1a2aa');
print get_class($postcode);
// Lukaswhite\UkPostcodes\UkPostcode
print $postcode->formatted();
// SW1A 2AA
print get_class($postcode->getCoordinate());
// League\Geotools\Coordinate\Coordinate
print $postcode->getCoordinate()->getLatitude();
// 51.503539898876
print $postcode->getCoordinate()->getLongitude();
// -0.12768084037293
print get_class($postcode->getCoordinate()->getEllipsoid());
// League\Geotools\Coordinate\Ellipsoid
print $postcode->council->title;
// City of Westminster
print $postcode->council->code;
// E09000033
print $postcode->council->uri;
// http://statistics.data.gov.uk/id/statistical-geography/E09000033
print $postcode->ward->title;
// St. James's
print $postcode->constituency->title;
// Cities of London and Westminster
If the postcode cannot be found for whatever reason, it'll simply return NULL., (*10)
You can get the nearest postcode to a given point using getNearest()., (*11)
Specify the point using either an instance of League\Geotools\Coordinate\Coordinate or an array of lat / lng, e.g. [51.503539898876, -0.12768084037293]., (*12)
// Create the client $client = new Lukaswhite\UkPostcodes\UkPostcodesClient(); $to = new League\Geotools\Coordinate\Coordinate([51.503539898876, -0.12768084037293]); $postcode = $client->getNearest($to); print $postcode->formatted(); // SW1A 2AA
Get the postcodes within x miles of a given point using getWithin(). First parameter is a point, second is the number of miles (5 miles maxiumum)., (*13)
// Create the client $client = new Lukaswhite\UkPostcodes\UkPostcodesClient(); $location = new League\Geotools\Coordinate\Coordinate([51.503539898876, -0.12768084037293]); $postcodes = $client->getWithin($location, 3); // returns an array of all postcodes within 3 miles of Lat 51.503539898876, Long -0.12768084037293
There are a bunch of unit tests (PHPUnit) in /tests., (*14)
Better error handling., (*15)
Caching., (*16)
Unit tests for getNearest() and getWithin()., (*17)
There is a function currently called getPostTown() adapted from this site, but it doesn't appear to be 100% reliable. When I get time, I'll attempt to fix this., (*18)