lemonade/component_postcode
Composer 安装命令:
composer require lemonade/component_postcode
包简介
A lightweight PHP library for validating and formatting international postal codes (ZIP/PSC).
README 文档
README
A lightweight PHP library for validating and formatting international postal codes (ZIP/PSC).
Provides country-specific formatters, unified exception handling, and clear error codes for integration in forms, APIs, and e-commerce applications.
✨ Features
- ✅ Validation and normalization of postal codes for multiple countries
- ✅ Country-specific formatters (
CZ,SK,DE,GB,FR,IT,…) - ✅ Unified API via
PostcodeFormatter - ✅ Strict typing (PHP 8.1+)
- ✅ Consistent error handling with
PostcodeExceptionandPostcodeErrorCodeenum - ✅ Ready for localization (
messageKey()for translation keys) - ✅ PSR-4 autoloading
- ✅ PHPUnit test coverage
- ✅ PHPStan Level 10 + Strict Rules
- ✅ Verified with 100% PHPUnit coverage
- ✅ Includes support for all 249 ISO 3166-1 alpha-2 country codes
🚀 Installation
composer require lemonade/component_postcode
🔧 Usage
use Lemonade\Postcode\PostcodeFormatter; use Lemonade\Postcode\CountryCode; use Lemonade\Postcode\Exception\PostcodeException; use Lemonade\Postcode\FormatterRegistry; // initialize with default formatters $registry = new FormatterRegistry(); // IMMUTABLE REGISTRY $formatter = new PostcodeFormatter($registry); // READONLY REFERENCE try { $postcode = $formatter->format(CountryCode::CZ, '12000'); // "120 00" } catch (PostcodeException $e) { echo $e->getValue() . ' is invalid: ' . $e->getMessage(); }
➕ Custom Formatters
You can easily extend the library with your own country-specific formatters.
Simply implement CountryPostcodeFormatter and register it via FormatterRegistry.
Example with an anonymous class:
use Lemonade\Postcode\CountryCode; use Lemonade\Postcode\CountryPostcodeFormatter; use Lemonade\Postcode\Exception\InvalidPostcodeException; use Lemonade\Postcode\FormatterRegistry; use Lemonade\Postcode\PostcodeFormatter; $registry = new FormatterRegistry(); // Add custom formatter for Antarctica (AQ) $registry = $registry->register(CountryCode::AQ, new class implements CountryPostcodeFormatter { public function format(string $postcode): string { if (preg_match('/^[0-9]{4}$/', $postcode) !== 1) { throw new InvalidPostcodeException($postcode); } return strtoupper($postcode); } }); $formatter = new PostcodeFormatter($registry); try { echo $formatter->format(CountryCode::AQ, '1231'); // outputs "1231" } catch (InvalidPostcodeException $e) { echo $e->getValue() . ' is invalid: ' . $e->getMessage(); }
📦 Country Formatters
Each country has its own Formatter class under Lemonade\Postcode\Formatter.
Examples:
- CZ_Formatter –
12000→120 00 - SK_Formatter –
81101→811 01 - PL_Formatter –
01001→01-001 - GB_Formatter –
SW1A1AA→SW1A 1AA - LT_Formatter – supports both
12345andLT12345
⚠️ Exceptions
All errors are reported using dedicated exception classes.
This makes it easy to distinguish between invalid input and unsupported countries.
-
InvalidPostcodeException
Thrown when the postcode does not match the expected format
or contains an unsupported value.
Example:"ABCDE"forCZ. -
UnknownCountryException
Thrown when trying to format a postcode for an unsupported
or unrecognized ISO 3166-1 alpha-2 country code.
Example:"XX". -
UnsupportedCountryException
Thrown when the country code is valid, but no formatter is registered for it.Example:
"AQ"(valid ISO code, but not implemented inFormatterMapper)..
All exceptions implement the PostcodeException interface, which provides:
getValue()→ returns the original input (postcodeorcountry)getCode()→ stable numeric error code (PostcodeErrorCode)getMessage()→ translation key for localization
📖 Changelog
All notable changes are documented in the CHANGELOG.md.
🧪 Development & Testing
This package is fully covered by PHPUnit tests and verified with PHPStan Level 10.
Run PHPUnit tests:
composer install vendor/bin/phpunit vendor/bin/phpunit -c vendor/lemonade/component_postcode/phpunit.xml --bootstrap vendor/autoload.php
Run static analysis (PHPStan Level 10 + Strict Rules):
vendor/bin/phpstan analyse vendor/lemonade/component_postcode/src \
--configuration=vendor/lemonade/component_postcode/phpstan.neon.dist \
--memory-limit=1G
lemonade/component_postcode 适用场景与选型建议
lemonade/component_postcode 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 09 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validator」 「formatter」 「zipcode」 「postcode」 「postal」 「lemonade」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lemonade/component_postcode 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lemonade/component_postcode 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lemonade/component_postcode 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
Yii2 phone formatter and validator.
Extension for Opis JSON Schema
Custom laravel monolog logger for datadog logs management, both api and agent ways
Validates Zip/Postal Codes for any country and provides each country's localized name for their version of the Zip/Postal code (CEP, PLZ, PIN code, Eircode, NPA, CAP, ZIP code, etc.).
Symfony bundle for Geonames API
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 38
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-30