commerceguys / zone
此包已被弃用且不再维护。未建议替代包。
区域管理库。
v0.8
2018-05-28 22:58 UTC
Requires
- php: >=5.5.0
- commerceguys/addressing: ~1.0
Requires (Dev)
- mikey179/vfsstream: 1.*
- phpunit/phpunit: ~4.0
- squizlabs/php_codesniffer: 2.*
README
zone
重要: 现在zone功能的新版本已直接包含在commerceguys/addressing库中。此库已弃用。
PHP 5.5+ 区域管理库。需要commerceguys/addressing。
区域通常是用于运输或税务目的的领土分组。例如,一组与区域相关的运输费率,只有当客户的地址与区域匹配时,费率才可用。
区域可以匹配其他区域、国家、次级区域(州/省/市),邮政编码。邮政编码也可以用范围或正则表达式表示。
区域示例
- 加利福尼亚州和内华达州
- 比利时、荷兰、卢森堡
- 欧盟
- 德国和一组奥地利邮政编码(6691,6991,6992,6993)
- 奥地利(没有特定的邮政编码:6691,6991,6992,6993)
数据模型
每个区域都有区域成员。如果其区域成员之一与提供的地址匹配,则区域与提供的地址匹配。
基本接口不强制设置器,因为服务类不需要它们。提供扩展接口(ZoneEntityInterface、ZoneMemberEntityInterface)用于此目的,以及匹配Zone和ZoneMember类,这些类可以用作示例或由Doctrine映射。
库包含两种类型的区域成员
use CommerceGuys\Addressing\Address; use CommerceGuys\Zone\Model\Zone; use CommerceGuys\Zone\Model\ZoneMemberCountry; $zone = new Zone(); $zone->setId('german_vat'); $zone->setName('German VAT'); $zone->setScope('tax'); // Create the German VAT zone (Germany and 4 Austrian postal codes). $germanyZoneMember = new ZoneMemberCountry(); $germanyZoneMember->setCountryCode('DE'); $zone->addMember($germanyZoneMember); $austriaZoneMember = new ZoneMemberCountry(); $austriaZoneMember->setCountryCode('AT'); $austriaZoneMember->setIncludedPostalCodes('6691, 6991:6993'); $zone->addMember($austriaZoneMember); // Check if the provided austrian address matches the German VAT zone. $austrianAddress = new Address(); $austrianAddress = $austrianAddress ->withCountryCode('AT') ->withPostalCode('6692'); echo $zone->match($austrianAddress); // true
匹配器
提供匹配器类,用于地址应与系统中的所有区域匹配的场景,匹配的区域按优先级排序。
use CommerceGuys\Addressing\Address; use CommerceGuys\Zone\Matcher\ZoneMatcher; use CommerceGuys\Zone\Repository\ZoneRepository; // Initialize the default repository which loads zones from json files stored in // resources/zone. A different repository might load them from the database, etc. $repository = new ZoneRepository('resources/zone'); $matcher = new ZoneMatcher($repository); $austrianAddress = new Address(); $austrianAddress = $austrianAddress ->withCountryCode('AT') ->withPostalCode('6692'); // Get all matching zones. $zones = $matcher->matchAll($austrianAddress); // Get all matching zones for the 'tax' scope. $zones = $matcher->matchAll($austrianAddress, 'tax'); // Get the best matching zone. $zone = $matcher->match($austrianAddress); // Get the best matching zone for the 'shipping' scope. $zone = $matcher->match($austrianAddress, 'shipping');