hurnell/postcode-api-bundle
Composer 安装命令:
composer require hurnell/postcode-api-bundle
包简介
Symfony 4 bundle for postcodeapi.nu
README 文档
README
postcode-api-bundle
A Symfony 4 bundle to access Dutch postcode API at Postcode API (postcodeapi.nu). Creates a PostcodeModel object based on postcode, house number & number extra combination.
Characteristics/Requirements
Search based on postcode, house number AND house number extra. Note that some combinations of postcode and house number require a house number extra and without this extra value the address does NOT EXIST:
- ('2011XA', 20, '') is not a valid combination. For this combination of postcode and house number, extra must be 'A', 'RD' or 'ZW'.
- For the values ('2011XA', 20, '') the bundle will return an InvalidNumberExtraException with the following message: "House number extra must be (A, RD, ZW) for this combination of postcode and house number."
Installation
- Download via composer.
- Enable bundle by adding class reference to config/bundles.php (if composer did not do that for you).
- Create yaml configuration config/packages/hurnell_postcode_api.yaml with reference to your api_key.
1 - Download via composer
composer require hurnell/postcode-api-bundle:*
2 - Enable bundle
# config/bundles.php Hurnell\PostcodeApiBundle\HurnellPostcodeApiBundle::class => ['all' => true],
3 - Configure with API key
# config/packages/hurnell_postcode_api.yaml hurnell_postcode_api: api_key: 'your_api_key'
Usage
Autowiring is enabled by default so in a controller action (or constructor of other classes)
<?php use Hurnell\PostcodeApiBundle\Service\PostcodeApiClient; // use Exception classes class MyController extends AbstractController { public function getPostcodeAction(PostcodeApiClient $client){ $form = $this->createForm(PostcodeFormType::class); try { $postcodeModel = $client ->makeRequest( '2011XC', 20, 'RD' ) ->populatePostcodeModel(); $postcodeModel->getStreet(); // Doelstraat $postcodeModel->getCity(); // Haarlem // $postcodeModel-> get etc etc // json response return $this->json($postcodeModel->toArray()); } catch (InvalidApiResponseException|InvalidPostcodeException $e) { // handle exception } catch (InvalidHouseNumberException $e) { // handle exception $form->get('number')->addError(new FormError($e->getMessage())); } catch (InvalidNumberExtraException $e) { // handle exception $postcodeModel = $client->populatePostcodeModelWithoutExtra(); return $this->json( array_merge( $postcodeModel->toArray(), ['warning'=>$e->getMessage()] ) ); } } }
Handling InvalidNumberExtraException
Note that an invalid number extra value is not critical. Furthermore the api is not flawless; there are omissions for house number extra.
The method populatePostcodeModelWithoutExtra exists for these situations:
try { $postcodeModel = $client ->makeRequest( '2011XC', 20, 'RD' ) ->populatePostcodeModel(); // ... } catch (InvalidNumberExtraException $e) { $postcodeModel = $client->populatePostcodeModelWithoutExtra(); return $this->json( array_merge( $postcodeModel->toArray(), ['warning'=>$e->getMessage()] ) ); }
hurnell/postcode-api-bundle 适用场景与选型建议
hurnell/postcode-api-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 05 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hurnell/postcode-api-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hurnell/postcode-api-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-13