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