addressing
v2.2.2
PHP 8.0+ 寻址库,由 CLDR 和 Google 的地址数据提供支持。
操纵邮政地址,旨在识别精确的收件人位置以用于运输或计费目的。
特征:
地址格式和细分最初是由 Google 的地址数据服务生成的,现在由图书馆拥有和维护。
另请查看 commerceguys/intl 以了解 CLDR 支持的语言/货币/数字格式。
地址接口表示邮政地址,具有以下字段的 getter:
字段名称遵循 OASIS 可扩展地址语言 (xAL) 标准。
该接口不对可变性做出任何假设。实现应用程序可以扩展接口以提供设置器,或者实现使用 PSR-7 样式和* 变元或依赖于 AddressBuilder 的值对象。提供了一个默认地址值对象,可以用作示例,或由 Doctrine 映射(最好作为可嵌入的)。
地址格式提供以下信息:
国家提供以下信息:
该细分提供以下信息:
细分是分层的,最多可以有三个级别:行政区域 -> 地区 -> 附属地区。
use CommerceGuys Addressing AddressFormat AddressFormatRepository ;
use CommerceGuys Addressing Country CountryRepository ;
use CommerceGuys Addressing Subdivision SubdivisionRepository ;
$ countryRepository = new CountryRepository ();
$ addressFormatRepository = new AddressFormatRepository ();
$ subdivisionRepository = new SubdivisionRepository ();
// Get the country list (countryCode => name), in French.
$ countryList = $ countryRepository -> getList ( ' fr-FR ' );
// Get the country object for Brazil.
$ brazil = $ countryRepository -> get ( ' BR ' );
echo $ brazil -> getThreeLetterCode (); // BRA
echo $ brazil -> getName (); // Brazil
echo $ brazil -> getCurrencyCode (); // BRL
print_r ( $ brazil -> getTimezones ());
// Get all country objects.
$ countries = $ countryRepository -> getAll ();
// Get the address format for Brazil.
$ addressFormat = $ addressFormatRepository -> get ( ' BR ' );
// Get the subdivisions for Brazil.
$ states = $ subdivisionRepository -> getAll ([ ' BR ' ]);
foreach ( $ states as $ state ) {
$ municipalities = $ state -> getChildren ();
}
// Get the subdivisions for Brazilian state Ceará.
$ municipalities = $ subdivisionRepository -> getAll ([ ' BR ' , ' CE ' ]);
foreach ( $ municipalities as $ municipality ) {
echo $ municipality -> getName ();
}
地址根据地址格式进行格式化,采用 HTML 或文本格式。
设置显示地址的格式,始终添加本地化的国家/地区名称。
use CommerceGuys Addressing Address ;
use CommerceGuys Addressing Formatter DefaultFormatter ;
use CommerceGuys Addressing AddressFormat AddressFormatRepository ;
use CommerceGuys Addressing Country CountryRepository ;
use CommerceGuys Addressing Subdivision SubdivisionRepository ;
$ addressFormatRepository = new AddressFormatRepository ();
$ countryRepository = new CountryRepository ();
$ subdivisionRepository = new SubdivisionRepository ();
$ formatter = new DefaultFormatter ( $ addressFormatRepository , $ countryRepository , $ subdivisionRepository );
// Options passed to the constructor or format() allow turning off
// html rendering, customizing the wrapper element and its attributes.
$ address = new Address ();
$ address = $ address
-> withCountryCode ( ' US ' )
-> withAdministrativeArea ( ' CA ' )
-> withLocality ( ' Mountain View ' )
-> withAddressLine1 ( ' 1098 Alta Ave ' );
echo $ formatter -> format ( $ address );
/** Output:
<p translate="no">
<span class="address-line1">1098 Alta Ave</span><br>
<span class="locality">Mountain View</span>, <span class="administrative-area">CA</span><br>
<span class="country">United States</span>
</p>
**/
根据格式要求处理大写字段(以促进自动邮件分类)。
需要指定始发国家/地区代码,以便区分国内和国际邮件。如果是国内邮件,则根本不显示国家/地区名称。如果是国际邮件:
use CommerceGuys Addressing Address ;
use CommerceGuys Addressing Formatter PostalLabelFormatter ;
use CommerceGuys Addressing AddressFormat AddressFormatRepository ;
use CommerceGuys Addressing Country CountryRepository ;
use CommerceGuys Addressing Subdivision SubdivisionRepository ;
$ addressFormatRepository = new AddressFormatRepository ();
$ countryRepository = new CountryRepository ();
$ subdivisionRepository = new SubdivisionRepository ();
// Defaults to text rendering. Requires passing the "origin_country"
// (e.g. 'FR') to the constructor or to format().
$ formatter = new PostalLabelFormatter ( $ addressFormatRepository , $ countryRepository , $ subdivisionRepository , [ ' locale ' => ' fr ' ]);
$ address = new Address ();
$ address = $ address
-> withCountryCode ( ' US ' )
-> withAdministrativeArea ( ' CA ' )
-> withLocality ( ' Mountain View ' )
-> withAddressLine1 ( ' 1098 Alta Ave ' );
echo $ formatter -> format ( $ address , [ ' origin_country ' => ' FR ' ]);
/** Output:
1098 Alta Ave
MOUNTAIN VIEW, CA 94043
ÉTATS-UNIS - UNITED STATES
**/
地址验证依赖于 Symfony Validator 库。
执行的检查:
use CommerceGuys Addressing Address ;
use CommerceGuys Addressing Validator Constraints AddressFormatConstraint ;
use CommerceGuys Addressing Validator Constraints CountryConstraint ;
use Symfony Component Validator Validation ;
$ address = new Address ( ' FR ' );
$ validator = Validation :: createValidator ();
// Validate the country code, then validate the rest of the address.
$ violations = $ validator -> validate ( $ address -> getCountryCode (), new CountryConstraint ());
if (! $ violations -> count ()) {
$ violations = $ validator -> validate ( $ address , new AddressFormatConstraint ());
}
区域是通常用于运输或税收目的的领土分组。例如,与某个区域关联的一组运费,仅当客户的地址属于该区域时,这些费率才可用。
区域可以匹配国家/地区、分区(州/省/直辖市)、邮政编码。邮政编码也可以使用范围或正则表达式来表示。
区域示例:
use CommerceGuys Addressing Address ;
use CommerceGuys Addressing Zone Zone ;
// Create the German VAT zone (Germany and 4 Austrian postal codes).
$ zone = new Zone ([
' id ' => ' german_vat ' ,
' label ' => ' German VAT ' ,
' territories ' => [
[ ' country_code ' => ' DE ' ],
[ ' country_code ' => ' AT ' , ' included_postal_codes ' => ' 6691, 6991:6993 ' ],
],
]);
// Check if the provided austrian address matches the German VAT zone.
$ austrianAddress = new Address ();
$ austrianAddress = $ austrianAddress
-> withCountryCode ( ' AT ' )
-> withPostalCode ( ' 6992 ' );
echo $ zone -> match ( $ austrianAddress ); // true