devmobgroup/postcodes

此包已被放弃,不再维护。未建议替代包。

荷兰邮政编码数据获取的抽象层。

0.1.0 2018-10-04 13:39 UTC

This package is auto-updated.

Last update: 2022-01-09 23:01:35 UTC


README

Software License StyleCI

此包提供了一些基本功能,可用于特定邮政编码API的实现。此包本身不提供对邮政编码数据的直接访问。

安装

  1. 安装基本包

    composer require devmobgroup/postcodes
    
  2. 选择邮政编码提供商或创建自己的。您可以在本存储库中找到我们所有的提供商实现这里

    • PostcodeAPI.nu(推荐)postcodeapi.nu

      composer require devmobgroup/postcodes-postcode-api-nu
      
    • API Postcode api-postcode.nl

      composer require devmobgroup/postcodes-api-postcode
      

    了解更多关于使用和配置这些提供商的信息。

使用

所有提供商都实现了ProviderInterface,该接口有一个查找方法

$provider->lookup(string $postcode, string $number);

此方法返回一个包含Address实例的数组。此数组永远不会为空,因为相反,NoSuchCombinationException异常表示未找到结果。

$address = $addresses[0];

$address->getPostcode(); // '3011 ED'
$address->getHouseNumber(); // '50'
$address->getStreet(); // 'Schiedamsedijk'
$address->getCity(); // 'Rotterdam' 
$address->getProvince(); // 'Zuid-Holland'
$address->getLatitude(); // '51.9147442'
$address->getLongitude(); // '4.4766394'

还有一个getRaw()方法,通常包含从提供商检索到的原始数据数组

$address->getRaw(); // ['city' => 'Rotterdam', 'year' => 1990, ...]

此外,lookup()方法会抛出异常,应捕获这些异常。

use DevMob\Postcodes\Exceptions\NoSuchCombinationException;
use DevMob\Postcodes\Exceptions\PostcodesException;

try {
    $provider->lookup('3011ED', '50');
} catch (NoSuchCombinationException $e) {
    // Combination of postcode and house number not found
} catch (PostcodesException $e) {
    // Catch-all interface for other exceptions.
    // It's best to always catch these exceptions, because
    // providers may implement their own exceptions that
    // are not documented in the ProviderInterface.
}

扩展

了解更多关于创建自己的提供商.

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件