Basic Usage

Danger

We’re sorry but this documentation section is outdated. Please have that in mind when trying to use it. You can help us making documentation up to date via Sylius Github. Thank you!

ZoneMatcher

Zones are not very useful by themselves, but they can take part in e.g. a complex taxation/shipping system. This service is capable of getting a Zone specific for given Address.

It uses a collaborator implementing Doctrine’s ObjectRepository interface to obtain all available zones, compare them with given Address and return best fitted Zone.

First lets make some preparations.

<?php

use Sylius\Component\Addressing\Model\Address;
use Sylius\Component\Addressing\Model\Zone;
use Sylius\Component\Addressing\Model\ZoneInterface;
use Sylius\Component\Addressing\Model\ZoneMember;
use Sylius\Component\Resource\Repository\InMemoryRepository;

$zoneRepository = new InMemoryRepository(ZoneInterface::class);
$zone = new Zone();
$zoneMember = new ZoneMember();

$address = new Address();
$address->setCountry('US');

$zoneMember->setCode('US');
$zoneMember->setBelongsTo($zone);

$zone->addMember($zoneMember);

$zoneRepository->add($zone);

Now that we have all the needed parts lets match something.

<?php

use Sylius\Component\Addressing\Matcher\ZoneMatcher;

$zoneMatcher = new ZoneMatcher($zoneRepository);

$zoneMatcher->match($address); // returns the best matching zone
                               // for the address given, in this case $zone

ZoneMatcher can also return all zones containing given Address.

<?php

$zoneMatcher->matchAll($address); // returns all zones containing given $address

To be more specific you can provide a scope which will narrow the search only to zones with same corresponding property.

<?php

$zone->setScope('earth');

$zoneMatcher->match($address, 'earth'); // returns $zone
$zoneMatcher->matchAll($address, 'mars'); // returns null as there is no
                                          // zone with 'mars' scope

Note

This service implements the ZoneMatcherInterface.

Caution

Throws \InvalidArgumentException.