lukaswhite/uk-postcodes

该包最新版本(1.0.0)没有提供许可信息。

1.0.0 2017-02-01 09:49 UTC

This package is auto-updated.

Last update: 2024-09-18 23:02:55 UTC


README

此包已被一个更新的版本取代。然而,它不包括网络服务;它仅仅是一个单一类。

CircleCI

此包有两个用途

  1. 一个简单的类,代表英国邮政编码,包含格式化和验证等功能。
  2. 用于地理编码邮政编码的此网络服务的包装器。

安装

通过Composer

"lukaswhite/uk-postcodes" : "dev-master"

UkPostcode 类

一个封装英国邮政编码的简单类。虽然它主要用于作为网络服务的返回格式,但它本身也有一些用途。

构造函数

$postcode = new Lukaswhite\UkPostcodes\UkPostcode('sw1a2aa');

验证

if ($postcode->isValid()) {
    // do something...
}

或者,使用静态方法

if (Lukaswhite\UkPostcodes\UkPostcode::validate('sw1a2aa')) {
    // do something...
}

格式化

$postcode = new Lukaswhite\UkPostcodes\UkPostcode('sw1a2aa');

print $postcode->formatted();

// > "SW1A 2AA"

区域代码

区域代码是英国邮政编码的第一部分。为了说明

$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

网络服务客户端

提供对此网络服务的包装,用于地理编码邮政编码。

地理编码邮政编码

下面通过一个示例来说明使用方法

// 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

如果由于任何原因找不到邮政编码,它将简单地返回NULL。

最近的邮政编码

您可以使用getNearest()获取给定点的最近邮政编码。

指定点,可以使用League\Geotools\Coordinate\Coordinate的一个实例,或者一个包含经纬度的数组,例如[51.503539898876, -0.12768084037293]

// 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

指定点x英里内的邮政编码

使用getWithin()获取给定点x英里内的邮政编码。第一个参数是点,第二个是英里数(最大5英里)。

// 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

测试

/tests中有一些单元测试(PHPUnit)。

待办事项

更好的错误处理。

缓存。

getNearest()getWithin()的单元测试。

目前有一个名为getPostTown()的函数,是从此网站改编的,但它似乎并不完全可靠。当我有空时,我会尝试修复这个问题。