black / address
PHP 5.4+ 库,使处理地址更安全、更简单、更有趣
Requires
- php: >=5.4.0
Requires (Dev)
- phpspec/phpspec: ~2.0
Suggests
- black/geo: ~1.0
- willdurand/geocoder: ~2.8
This package is auto-updated.
Last update: 2024-08-29 03:55:40 UTC
README
PHP 5.4+ 库,使处理地址更安全、更简单、更有趣!
安装
推荐通过 Composer 安装 Address
{ "require": { "black/address": "@stable" } }
小贴士: 应浏览 black/address
页面以选择要使用的稳定版本,避免使用 @stable
元约束。
用法
该类的使用很简单。完整的邮政地址基于街道(编号和名称)、邮政编码和地区。但我们还需要地区、邮政信箱编号和国家,因此完整的邮政地址应使用所有这些字段。
我们有三个用于完整邮政地址的值对象。
国家
国家由代码和名称组成。此代码是 ISO 3166-1 alpha-2 代码,名称为英文。
示例
$country = new Address\Country("France", "FR"); echo $country->getName(); // return (string) France
可以通过两个静态函数创建 Country 对象。
$country = Address\Country::buildFromISOCode("FR");
$country = Address\Country::buildFromName("France");
可用函数
::buildFromISOCode($code)
::buildFromName($name)
->getName()
->getCode()
->getValue()
->getValueAsArray()
->isEqualTo($country)
异常
如果代码或名称在 Resources\countries.php
中找不到,将抛出 Address\InvalidCountryException()
。
街道
街道由编号和名称组成。
示例
$street = new Address\Street(1600, "Amphitheatre Pkwy"); $street->getValue(','); // return (string) 1600, Amphitheatre Pkwy
可用函数
->getNumber()
->getName()
->getValue($separator)
->getValueAsArray()
->isEqualTo($street)
邮政地址
PostalAddress 是主要值对象。它需要所有信息,但允许空字符串。
$street = new Address\Street(1600, "Amphitheatre Pkwy"); $country = new Address\Country("United States", "US"); $postalAddress = new Address\PostalAddress( $street, 94043, "Mountain View", "CA", 23, $country );
可用函数
->getStreet()
->getStreetName()
->getStreetNumber()
->getPostalCode()
->getLocality()
->getRegion()
->getPostOfficeBoxNumber()
->getCountry()
->getCountryCode()
->getCountryName()
->getValue()
返回数组
格式化器
现在,您有了完整的邮政地址,但在您的脑海中,您可能会说
哦,该死,有时我没有地区或邮政信箱,地址也不是按照法国或美国的相同顺序编写的...
别慌,有一个格式化器可以解决这个问题!
有三种格式化器可以通过相同的 ->format()
函数使用
- Address\Formatter\CountryFormatter
- Address\Formatter\StreetFormatter
- Address\Formatter\PostalAddressFormatter
以下是映射
- 街道:%S
- 街道名称:%n
- 街道编号:%u
- 邮政编码:%P
- 地区:%L
- 地区:%R
- 邮政信箱编号:%B
- 国家:%C
- 国家代码:%C
- 国家名称:%a
如何使用它
$street = new Address\Street(1600, "Amphitheatre Pkwy"); $country = new Address\Country("United States", "US"); $postalAddress = new Address\PostalAddress( $street, 94043, "Mountain View", "CA", 23, $country ); $formatter = new Address\PostalAddressFormatter($postalAddress); echo $formatter->format("%u %n %P %a"); // return 1600 Amphitheatre Pkwy 94043 United States
许可证
Address 根据 MIT 许可证发布。有关详细信息,请参阅捆绑的 LICENSE 文件。
贡献
请参阅 CONTRIBUTING 文件。
致谢
此 README 严重受到由伟大的 @willdurand 编写的 Geocoder 库的启发。这位先生需要您的 [PR][3] 以实现 PHP 中的 REST。
Alexandre "pocky" Balmes alexandre@lablackroom.com。如果您喜欢我的工作,请给我 Flattrs,买礼物 或雇佣我!